#include<iostream>
using namespace std;
class parent
{
public:
parent(int ,int );
void show();
~parent();
private:
int a;
int b;
};
parent::parent(int x,int y):a(x),b(y){}
void parent::show()
{
cout<<"a ="<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"我是父亲类的构造函数"<<endl;
}
parent::~parent()
{
cout<<"我是父亲的析构函数"<<endl;
}
class child: public parent
{
public:
child(int,int ,int );
void show();
~child();
private:
int c;
};
void child::show()
{
parent::show();
cout<<"c ="<<c<<endl;
}
child::child(int x,int y,int z):parent(x,y),c(z){cout<<"我是儿子类的构造函数"<<endl;}
child::~child()
{
cout<<"我是儿子类的析构函数"<<endl;
}
int main()
{
child young1(1,2,3);
young1.show();
return 0;
}
不是说先调用 基类构造函数再调用子类构造函数吗,为啥这个顺序是错误 但是析构函数的顺序又是对的
为啥构造函数的顺序和析构函数顺序一样
最新推荐文章于 2022-11-05 12:24:47 发布