标签:清理 开始 mic 小结 参数 构造顺序 image 继承 strong
class Child : public Parent { public: //隐式调用父类中的无参构造函数 Child() { cout << "Child()" << endl; } //显式调用父类构造函数 Child(string _str) : Parent(_str) { name = _str; cout << "Child(string _str):"<< name<< endl; } };
// 继承的构造和析构.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <string> using namespace std; class Grandfather { public: Grandfather() { cout << " Grandfather()" << endl; } Grandfather(string _str) { cout << "Grandfather(string _str):" << _str << endl; } }; class Parent :public Grandfather { protected: string name; public: Parent() : Grandfather("default") { cout << "Parent()" << endl; } Parent(string _str):Grandfather("Grandfather") { name = _str; cout << "Parent(string _str):"<<name << endl; } }; class Child : public Parent { Grandfather guest; public: Child() : guest("default") { cout << "Child()" << endl; } //显式调用父类构造函数 Child(string _str) : Parent("father"),guest("guest") { name = _str; cout << "Child(string _str):"<< name<< endl; } }; int main() { Child chenge("son"); }
1 // 继承的构造和析构.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 2 // 3 #include <iostream> 4 #include <string> 5 using namespace std; 6 class Grandfather 7 { 8 string name; 9 public: 10 Grandfather() 11 { 12 cout << " Grandfather()" << endl; 13 } 14 Grandfather(string _str) 15 { 16 name = _str; 17 cout << "Grandfather(string _str):" << _str << endl; 18 } 19 ~Grandfather() 20 { 21 cout << "~Grandfather():" << name << endl; 22 } 23 }; 24 class Parent :public Grandfather 25 { 26 string name; 27 public: 28 Parent() : Grandfather("default") 29 { 30 name = "default"; 31 cout << "Parent()" << endl; 32 } 33 Parent(string _str):Grandfather("Grandfather") 34 { 35 name = _str; 36 cout << "Parent(string _str):"<<name << endl; 37 } 38 ~Parent() 39 { 40 cout << "~Parent():" << name << endl; 41 } 42 }; 43 class Child : public Parent 44 { 45 string name; 46 Grandfather guest; 47 public: 48 Child() : guest("default") 49 { 50 name = "default"; 51 cout << "Child()" << endl; 52 } 53 //显式调用父类构造函数 54 Child(string _str) : Parent("father"),guest("guest") 55 { 56 name = _str; 57 cout << "Child(string _str):"<< name<< endl; 58 } 59 ~Child() 60 { 61 cout << "~Child():" << name << endl; 62 } 63 }; 64 int main() 65 { 66 Child chenge("son"); 67 }
标签:清理 开始 mic 小结 参数 构造顺序 image 继承 strong
原文地址:https://www.cnblogs.com/chengeputongren/p/12243105.html