12.4 作业

1,完成沙发床的多继承

#include 

using namespace std;

class Physical{
private:
    double *weight;
protected:
    double *high;
public:
    string other_name;

public:
    Physical()
    {
        cout << "Physical::无参构造函数" << endl;
    }
    Physical(string other_name,double high,double weight):other_name(other_name),high(new double(high)),weight(new double(weight))
    {
        cout << "Physical::有参构造函数" << endl;
    }
    ~Physical()
    {
        cout << "Physical::析构函数" << endl;
    }
    Physical(const Physical &other):other_name(other.other_name),high(new double(*(other.high))),weight(new double(*(other.weight)))
    {
        cout << "Physical::拷贝构造函数" << endl;
    }
    Physical &operator=(const Physical &other)
    {
        if(this != &other){
            other_name=other.other_name;
            high=new double(*(other.high));
            weight=new double(*(other.weight));
        }
        cout << "Physical::拷贝赋值函数" << endl;
        return *this;
    }
    void show(){
        cout << other_name << endl;
        cout << *high << endl;
        cout << *weight << endl;
    }
};

class Learn{
private:
    double *score;
protected:
    int age;
public:
    int id;

public:
    Learn(){
        cout << "Learn::无参构造函数" << endl;
    }
    Learn(double score,int age,int id):score(new double(score)),age(age),id(id)
    {
        cout << "Learn::有参构造函数" << endl;
    }
    Learn(const Learn &other):score(new double(*(other.score))),age(other.age),id(other.id)
    {
        cout << "Learn::拷贝构造函数" << endl;
    }
    ~Learn(){
        cout << "Learn::析构函数" << endl;
    }
    Learn &operator=(const Learn &other)
    {
        if(this != &other){
            id=other.id;
            age=other.age;
            score=new double(*(other.score));
        }
        cout << "Learn::拷贝赋值函数" << endl;
        return *this;
    }
    void show(){
        cout << id << endl;
        cout << age << endl;
        cout << *score << endl;
    }

};

class Person:public Physical,public Learn{
private:
    string name;
public:
    Person()
    {
        cout << "Person::无参构造函数" << endl;
    }
    Person(string name,int id,int age,double score,string other_name,double high,double weight):Physical(other_name,high,weight),Learn(id,age,score),name(name)
    {
        cout << "Person::有参构造函数" << endl;
    }
    Person(const Person &other):Physical(other),Learn(other),name(other.name)
    {
         cout << "Person::拷贝构造函数" << endl;
    }
    ~Person()
    {
         cout << "Person::析构函数" << endl;
    }
    Person &operator=(const Person &other)
    {
        if(this != &other){
            name=other.name;
            Physical::operator=(other);
            Learn::operator=(other);
        }
        cout << "Person::拷贝赋值函数" << endl;
        return *this;
    }
    void show(){
        Physical::show();
        Learn::show();
        cout << name << endl;

    }
};
int main()
{
    Physical s1;
    Learn s2;
    Person s3("张三",1001,18,99.98,"小三",190.5,156.5);
//    s3.show();
//    Person s4=s3;
//    s4.show();
    Person s4;
    s4=s3;
    s4.show();


    return 0;
}

结果:

12.4 作业_第1张图片

2,思维导图

12.4 作业_第2张图片

你可能感兴趣的:(c++,算法,开发语言)