菜鸟的博客

纵有疾风起,人生不言弃。

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

2023.4.27

 

复制代码
 1 //实验六任务2
 2 #include <iostream>
 3 #include <string>
 4 using namespace std;
 5 class People
 6 {
 7 public:
 8     People(){}
 9     ~People(){}
10     void setValue(int m,string str)
11     {
12         age = m;
13         name = str;
14     }
15     virtual void display() = 0;
16 protected:
17     int age;
18     string name;
19 };
20 class Student :public People
21 {
22 public:
23     Student(){}
24     ~Student(){}
25     void setID(int m)
26     {
27         studentID = m;
28     }
29     void display()
30     {
31         cout<<"姓名:"<<name<<endl;
32         cout<<"年龄:"<<age<<endl;
33         cout<<"学号:"<<studentID<<endl;
34     }
35 private:
36     int studentID;
37 };
38 class Teacher : public People
39 {
40 public:
41     Teacher(){}
42     ~Teacher(){}
43     void setID(int m)
44     {
45         teacherID = m;
46     }
47     void display()
48     {
49         cout<<"姓名:"<<name<<endl;
50         cout<<"年龄:"<<age<<endl;
51         cout<<"工号:"<<teacherID<<endl;
52     }
53 private:
54     int teacherID;
55 };
56 int main()
57 {
58     Student s;
59     Teacher t;
60     s.setValue(18,"张三");
61     t.setValue(28,"李四");
62     s.setID(123);
63     t.setID(654);
64     s.display();
65     t.display();
66     return 0;
67 }
复制代码

 

posted on   hhmzd233  阅读(9)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示