在派生类中引用保护成员

 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         void display();
 9     protected:
10         int num;
11         string name;
12         char sex;
13 };
14 
15 void Student::display()
16 {
17     cout<<"num:"<<num<<endl;
18     cout<<"name:"<<name<<endl;
19     cout<<"sex:"<<sex<<endl;
20 }
21 
22 class Student1:protected Student
23 {
24     public:
25         void display1();
26     private:
27         int age;
28         string addr;
29 };
30 
31 void Student1::display1()
32 {
33     cout<<"num:"<<num<<endl;
34     cout<<"name:"<<name<<endl;
35     cout<<"sex:"<<sex<<endl;
36     cout<<"age:"<<age<<endl;
37     cout<<"address:"<<addr<<endl;
38 }
39 
40 int main(int argc, char** argv) {
41     Student1 stud1;
42     stud1.display1();
43     return 0;
44 }

 

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