c++继承子类构造函数问题

c++中子类在继承基类的时候需要在构造函数中初始化变量。如果基类没有构造函数或者只有不带参数的构造函数,那么子类中的构造函数就不需要调用基类的构造函数了。

个人总结了两点子类中构造函数必须调用父类的构造函数的情况(最常见的情况,不完全):

1.当子类(派生类)中申明了父类(基类)类型的对象的时候,必须在子类的构造函数中进行对象的初始化。

2.当子类继承的父类中有带参数的构造函数的时候,必须在子类的构造函数中调用基类的构造函数。

 

下面以代码为例子:

例一(说明2):

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 class person
 6 {
 7     string name;
 8     int age;
 9     public:
10         person(string _name,int _age):name(_name),age(_age){}
11         void call()
12         {
13             cout<<"I am a person..."<<endl;
14         }
15 };
16 
17 class student : public person
18 {
19     int id;
20     public:
21         student(string _name,int _age,int _id):person(_name,_age),id(_id){}
22         void call()
23         {
24             cout<<"I am a student..."<<endl;
25         }
26 };
27 
28 int main()
29 {
30     person a("ren",22);
31     student b("jeavenwong",22,4002);
32     a.call();
33     b.call();
34     return 0;
35 }

 

例二(说明1)

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 class person
 6 {
 7     string name;
 8     int age;
 9     public:
10         person(string _name,int _age):name(_name),age(_age){}
11         void call()
12         {
13             cout<<"I am a person..."<<endl;
14         }
15 };
16 
17 class animal
18 {
19     string name;
20     int age;
21     public:
22         animal(string _name,int _age):name(_name),age(_age){}
23 };
24 
25 class student : public person
26 {
27     int id;
28     animal object;  //在student类中有animal类的对象,所以必须调用student类的构造函数进行初始化
29     public:
30         student(string _name,int _age,int _id):object(_name,_age),person(_name,_age),id(_id){} //注意:这里用对象的名字来对对象object进行初始化。
31         void call()
32         {
33             cout<<"I am a student..."<<endl;
34         }
35 };
36 
37 int main()
38 {
39     person a("ren",22);
40     student b("jeavenwong",22,4002);
41     a.call();
42     b.call();
43     return 0;
44 }

 

 例三:

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 class Student
 6 {
 7     protected:
 8         string name;
 9         int age;
10         int id;
11     public:
12         Student(string _name,int _age,int _id):name(_name),age(_age),id(_id){cout<<"Base class constructor..."<<endl;}
13         ~Student(){cout<<"Base class destructor..."<<endl;}
14 };
15 
16 class Graduate : public Student
17 {
18     private:
19         string addr;
20     public:
21         Graduate(string _name,int _age,int _id,string _addr):Student(_name,_age,_id),addr(_addr){cout<<"sub class constructor..."<<endl;}
22         ~Graduate(){cout<<"sub class destructor..."<<endl;}
23         void show()
24         {
25             cout<<"my name: "<<name<<endl<<"my age: "<<age<<endl<<"my id: "<<id<<endl<<"my addr: "<<addr<<endl;
26         }
27 };
28 
29 int main()
30 {
31     Graduate me("jeavenwong",22,4002,"Hefei");
32     me.show();
33     return 0;
34 }
35     

例三运行结果:

 

 

如有不对,欢迎批评指正!

posted @ 2017-12-23 22:04  jeavenwong  阅读(4672)  评论(0编辑  收藏  举报