#include<iostream>
using namespace std;
class father{
public:
father(){cout<<"father默认构造函数调用"<<endl;}
father(int i){
data1 = i;
cout<<"father有参构造函数调用"<<endl;
}
~father(){cout<<"father析构函数调用"<<endl;}
private:
int data1;
};
class son:public father{
public:
son();
son(int i,int j);
~son();
private:
int data1;
int data2;
};
son::son(){//自动调用基类B的默认构造函数
cout<<"son默认构造函数调用"<<endl;
}
son::son(int i,int j):father(i){显式调用基类含有参数的构造函数
cout<<"son有参构造函数调用"<<endl;
}
son::~son(){
cout<<"son析构函数调用"<<endl;
}
int main()
{
son A;
son B(1,2);
}
调用son A;
调用son B(1,2);