多层派生时的构造函数

 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 
 6 class Student
 7 {
 8     public:
 9         Student(int n,string nam)
10         {
11             num=n;
12             name=nam;
13         }
14         void display()
15         {
16             cout<<"num:"<<num<<endl;
17             cout<<"name:"<<name<<endl;
18         }
19         
20         protected:
21             int num;
22             string name;
23 };
24 
25 class Student1:public Student
26 {
27     public:
28         Student1(int n,string nam,int a):Student(n,nam)
29         {
30         age=a;
31         }
32         void show()
33         {
34             display();
35             cout<<"age:"<<age<<endl;
36         }
37         private:
38             int age;
39 };
40 
41 class Student2:public Student1
42 {
43     public:
44         Student2(int n,string nam,int a ,int s):Student1(n,nam,a)
45         {
46             score=s;
47         }
48         void show_all()
49         {
50             show();
51             cout<<"score:"<<score<<endl;
52         }
53         private:
54             int score;
55 };
56 
57 int main(int argc, char** argv) {
58     Student2 stud(10010,"li",17,89);
59     stud.show_all();
60     return 0;
61 }

 

posted @ 2018-08-02 10:09  borter  阅读(213)  评论(0编辑  收藏  举报