首页 > 编程语言 > 详细

C++继承中的构造与析构

时间:2018-01-09 23:09:42      阅读:238      评论:0      收藏:0      [点我收藏+]
#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++继承中的构造与析构

原文:http://blog.51cto.com/chen0547/2059220

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!