包含子对象的派生类的构造函数

 1 #include <iostream>
 2 #include <string.h>
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 class Student
 6 {
 7     public:
 8         Student(int n,string nam)
 9         {
10             num=n;
11             name=nam;
12         }
13         void display()
14         {
15             cout<<"num:"<<num<<endl<<"name:"<<name<<endl;
16         }
17         protected:
18             int num;
19             string name;
20 };
21 
22 class Student1:public Student
23 {
24     public:
25         Student1(int n,string nam,int n1,string nam1,int a ,string ad):
26             Student(n,nam),monitor(n1,nam1){
27                 age=a;
28                 addr=ad;
29             }
30             void show()
31             {
32                 cout<<"This student is:"<<endl;
33                 display();
34                 cout<<"age:"<<age<<endl;
35                 cout<<"address:"<<addr<<endl<<endl;
36             }
37             void show_monitor()
38             {
39                 cout<<endl<<"Class monitor is:"<<endl;
40                 monitor.display();
41             }
42             private:
43                 Student monitor;
44                 int age;
45                 string addr;
46 };
47 int main(int argc, char** argv) {
48     Student1 stud1(10010,"wang",10001,"li",19,"bj");
49     stud1.show();
50     stud1.show_monitor();
51     return 0;
52 }
原文地址:https://www.cnblogs.com/borter/p/9405464.html