编写一个学生和教师数据输入和显示程序

【5.18】编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班别和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并将学生数据操作类student和教师数据操作类teacher的基类。

#include<iostream>    
using namespace std;
class person {
public:
    void input()
{ cout<<" 编号:";  cin>>no;
      cout<<" 姓名: "; cin>>name;
}
    void display()
{ cout<<" 编号:"<<no<<endl;
      cout<<" 姓名: "<<name<<endl;
}
private:
   int no;
   char name[10];
};
class student:public person{
  public:
    void input()
	{ person::input();
      cout<<" 班号:"; 
cin>>depart;
      cout<<" 成绩:"; 
cin>>degree;
}
    void display()
    { person::disp();
      cout<<" 班号:"<<depart<<endl;
      cout<<" 成绩:"<<degree<<endl;
    }
private:
     char depart;
     int degree;
};
class teacher:public person{
private:
    char prof;
    char depart;
public:
    void input()
{ person::input();
cout<<" 职称:"; 
cin>>prof;
cout<<" 部门:";
cin>>depart;
}
   void display()
{ person::display();
cout<<" 职称:"<<prof<<endl;
cout<<" 部门:"<<depart<<endl;
}
};
int  main()
{ student s1;
teacher t1;
cout<<" 输入一个学生数据:\n";
s1.input();
cout<<" 输入一个教师数据:\n"; 
t1.input();
cout<<" 显示一个学生数据:\n"; 
s1.display();
cout<<" 显示一个教师数据:\n"; 
t1.display();
return 0;
}


  • 17
    点赞
  • 84
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,我已经理解了您的要求。下面是一个简单的程序示例,以C++语言为例: ```c++ #include <iostream> #include <string> using namespace std; class person { // person类为基类,包含编号、姓名、年龄等属性 public: int id; string name; int age; }; class student : public person { // student类继承自person类,增加班号和成绩属性 public: int classNo; float score; }; class teacher : public person { // teacher类继承自person类,增加职称和部门属性 public: string title; string department; }; int main() { student s; s.id = 1; s.name = "张三"; s.age = 18; s.classNo = 1; s.score = 90.5; teacher t; t.id = 1001; t.name = "李老师"; t.age = 35; t.title = "教授"; t.department = "计算机科学与技术"; cout << "学生信息:" << endl; cout << "编号:" << s.id << endl; cout << "姓名:" << s.name << endl; cout << "年龄:" << s.age << endl; cout << "班号:" << s.classNo << endl; cout << "成绩:" << s.score << endl; cout << endl; cout << "教师信息:" << endl; cout << "编号:" << t.id << endl; cout << "姓名:" << t.name << endl; cout << "年龄:" << t.age << endl; cout << "职称:" << t.title << endl; cout << "部门:" << t.department << endl; return 0; } ``` 这个程序定义了一个person类,作为学生数据操作类student和教师数据操作类teacher的基类。其中,student类和teacher类都继承自person类,并增加了各自特有的属性。在主函数中,我们创建了一个学生对象s和一个教师对象t,并输出它们的属性信息。 希望这个程序可以帮助您完成本关任务。如果您有任何问题,欢迎随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值