#include <iostream>
#include <string>
using namespace std;
class Object{
private:
string ms;
public:
Object ( string s ){
cout << "Object ( string s ) : " << s << endl;
this->ms = s;
}
~Object (){
cout << "~Object() : " << ms << endl;
}
};
class Parent : public Object{
private:
string ms;
public:
Parent () : Object( "Default" ){
cout << "Parent() " << endl;
this->ms = "Default";
}
Parent ( string s ) : Object( s ){
cout << "Parent ( string s ) : " << s << endl;
this->ms = s;
}
~Parent (){
cout << "~Parent() : " << ms << endl;
}
};
class Child: public Parent{
private:
Object mO1;
Object mO2;
string ms;
public:
Child () : mO1( "Default 1" ), mO2( "Default 2" ){
cout << "Child() " << endl;
this->ms = "Default";
}
Child ( string s ) : Parent ( s ), mO1( s + " 1" ), mO2( s + " 2" ){
cout << "Child( string s ): " << s << endl;
this->ms = s;
}
~Child (){
cout << "~Child() : " << ms << endl;
}
};
int main ( int argc, char** argv ){
Child c;
Child cc( "cc" );
system ( "pause" );
return 0;
}
C++继承中的构造与析构
原创
©著作权归作者所有:来自51CTO博客作者陈通_TongChen的原创作品,谢绝转载,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C++继承中构造和析构顺序
子类继承父类后,当创建子类对象,也会调用父类的构造函数问题:父类和子类的构造和析构顺
c++ 继承 析构函数 构造函数 父类 -
【C++深度解析】32、继承中的构造与析构
文章目录11
C++深度解析 c 学习