12.4作业

12.4作业_第1张图片

#include 
 
  
using namespace std;
 
  
 
  
//封装 沙发 类
class Sofa
{
private:
    string sitting;
    int *price;
public:
    //无参
    Sofa(){cout << "Sofa::无参构造函数" << endl;}
    //有参
    Sofa(string s,int price):sitting(s),price(new int (price))
    {
       cout << "Sofa::有参构造函数" << endl;
    }
    //拷贝构造函数
    Sofa(const Sofa &other):sitting(other.sitting),price(new int (*other.price))
    {
        cout <<"Sofa::拷贝构造函数" << endl;
    }
    //拷贝赋值函数
    Sofa &operator=(const Sofa &O1)
    {
        if(this!=&O1)
        {
            sitting=O1.sitting;
            price=O1.price;
 
  
        }
        cout <<"Sofa::拷贝赋值函数" << endl;
        return *this;
    }
    ~Sofa()
    {
        cout << "Sofa::析构函数" << endl;
 
  
        delete price;
        price=nullptr;
    }
     void show()
     {
              cout << sitting << " "< 
  
          }
     };
 
  
 
  
     //封装 床 类
     class Bed
     {
     private:
         string sleep;
         int *size;
     public:
         Bed(){cout << "Bed::无参构造函数" << endl;}
         Bed(string s,int size):sleep(s),size(new int (size))
         {
             cout << "Bed::有参构造函数" << endl;
         }
         //拷贝构造
         Bed (const Bed &other):sleep(other.sleep),size(new int(*other.size))
         {
             cout << "Bed::拷贝构造函数" << endl;
         }
         //拷贝赋值函数
         Bed &operator=(const Bed &o)
         {
           if(this != &o)
             {
                 sleep=o.sleep;
                 size=o.size;
 
  
 
  
             }
           cout <<" Bed::拷贝赋值函数 "< 
  
            return *this;
         }
         ~Bed()
         {
             cout << "Bed::析构函数" << endl;
             delete size;
             size=nullptr;
         }
 
  
         void show()
         {
             cout << sleep <<" "< 
  
         }
     };
 
  
 
  
     //封装 沙发床 类  共有继承于沙发和床
     class Sofa_bed:public Sofa,public Bed
     {
     private:
         string color;
         int *soft;
     public:
         Sofa_bed(){cout << "Sofa_bed::无参构造函数" << endl;}
         Sofa_bed(string s,int p, string sl,int si, string c,int soft):Sofa(s,p),Bed(sl,si),color(c),soft(new int (soft))
         {
             cout << "Sofa_bed::有参构造函数" << endl;
         }
         //拷贝构造函数
         Sofa_bed(const Sofa_bed&other):Sofa(other),Bed(other),color(other.color),soft(new int(*(other.soft)))
         {
             cout << "Sofa_bed::拷贝构造函数"<< endl;
         }
         //拷贝赋值函数
         Sofa_bed &operator=(const Sofa_bed &o)
         {
             if(this!=&o)
             {
                 color=o.color;
                 soft=o.soft;
                 Bed::operator=(o);
                 Sofa::operator=(o);
 
  
             }
 
  
             cout<< "Sofa_bed::拷贝赋值函数"<< endl;
             return *this;
         }
         ~Sofa_bed()
         {
             cout << "Sofa_bed::析构函数" << endl;
 
  
         }
         void show()
         {
             cout << color << " " < 
  
             Bed::show();
             Sofa::show();
         }
 
  
     };
 
  
 
  
 
  
int main()
{
    Sofa_bed b("可睡",10000,"可躺",2000,"pink",9);
 
  
    b.show();
    return 0;
}
 
  

12.4作业_第2张图片

12.4作业_第3张图片

12.4作业_第4张图片

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