栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++类的继承与派生

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++类的继承与派生

​
#include 
#include 
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;
}

​

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/348164.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号