码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-01-09 23:15:52      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:iostream   sys   ios   system   def   this   string   str   child   

#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++继承中的构造与析构

标签:iostream   sys   ios   system   def   this   string   str   child   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!