define the constructor c++ code example
Example 1: constructor c++ struct
struct Rectangle {
int width; // member variable
int height; // member variable
// C++ constructors
Rectangle()
{
width = 1;
height = 1;
}
Rectangle( int width_ )
{
width = width_;
height = width_ ;
}
Rectangle( int width_ , int height_ )
{
width = width_;
height = height_;
}
// ...
};
Example 2: c++ class constructor
class MyClass {
public:
MyClass(); // This is the constructor
private:
int someInt;
};
MyClass::MyClass(void) {
someInt = 1;
}