实验内容:
1、编写一个学生和教师数据输入和显示程序,学生数据有编号,姓名,班级和成绩,教师数据有编号,姓名,职称和部门。要求将编号,姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类。
2、合理设计student类、 teacher类以及主函数,完成输入多个学生和教师的信息并显示。
用C++编写相关程序1
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- qfl_sdu 2022-10-16 19:10关注
下面的代码是通过重载输入输出符来实现的,也可通过函数的方式。
#include <iostream> #include <string> using namespace std; class person { public: string id; string name; public: friend istream& operator >>(istream& is,person &p) { is >> p.id >> p.name; return is; } friend ostream& operator <<(ostream& os,person& p) { os << "id:" << p.id << ",name:" << p.name; return os; } }; class Student:public person { public: int grade; //班级 float score;//成绩 public: friend istream& operator >>(istream& is,Student& stu) { is >> stu.id >> stu.name >> stu.grade >> stu.score; return is; } friend ostream& operator <<(ostream& os,Student& stu) { os << "id:" << stu.id << ",name:" << stu.name << ",grade:" << stu.grade << ",score:" << stu.score; return os; } }; class teacher :public person { public: string pro; //职称 string partment; //部门 public: friend istream& operator >>(istream& is,teacher& t) { is >> t.id >> t.name >> t.pro >> t.partment; return is; } friend ostream& operator <<(ostream& os,teacher& t) { os << "id:" << t.id << ",name:" << t.name << ",pro:" << t.pro << ",partment:" << t.partment; return os; } }; int main() { Student s1, s2; teacher t1, t2; cout << "请输入学生1的编号、姓名、班级和成绩:"; cin >> s1; cout << "学生1的信息如下:"; cout << s1 << endl; cout << "请输入学生2的编号、姓名、班级和成绩:"; cin >> s2; cout << "学生2的信息如下:"; cout << s2 << endl; cout << "请输入教师1的编号、姓名、职称和部门:"; cin >> t1; cout << "教师1的信息如下:"; cout << t1 << endl; cout << "请输入教师2的编号、姓名、职称和部门:"; cin >> t2; cout << "教师1的信息如下:"; cout << t2 << endl; return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
- ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
- ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
- ¥15 运动想象脑电信号数据集.vhdr
- ¥15 三因素重复测量数据R语句编写,不存在交互作用
- ¥15 微信会员卡等级和折扣规则
- ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
- ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
- ¥15 gdf格式的脑电数据如何处理matlab
- ¥20 重新写的代码替换了之后运行hbuliderx就这样了