| constructor-inh/multiple-1.cc - Constructors and multiple inheritance. | Lecture 5 - slide 4 : 40 Program 2 |
// Multiple inheritance and constructors.
// Illustrates use of Base constructors in Derived constructor with multiple inheritance.
#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;
// No default constructor in Base2. In the next version, there will be 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), Base2(i+2, false), d(d){
}
};
int main(){
Derived d1(5.0, 1, "AP");
cout << d1.d << endl; // 5
cout << d1.i << endl; // 2
cout << d1.s << endl; // APAP
cout << d1.j << endl; // 3
cout << d1.b << endl; // 0
}