C++类的继承与派生

该代码示例展示了使用C++定义枚举类型表示性别,创建Person、Student和Teacher类来表示不同类型的人员信息,包括姓名、编号、性别、学院、成绩、职称和部门。并通过Print成员函数输出相关信息。
摘要由CSDN通过智能技术生成
​
#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;
}

​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值