C++ day5

1.
2.
 

#include 

using namespace std;

class Bed
{
private:
    string color;
    int sale;
public:
    Bed(){cout << "床 : 无参构造函数"  << endl;}
    Bed(string c , int s):color(c),sale(s)
    {
        cout << "床 : 有参构造函数"  << endl;
    }
    ~Bed()
    {cout << "床 : 析构函数"  << endl;}
    Bed(const Bed &other):color(other.color),sale(other.sale)
    {{cout << "床 : 拷贝构造函数"  << endl;}}
    Bed &operator=(const Bed &other)
    {
        color = other.color;
        sale = other.sale;
        cout << "床 : 拷贝赋值函数" << endl;
        return *this;
    }
    void show()
    {
        cout <<  "床 :" << color << ' ' << sale << endl;
    }
};
class Sofa
{
private:
    string color;
    int sale;
public:
    Sofa(){cout << "沙发 : 无参构造函数"  << endl;}
    Sofa(string c , int s):color(c),sale(s)
    {
        cout << "沙发 : 有参构造函数"  << endl;
    }
    ~Sofa()
    {{cout << "沙发 : 析构函数"  << endl;}}
    Sofa(const Sofa &other):color(other.color),sale(other.sale)
    {{cout << "沙发 : 拷贝构造函数"  << endl;}}
    Sofa &operator=(const Sofa &other)
    {
        color = other.color;
        sale = other.sale;
        cout << "沙发 : 拷贝赋值函数" << endl;
        return *this;
    }
    void show()
    {
        cout <<  "沙发 :" << color << ' ' << sale << endl;
    }
};
class Sofa_Bed:public Sofa,public Bed
{
private :
    string color ;
    int sale;
public:
    Sofa_Bed(){cout << "沙发床 : 无参构造函数"  << endl;}
    Sofa_Bed(string c1, int s1,string c2,int s2,string c3,int s3):Sofa(c1,s1),Bed(c2,s2),color(c3),sale(s3)
    {
        cout << "沙发床 : 有参构造函数"  << endl;
    }
    ~Sofa_Bed()
    {{cout << "沙发床 : 析构函数"  << endl;}}
    Sofa_Bed(const Sofa_Bed &other):Sofa(other),Bed(other),color(other.color),sale(other.sale)
    {{cout << "沙发床 : 拷贝构造函数"  << endl;}}
    Sofa_Bed &operator=(const Sofa_Bed &other)
    {
        color = other.color;
        sale = other.sale;
        Bed :: operator=(other);
        Sofa :: operator=(other);
        cout << "沙发床 : 拷贝赋值函数" << endl;
        return *this;
    }
    void show()
    {
       cout <<  "沙发床 :" << color << ' ' << sale << endl;
    }
};

int main()
{
    cout << "sb1:" << endl;
    Sofa_Bed sb1;
    cout << endl;
    cout << "sb2:" << endl;
    Sofa_Bed sb2("yellow",1200,"blue",1500,"pink",1300);
    sb2.show();
    sb2.Sofa::show();
    sb2.Bed::show();
    cout << endl;
    cout << "sb3:" << endl;
    Sofa_Bed sb3(sb2);
    sb3.show();
    sb3.Sofa::show();
    sb3.Bed::show();
    cout << endl;
    cout << "sb1:" << endl;
    sb1=sb3;
    sb1.show();
    sb1.Sofa::show();
    sb1.Bed::show();
    cout << endl << endl;
    return 0;

}

C++ day5_第1张图片

你可能感兴趣的:(c++)