实践考核c++

/*
people是基类,student和graduate是子类,重载“==”运算符
输入2个学生的信息:姓名、编号、身份证号、班级、专业
输入1个研究生的信息:姓名、编号、身份证号、班级、专业、导师
重载“==”,当两个学生的编号相同时,调用重载运算符,输出错误信息。
*/

#include <iostream>
#include <string>
using namespace std;

class People //定义基类
{
public:
string name; //姓名
string number; //编号
string id_number; //身份证号

public:
People() {} //缺省的构造函数
People(string name1, string number1, string id_number1) //带参数的构造函数
{
name = name1;
number = number1;
id_number = id_number1;
}

void input(); //用于输入信息的
void output(); //用于输出信息的
};

//定义input()函数
void People::input()
{
cin >> name;
cin >> number;
cin >> id_number;
}

//定义output()函数
void People::output()
{
cout << "name:" << name<<endl;
cout << "number:" << number << endl;
cout << "id_number:" << id_number << endl;
}

//子类student
class Student :public People
{
public:
string Class;
string major;
public:
Student() {};
Student(string name1, string number1, string id_number1, string Class1, string major1
) :People(name1, number1, id_number1)
{
Class = Class1;
major = major1;
}

void input();
void output();
bool operator==(const Student&); //运算符“==”的重载函数
};

//定义子类Student中的成员函数
void Student::input()
{
People::input();
cin >> Class >> major;
}

void Student::output()
{
People::output();
cout << "Class:" << Class << endl;
cout << "major:" << major << endl;
}

//运算符重载“==”函数
bool Student::operator==(const Student& s)
{
if (number == s.number)
{
cout << "两个学生的编号相同,不合法!";
exit(1);
}
else
return 1;
}

//子类Graduate(研究生)
class Graduate :public Student
{
public:
string daoshi;
public:
Graduate() {};
Graduate(string name1, string number1, string id_number1, string Class1, string major1, string daoshi1
) :Student(name1, number1, id_number1, Class1, major1)
{
daoshi = daoshi1;
}

void input();
void output();
};

void Graduate::input()
{
Student::input();
cin >> daoshi;
}

void Graduate::output()
{
Student::output();
cout << "daoshi:" << daoshi << endl;
}

//主函数

int main()
{
Student s1, s2; //学生对象
Graduate g; //研究生对象
cout << "please input the first student infomation:" << endl;
s1.input();
cout << "please input the second student infomation:" << endl;
s2.input();

s1 == s2;

cout << "please input the graduate infomation:" << endl;
g.input();

cout << "please show the students info:" << endl;
s1.output(); s2.output();

cout << "please show the graduate info:" << endl;
g.output();

return 1;
}

 运行结果:

 

 

 

posted @ 2022-09-18 14:55  bobo哥  阅读(68)  评论(0)    收藏  举报