欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-25 17:03:14
1 #include 2 #include 3 4 using namespace std; 5 6 class Person 7 { 8 private: 9 char Name[20]; 10 char Sex; 11 int Age; 12 publi ......
 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