编写一个学生和教师数据的输入和显示程序,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。实验要求:将编号、姓名输入和显示设计成一个类Person,并作为学生数据操作类student

#include
#include
using namespace std;
class Person
{
public:
Person(string a, string s)
{
number = a;
name = s;
cout << “number:” << number << " name:" << name << endl;

}

private:
string number;
string name;
};
class student :public Person
{
public:
student(string a, string s, string m, string n) :Person(a, s)
{
Class = m;
score = n;
}
void disp();
private:
string Class;
string score;
};
void student::disp()
{
cout << “Class:” << Class << " score:" << score << endl;

}
class teacher :public Person
{
public:
teacher(string a, string s, string m, string n) :Person(a, s)
{
title = m;
section = n;
}
void disp();
private:
string title;
string section;
};
void teacher::disp()
{
cout << “title:” << title << " section:" << section << endl;
}
int main()
{
string name1, name2,Class,title, section,number1, number2,score;
cout << “请输入学生信息:学号 姓名 班级 成绩:” << endl;
cin >> number1 >> name1 >> Class >> score;
cout << "请输入老师信息:编号 姓名 职称 部门: " << endl;
cin >> number2 >> name2 >> title >> section;
student s(number1, name1, Class, score);
s.disp();
teacher m(number2,name2,title,section);
m.disp();
return 0;
}

  • 13
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
下面是一个示例代码实现: ```python class Person: def __init__(self, id, name): self.id = id self.name = name class Student(Person): def __init__(self, id, name, cls, score): super().__init__(id, name) self.cls = cls self.score = score class Teacher(Person): def __init__(self, id, name, title, department): super().__init__(id, name) self.title = title self.department = department class StudentData: def __init__(self): self.students = [] def add_student(self, student): self.students.append(student) def display_students(self): for student in self.students: print("ID: {}, Name: {}, Class: {}, Score: {}".format(student.id, student.name, student.cls, student.score)) class TeacherData: def __init__(self): self.teachers = [] def add_teacher(self, teacher): self.teachers.append(teacher) def display_teachers(self): for teacher in self.teachers: print("ID: {}, Name: {}, Title: {}, Department: {}".format(teacher.id, teacher.name, teacher.title, teacher.department)) ``` 在上面的代码中,我们先定义了一个基类 `Person`,它有编号姓名两个属性。然后我们定义了两个子类 `Student` 和 `Teacher`,分别表示学生教师,它们都继承了 `Person` 类,并且分别有自己的属性。最后我们定义了 `StudentData` 和 `TeacherData` 两个类,用于存储学生教师数据,并提供添加和显示数据的方法。 下面是一个使用示例: ```python # 创建一些学生教师对象 s1 = Student(1, "Alice", "Class 1", 90) s2 = Student(2, "Bob", "Class 2", 85) t1 = Teacher(1, "Bill", "Professor", "Department A") t2 = Teacher(2, "Cindy", "Associate Professor", "Department B") # 创建学生教师数据操作对象 student_data = StudentData() teacher_data = TeacherData() # 添加学生教师数据 student_data.add_student(s1) student_data.add_student(s2) teacher_data.add_teacher(t1) teacher_data.add_teacher(t2) # 显示学生教师数据 print("Students:") student_data.display_students() print("Teachers:") teacher_data.display_teachers() ``` 输出结果如下: ``` Students: ID: 1, Name: Alice, Class: Class 1, Score: 90 ID: 2, Name: Bob, Class: Class 2, Score: 85 Teachers: ID: 1, Name: Bill, Title: Professor, Department: Department A ID: 2, Name: Cindy, Title: Associate Professor, Department: Department B ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZengqiangChen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值