| <<< User-defined data types | Index | C++ struct, cont. >>> |
In C++, the struct may also contain functions as members. For example,
struct Rectangle {
int width; // member variable
int height; // member variable
// C++
void set_dimensions( int width_ , int height_ )
{
width = width_;
height = height_;
}
int area() const
{
return ( width * height );
}
};
Similar to variables, the struct must be defiend before it can be used.
Notice that member function area( ) is declared const. The const modifier tells the compiler that the function does not modify the object's state, only reading its data.
| <<< User-defined data types | Index | C++ struct, cont. >>> |