constructor-inh/multiple-2.cc - Constructors and multiple inheritance - implicit activation of default constructor in Base. | Lecture 5 - slide 4 : 40 Program 3 |
// Illustrates that the default Base2 constructor may be called implicitly. // Illustrates that a default constructor in a base class may be implicitly activated. #include <iostream> #include <string> using namespace std; class Base1{ public: int i; string s; Base1(int i, string s): i(i), s(s){ } }; class Base2{ public: int j; bool b; Base2(): j(11), b(false){ // Now with a default constructor } Base2(int j, bool b): j(j), b(b){ } }; class Derived: public Base1, public Base2{ public: double d; Derived(double d, int i, string s): Base1(i+1, s+s), // The Base2 constructor is not activated explictly here. d(d){ // ... The default constructor in Base2 is activated implicitly. } }; int main(){ Derived d1(5.0, 1, "AP"); cout << d1.i << endl; // 2 cout << d1.s << endl; // APAP cout << d1.j << endl; // 11 cout << d1.b << endl; // 0 cout << d1.d << endl; // 5 }