本文主要是介绍c++课后习题-----创建教师学生类实现输入显示,青铜版VS王者版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一个教学系统至少有学生和教师两种人员,假设教师的数据有教师编号、姓名、年龄,性别、职称和系别,学生的数据有学号、姓名、年龄、性别、班级和语文、数学、英语三门课程的成绩。编程完成学生和教师档案数据的输入和显示。
要求如下:设计三个类Person、Teacher、Student,Person是Teacher和Student的基类,具有此二类共有的数据成员姓名、年龄、性别,并具有输入和显示这些数据的成员函数;Teacher类继承了Person类的功能,并增加对教师编号、职称和系别等数据成员进行输入和显示的成员函数;同样的方法完善 Student类的设计。
这个题有两种方法,第一种比较简单输入之后直接输出,没有实现保存,不过通俗易懂
#include<iostream>
#include<cstring>
using namespace std;//基类
class Person
{
public://姓名string name;//年龄int age;//性别string gender;
};//教师类
class Teacher : public Person
{
public://输入void input(){cout << endl;cout << "请输入:教师编号 姓名\t年龄\t性别\t职位\t系别" << endl;cin >> id >> name >> age >> gender >> title >> dept;}//显示void display(){cout << endl;cout << "教师编号:" << id<< " 姓名:" << name<< "\t年龄:" << age<< "\t性别:" << gender<< "\t职称:" << title<< " 系别:" << dept << endl<<endl;}//教师编号int id;//职称string title;//系别string dept;
};//学生类
class Student : public Person
{
public:void input(){cout << endl;cout << "请输入:学生学号 姓名\t年龄\t性别\t班级 语文成绩"<< " 数学成绩" << " 英语成绩" << endl;cin >> id >> name >> age >> gender >> cla >> chinese>>math>>english;}//显示void display(){cout << endl;cout << "学生学号:" << id<< " 姓名:" << name<< "\t年龄:" << age<< "\t性别:" << gender<< "\t班级:" << cla<< "\t语文:" << chinese<< " 数学:" << math<< " 英语:" << english << endl<<endl;}//学号int id;//班级string cla;//语文int chinese;//数学int math;//英语int english;
};int main()
{Teacher t;Student s;int choice;cout << "请选择要输入的信息:\t1:学生\t2:教师\t0:退出" << endl;while (cin >> choice){if (choice == 1){s.input();s.display();}else if(choice==2){t.input();t.display();}else if (choice == 0){cout << "欢迎下次使用!" << endl;system("pause");exit(0);}else{cout << "输入的数据有误,请重新输入!" << endl;}cout << "请选择要输入的信息:\t1:学生\t2:教师\t0:退出" << endl;}
}
运行结果如下
第二种就比较高级了,把数据保存到vector容器中,实现了按照编号查找
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;class Person
{
public://默认构造Person(){}//构造函数Person(string name,int age,string gender){this->m_Name = name;this->m_Age = age;this->m_Gender = gender;}//姓名string m_Name;//年龄int m_Age;//性别 string m_Gender;//析构~Person(){};
};class Teacher:public Person
{
public://默认构造Teacher(){}//构造函数Teacher (int id,string name,int age,string gender,string title,string dept): Person(name,age,gender){this->m_Id = id;this->m_Title = title;this->m_Dept = dept;}//打印void print(){cout << endl;cout << "教师编号:" << this->m_Id<< " 姓名:" << this->m_Name<< "\t年龄:" << this->m_Age<< "\t性别:" << this->m_Gender<< "\t职称:" << this->m_Title<< " 系别:" << this->m_Dept << endl << endl;}//教师信息vector<Teacher> vteacher;//教师信息的添加void AddTeacher();//教师信息的查找void PrintTeacher();//教师编号int m_Id;//职称string m_Title;//系别string m_Dept;//析构~Teacher(){};
};class Student :public Person
{
public://默认构造Student(){}//构造函数Student(int id, string name, int age, string gender, string clas, int chinese,int math,int english) : Person(name, age, gender){this->m_Id = id;this->m_Class = clas;this->m_Chinese = chinese;this->m_Math = math;this->m_English = english;}//打印函数void print(){cout << "学生学号:" << this->m_Id<< " 姓名:" << this->m_Name<< "\t年龄:" << this->m_Age<< "\t性别:" << this->m_Gender<< "\t班级:" << this->m_Class<< "\t语文:" << this->m_Chinese<< " 数学:" << this->m_Math<< " 英语:" << this->m_English << endl << endl;}//学生信息的插入vector<Student> vstudent;//插入学生信息void AddStudent();//学生信息的查找void PrintStudent();//学号int m_Id;//班级string m_Class;//语文int m_Chinese;//数学int m_Math;//英语int m_English;//析构~Student(){};
};void Student ::AddStudent()
{int id,age,chinese,math,english;string name, gender, cla;cout << "请输入:学生学号 姓名\t年龄\t性别\t班级 语文成绩"<< " 数学成绩" << " 英语成绩" << endl;cin >> id >> name >> age >> gender >> cla >> chinese >> math >> english;Student s(id, name, age, gender, cla, chinese, math, english);vstudent.push_back(s);cout << "插入成功!" << endl;
}//学生信息的查找
void Student::PrintStudent()
{int option=0,idn=0;int flag = 0;cout << "请选择 1:查找单个学生 2:显示全体学生信息" << endl;cin >> option;switch (option){case 1:cout << "请输入学生的学号:" << endl;cin >> idn;for (vector<Student>::iterator it = vstudent.begin(); it!=vstudent.end(); it++){if (it->m_Id == idn){it->print();flag = 1;break;}}if (flag == 0)cout << "查无此人" << endl;break;case 2:for (vector<Student>::iterator it = vstudent.begin(); it != vstudent.end(); it++){it->print();}break;}}//教师信息的添加void Teacher::AddTeacher()
{int id, age;string name, gender, title, dept;cout << "请输入:教师编号 姓名\t年龄\t性别\t职位\t系别" << endl;cin >> id >> name >> age >> gender >> title >> dept;Teacher t(id, name, age, gender, title, dept);vteacher.push_back(t);cout << "插入成功!" << endl;
}//教师信息的查找
void Teacher::PrintTeacher()
{int option=0, id=0;int flag = 0;cout << "请选择 1:查找单个教师 2:显示全体教师信息" << endl;cin >> option;switch (option){case 1:cout << "请输入教师的编号:" << endl;cin >> id;for (vector<Teacher>::iterator it = vteacher.begin(); it != vteacher.end(); it++){if (it->m_Id == id){it->print();flag = 1;break;}}if (flag==0)cout << "查无此人!" << endl;break;case 2:for (vector<Teacher>::iterator it = vteacher.begin(); it != vteacher.end(); it++){it->print();}break;}
}int main()
{Student s1;Teacher t1;int choice=0;while (1){cout << "请输入您的选择: 1:添加学生信息 2:添加教师信息"<< "3:查找学生信息 4:查找教师信息 5:退出" << endl;cin >> choice;switch (choice){case 1:s1.AddStudent();break;case 2:t1.AddTeacher();break;case 3:s1.PrintStudent();break;case 4:t1.PrintTeacher();break;case 5:cout << "欢迎下次使用" << endl;system("pause");exit(0);}}}
这篇关于c++课后习题-----创建教师学生类实现输入显示,青铜版VS王者版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!