对类组合和继承的思考
#include <iostream>
using namespace std;
class A{
public:
int i;
A(int ii):i(ii){cout<<"A constructor"<<endl;}
~A() {cout<<"A destructor"<<endl;}
void fun() const {cout<<"A fun()"<<endl;}
};
class B{
public:
int i;
B(int ii):i(ii){cout<<"B constructor"<<endl;}
~B(){cout<<"B destructor"<<endl;}
void fun() const {cout<<"B fun()"<<endl;}
};
class C:public B{
public:
A a;
C(int ii):B(ii),a(2*ii) {cout<<"C constructor"<<endl;}
~C() {cout<<"C destructor"<<endl;};
void fun() const {a.fun();B::fun();}
};
int main()
{
C c(47);
c.fun();
c.a.fun();
cout<<"c.i:"<<c.i<<endl;
cout<<"c.a.i:"<<c.a.i<<endl;
return 0;
}