公有继承中派生类Student对基类Person成员的访问 代码参考

0
9
 1 #include <iostream>
 2 #include <cstring>
 3 
 4 using namespace std;
 5 
 6 class Person
 7 {
 8     private:
 9         char Name[20];
10         char Sex;
11         int Age;
12     public:
13         void Register(char *name, int age, char sex);
14         void ShowMe();
15 };
16 
17 class Student:public Person
18 {
19     private:
20         int Number;
21         char ClassName[10];
22     public:
23         void RegisterStu(char *classname, int number, char *name, int age, char sex);
24         void ShowStu();
25 };
26 
27 void Person::Register(char *name, int age, char sex)
28 {
29     strcpy(Name,name);
30     Age=age;
31     Sex=sex;
32     return;
33 }
34 void Person::ShowMe()
35 {
36     cout<<Name<<' '<<Age<<' '<<Sex<<endl;
37     return;
38 }
39 
40 void Student::RegisterStu(char *classname, int number, char *name, int age, char sex)
41 {
42     strcpy(ClassName,classname);
43     Number=number;
44     Person::Register(name,age,sex);
45     return;
46 }
47 
48 void Student::ShowStu()
49 {
50     cout<<Number<<' '<<ClassName<<' ';
51     Person::ShowMe();
52     Person::ShowMe();
53     return;
54 }
55 
56 int main()
57 {
58     char ClassName[10],Name[20];
59     char Sex;
60     int Age,Number;
61     Student one;
62     cin>>ClassName>>Number>>Name>>Age>>Sex;
63     one.RegisterStu(ClassName,Number,Name,Age,Sex);
64     one.ShowStu();
65     return 0;
66 }
67     
68     

<

发布回复

请输入评论!
请输入你的名字