zoukankan      html  css  js  c++  java
  • 自考新教材-p185

    源程序:

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

    class CStudent //基类
    {
    private:
    string name; //姓名
    string id; //学号
    char gender; //性别,‘F’代表女生,‘M’代表男生
    int age;
    public:
    void PrintInfo();
    void SetInfo(const string &, const string &, int, char);
    void SetName(string);
    string GetName();
    void SetId(string);
    string GetId();
    };

    void CStudent::PrintInfo()
    {
    cout << "姓名: " << name << endl;
    cout << "学号: " << id << endl;
    cout << "年龄: " << age << endl;
    cout << "性别: " << gender << endl;
    }

    void CStudent::SetInfo(const string & name_, const string & id_, int age_, char gender_)
    {
    name = name_;
    id = id_;
    age = age_;
    gender = gender_;
    }

    void CStudent::SetName(string name)
    {
    this->name = name;
    }

    string CStudent::GetName()
    {
    return name;
    }

    void CStudent::SetId(string id)
    {
    this->id = id;
    }

    string CStudent::GetId()
    {
    return id;
    }

    class CUndergraduateStudent :public CStudent //本科生类,继承于类CStudent
    {
    private:
    string department; //学生所属的名
    public:
    void PrintInfo();
    void SetInfo(const string &, const string &, int, char, const string &);
    };

    void CUndergraduateStudent::PrintInfo()
    {
    CStudent::PrintInfo(); //调用基类的公有PrintInfo函数
    cout << "院系: " << department << endl << endl; //访问的是本派生类的成员变量

    }

    void CUndergraduateStudent::SetInfo(const string & name_, const string & id_,
    int age_, char gender_, const string & department_)
    {
    CStudent::SetInfo(name_, id_, age_, gender_); //调用基类的公有SetInfo函数
    department = department_; //访问的是本派生类中的成员变量
    }

    class CGraduatedStudent :public CStudent //研究生类,继承于类CStudent
    {
    private:
    string department; //学生所属的系名
    string advisor; //导师
    public:
    void PrintInfo();
    void SetInfo(const string & name_, const string & id_, int age_, char gender_,
    const string & department_, const string &advisor_);
    };

    void CGraduatedStudent::PrintInfo()
    {
    CStudent::PrintInfo(); //调用基类的公有PrintInfo函数
    cout << "院系: " << department << endl; //访问本派生类中的成员变量
    cout << "导师: " << advisor << endl << endl; //访问本派生类中的成员变量
    }

    void CGraduatedStudent::SetInfo(const string & name_, const string & id_, int age_,
    char gender_, const string & department_, const string & advisor_)
    {
    CStudent::SetInfo(name_, id_, age_, gender_);
    department = department_;
    advisor = advisor_;
    }

    int main()
    {
    CStudent s1;
    CUndergraduateStudent s2;
    CGraduatedStudent s3;
    s2.SetInfo("小张", "2018-6-2-4", 19, 'M', "数学系");
    s3.SetInfo("小李", "M2017-9", 23, 'F', "计算机学院", "孟教授");
    s2.PrintInfo();
    s3.PrintInfo();
    //cout<<s2.name<<endl;
    //s2.name="张一";
    cout << s2.GetName() << endl;
    s2.SetName("张一");
    s2.PrintInfo();
    cout << s2.GetName() << "," << s2.GetId() << endl;
    system("pause");
    return 1;
    }

    运行结果:

  • 相关阅读:
    亚像素显示详解
    一文看懂显示关键材料之彩色滤光片(Color Filter)
    Android Camera2 Opengles2.0 实时滤镜(冷暖色/放大镜/模糊/美颜)
    Android Camera2 预览,拍照,人脸检测并实时展现
    Camera2点击对焦实现2
    关于Android Camera2 API 的自动对焦的坑
    Camera2点击对焦实现
    Android 新老两代 Camera API 大起底
    Android Camera2 参数调节关键字翻译集合,常用关键字解析
    MediaCodec在Android视频硬解码组件的应用
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12259782.html
Copyright © 2011-2022 走看看