4.22

#include<iostream>

using namespace std;

#include<string>

class student {

public:

    student(void);

    ~student(void);

    void setValues(int n, string str, char c);

    void display();

protected:

    int num;

    string name;

    char sex;

};

student::student(void){}

student::~student(void){}

void student::setValues(int n, string str, char c) {

    num = n;

    name = str;

    sex = c;

}

void student::display() {

    cout << num << "" << name << "" << sex << endl;

}

class postgraduent : public student {

public:

    postgraduent(void);

    ~postgraduent(void);

    void setAdvisor(string str) {

        advisor = str;

    }

    string getAdvisor() {

        return advisor;

    }

private:

    string advisor;

};

postgraduent::postgraduent(void) {

 

}

postgraduent::~postgraduent(void) {

 

}

void main() {

    postgraduent xq;

    xq.setValues(1122, "小强", 'M');

    xq.setAdvisor("Prof.Zhu");

    xq.display();

    cout << "Advisor:" << xq.getAdvisor() << endl;

    system("pause");

}

 

#include<iostream>

using namespace std;

#include<string>

class CPolygon {

protected:

    int width, height;

public:

    void set_values(int a, int b)

    {

        width = a; height = b;

    }

};

class CRectangle : public CPolygon {

public:

    int area()

    {

        return (width * height);

    }

};

class CTriangle : public CPolygon {

public:

    int area()

    {

        return (width * height / 2);

    }

};

posted @ 2023-04-22 22:59  刘梦磊  阅读(22)  评论(0)    收藏  举报