// class with multiple constructors // Michael L. Collard // 10/14/99 // class with two constructors - one with parameter, one without // which is invoked depeneds on whether we pass it a parameter // note that all constructors have the same name class MyClass { public: MyClass(); // void or default constructor MyClass(int); // constructor with one parameters MyClass(int, int); // constructor with two parameters private: int data1_; int data2_; }; // void constructor MyClass::MyClass(){ data1_=0; // assign zeros by default data2_=0; } // constructor with one parameter MyClass::MyClass(int data1){ data1_=data1; data2_=0; // only data2_ is assigned zero } // constructorw with two parameters // defined with initializer list MyClass::MyClass(int data1, int data2): data1_(data1), data2_(data2){} int main(){ MyClass ob1; // void constructor is invoked MyClass ob2(2); // one param constructor is invoked MyClass ob3(2, 3); // to param constructor is invoked }