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     

例三運行結果:

 

 

如有不對,歡迎批評指正!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM