码迷,mamicode.com
首页 > 其他好文 > 详细

第46课 继承中的构造与析构

时间:2018-09-02 20:26:48      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:直接   继承   auto   分享图片   play   子类   using   析构   child   

思考:

如何初始化父类成员?

父类构造函数和子类构造函数有什么关系?

子类对象的构造:

技术分享图片

 

子类构造函数对继承而来的成员进行初始化有两种方式:

1、直接通过初始化列表或者赋值的方式进行初始化

2、调用父类构造函数进行初始化

父类构造函数在子类中的调用方式:

技术分享图片

 

显式调用只能在初始化列表进行。

如下:

技术分享图片

 

 示例程序:

技术分享图片

第30行会调用子类的无参构造函数Child(),而这个构造函数会默认调用父类的无参构造函数,因为父类没有默认构造函数,因此报错。

 在父类中加上无参构造函数即可:

技术分享图片

继续完善:

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 class Parent 
 7 {
 8 public:
 9     Parent()
10     {
11         cout << "Parent()" << endl;
12     }
13     Parent(string s)
14     {
15         cout << "Parent(string s) : " << s << endl;
16     }
17 };
18 
19 class Child : public Parent
20 {
21 public:
22     Child()
23     {
24         cout << "Child()" << endl;
25     }
26     Child(string s) : Parent(s)
27     {
28         cout << "Child(string s) : " << s << endl;
29     }
30 };
31 
32 int main()
33 {       
34     Child c; 
35     Child cc("cc");
36     
37     return 0;
38 }

35行会触发26行的子类构造函数,在调用子类的构造函数前,先调用父类的Parent(string s)这个构造函数。

输出结果如下:

技术分享图片

 子类对象的构造:

技术分享图片

 

技术分享图片

深度解析:

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 class Object
 7 {
 8 public:
 9     Object(string s)
10     {
11         cout << "Object(string s) : " << s << endl;
12     }
13 };
14 
15 class Parent : public Object
16 {
17 public:
18     Parent() : Object("Default")
19     {
20         cout << "Parent()" << endl;
21     }
22     Parent(string s) : Object(s)
23     {
24         cout << "Parent(string s) : " << s << endl;
25     }
26 };
27 
28 class Child : public Parent
29 {
30     Object mO1;
31     Object mO2;
32 public:
33     Child() : mO1("Default 1"), mO2("Default 2")
34     {
35         cout << "Child()" << endl;
36     }
37     Child(string s) : Parent(s), mO1(s + " 1"), mO2(s + " 2")
38     {
39         cout << "Child(string s) : " << s << endl;
40     }
41 };
42 
43 int main()
44 {       
45     Child cc("cc");
46     
47     return 0;
48 }

运行结果如下:

技术分享图片

 

 

子类对象的析构:

技术分享图片

 

 

 示例程序:

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 class Object
 7 {
 8     string ms;
 9 public:
10     Object(string s)
11     {
12         cout << "Object(string s) : " << s << endl;
13         ms = s;
14     }
15     ~Object()
16     {
17         cout << "~Object() : " << ms << endl;
18     }
19 };
20 
21 class Parent : public Object
22 {
23     string ms;
24 public:
25     Parent() : Object("Default")
26     {
27         cout << "Parent()" << endl;
28         ms = "Default";
29     }
30     Parent(string s) : Object(s)
31     {
32         cout << "Parent(string s) : " << s << endl;
33         ms = s;
34     }
35     ~Parent()
36     {
37         cout << "~Parent() : " << ms << endl;
38     }
39 };
40 
41 class Child : public Parent
42 {
43     Object mO1;
44     Object mO2;
45     string ms;
46 public:
47     Child() : mO1("Default 1"), mO2("Default 2")
48     {
49         cout << "Child()" << endl;
50         ms = "Default";
51     }
52     Child(string s) : Parent(s), mO1(s + " 1"), mO2(s + " 2")
53     {
54         cout << "Child(string s) : " << s << endl;
55         ms = s;
56     }
57     ~Child()
58     {
59         cout << "~Child() " << ms << endl;
60     }
61 };
62 
63 int main()
64 {       
65     Child cc("cc");
66     
67     cout << endl;
68     
69     return 0;
70 }

执行结果如下:

技术分享图片

 

小结:

技术分享图片

 

第46课 继承中的构造与析构

标签:直接   继承   auto   分享图片   play   子类   using   析构   child   

原文地址:https://www.cnblogs.com/wanmeishenghuo/p/9574731.html

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