继承中的构造与析构

//构造与析构
继承中的构造与析构:
原则:先构造父类,再构造成员变量,最后构造自己,先析构自己,再析构成员变量,最后析构父类
class parent{
public:
parent(int a, int b){
cout << “this is parent(int a, int b)—” << endl;
this->m_a = a;
this->m_b = b;
}
~parent(){
cout << “this is ~parent()—” << endl;
}
public:
int m_a;
int m_b;
};
class child :public parent{
public:
child(int a, int b, int c) : parent(a, b){
cout << “this is child(int a, int b, int c):parent(a, b)–” << endl;
this->m_c = c;
}
~child(){
cout << “this is ~child()–” << endl;
}
private:
int m_c;
};
void display(){
child c(4, 5, 6);
}
Main:
display();//1.this is parent(int a, int b)— 2.this is child(int a, int b, int c):parent(a, b)–
//3.this is ~child()-- 4.this is ~parent()—
2:
class Object
{
public:
Object(const char* s)
{
cout << “Object()” << " " << s << endl;
}
~Object()
{
cout << “~Object()” << endl;
}
};
class Parent : public Object
{
public:
Parent(const char* s) : Object(s)
{
cout << “Parent()” << " " << s << endl;
}
~Parent()
{
cout << “~Parent()” << endl;
}
};
class Child : public Parent
{
protected:
Object o1;
Object o2;
public:
Child() : o2(“o2”), o1(“o1”), Parent(“Parameter from Child!”)
{
cout << “Child()” << endl;
}
~Child()
{
cout << “~Child()” << endl;
}
};
void run05()
{
Child child;
}
Main:
cout << “demo05_extend_construct_destory.cpp” << endl;
run05();
//1.Object() Parameter from Child! 2.Parent() Parameter from Child!
//3.Object() o1 4.Object() o2 5.Child() 6.~Child() 7.~Object()
//8.~Object() 9.~Parent() 10.~Object()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值