#include <iostream>
#include <string>
using namespace std;
enum Gender {Male, Female};
ostream& operator << (ostream& os, Gender gender) {
if (gender == Female) {
os << "Female";
}
else {
os << "Male";
}
return os;
}
class Person{
//to do
private:
string name_;
int number_;
Gender gender_;
public:
Person() {
name_ = "";
gender_ = Male;
number_ = 0;
}
Person(string name, Gender gender, int number) {
name_ = name;
gender_ = gender;
number_ = number;
}
~Person() {}
void Print() const {
cout << "姓名:" << name_ + " " << "性别:" << gender_ << " 编号:" << number_ << endl;
}
};
class Student : public Person{
//to do
private:
string faculty_;
int score_;
public:
Student() {
faculty_ = "";
score_ = 0;
}
Student(string name, Gender gender, int number, string faculty, int score) : Person(name, gender, number) {
faculty_ = faculty;
score_ = score;
}
~Student() {}
void Print() const {
Person::Print();
cout << "学院:" << faculty_ + " " << "成绩:" << score_ << endl;
}
};
class Teacher : public Person{
//to do
private:
string title_;
string department_;
public:
Teacher() {
title_ = "";
department_ = "";
}
Teacher(string name, Gender gender, int number, string title, string department) : Person(name, gender, number) {
title_ = title;
department_ = department;
}
~Teacher() {}
void Print() const {
Person::Print();
cout << "职称:" << title_ + " " << "部门:" << department_ + " " << endl;
}
};
int main()
{
Person person("王维维", Male, 1007);
Student student("李君瑞", Male, 1007,"Information Technology", 100);
Teacher teacher("童振", Male, 1007, "Professor", "Teaching Affairs");
person.Print();
student.Print();
teacher.Print();
return 0;
}
C++类的继承与派生
最新推荐文章于 2024-11-09 20:38:27 发布
该代码示例展示了使用C++定义枚举类型表示性别,创建Person、Student和Teacher类来表示不同类型的人员信息,包括姓名、编号、性别、学院、成绩、职称和部门。并通过Print成员函数输出相关信息。
摘要由CSDN通过智能技术生成