#include "iostream"
using namespace std;
//结论
using namespace std;
//结论
//先调用父类构造函数 再调用子类构造函数
//析构的顺序和构造相反
/*子类对象在创建时首先会调用父类的构造函数
2 父类的构造函数执行结束后,执行子类的构造函数
3 当父类的构造函数有参数时,需要在子类的初始化列表中显示调用
4 析构函数调用的先后顺序与构造函数的相反*/
class Parent
{
public:
Parent(int a,int b)
{
this->a = a;
this->b = b;
//析构的顺序和构造相反
/*子类对象在创建时首先会调用父类的构造函数
2 父类的构造函数执行结束后,执行子类的构造函数
3 当父类的构造函数有参数时,需要在子类的初始化列表中显示调用
4 析构函数调用的先后顺序与构造函数的相反*/
class Parent
{
public:
Parent(int a,int b)
{
this->a = a;
this->b = b;
cout << "父类的构造函数" << endl;
}
~Parent()
{
cout << "父类的析构函数" << endl;
}
protected:
private:
int a;
int b;
};
class child :public Parent
{
public:
child(int a, int b, int c) :Parent(a, b)
{
this->c = c;
cout << "子类的构造函数" << endl;
}
~child()
{
cout << "子类的析构函数" << endl;
}
protected:
private:
int c;
};
void playobj()
{
child c1(1, 2, 3);
}
void main()
{
//Parent p1(1, 2);
playobj();
system("pause");
return ;
}
}
~Parent()
{
cout << "父类的析构函数" << endl;
}
protected:
private:
int a;
int b;
};
class child :public Parent
{
public:
child(int a, int b, int c) :Parent(a, b)
{
this->c = c;
cout << "子类的构造函数" << endl;
}
~child()
{
cout << "子类的析构函数" << endl;
}
protected:
private:
int c;
};
void playobj()
{
child c1(1, 2, 3);
}
void main()
{
//Parent p1(1, 2);
playobj();
system("pause");
return ;
}