2023/12/4作业

2023/12/4作业_第1张图片

1.

2.

程序:

#include 
 
  
using namespace std;
class Sofe
{
private:
    string siting;
    double *sofe;
public:
    Sofe(){
        cout<<"Sofe::无参"< 
  
    }
    Sofe(string siting,double sofe):siting(siting),sofe(new double(sofe))
    {
        cout<<"Sofe::有参"< 
  
    }
    Sofe(const Sofe &other):siting(other.siting),sofe(new double(*(other.sofe))){
        cout<<"Sofe::拷贝构造"< 
  
    }
    Sofe &operator=(const Sofe &other)
    {
        if(this!=&other)
        {
            siting=other.siting;
            sofe=new double (*(other.sofe));
 
  
         }
        cout<<"Sofe::拷贝赋值"< 
  
      return *this;
    }
 
  
    ~Sofe(){
        cout<<"Sofe::析构"< 
  
    }
    void show()
    {
        cout<<"======================"< 
  
        cout<<"siting="< 
  
 
  
    }
};
class Bed{
private:
    string sleep;
    double *bed;
 
  
public:
    Bed(){
          cout<<"Bed::无参"< 
  
    }
    Bed(string sleep,double bed):sleep(sleep),bed( new double (bed))
    {
          cout<<"Bed::有参"< 
  
    }
    Bed(const Bed &other):sleep(other.sleep),bed(new double(*(other.bed)))
    {
          cout<<"Bed::拷贝构造"< 
  
    }
    Bed operator=(const Bed &other)
    {
        if(this!=&other)
        {
            sleep=other.sleep;
            bed=new double(*(other.bed));
        }
          cout<<"Bed::拷贝赋值"< 
  
        return *this;
    }
    ~Bed()
    {
          cout<<"Bed::析构"< 
  
    }
    void show()
    {
        cout<<"======================"< 
  
        cout<<"sleep="< 
  
 
  
    }
};
class Sofe_Bed:public Sofe,public Bed
{
private:
    int siting_sleep;
    string  * sofe_bed;
public:
    Sofe_Bed(){
        cout<<"Sofe_Bed::无参"< 
  
    }
    Sofe_Bed( string siting,double sofe, string sleep,double bed,int siting_sleep,string sofe_bed):Sofe(siting,sofe),Bed(sleep,bed),siting_sleep(siting_sleep),sofe_bed(new string(sofe_bed))
    {
 
  
   cout<<"Sofe_Bed::有参"< 
  
    }
Sofe_Bed(const Sofe_Bed &other):Sofe(other),Bed(other),siting_sleep(other.siting_sleep),sofe_bed(new string (*(other.sofe_bed)))
{
 
  
  cout<<"Sofe_Bed::拷贝构造"< 
  
}
Sofe_Bed operator=(const Sofe_Bed &other)
{
    if(this!=&other)
    {
      Sofe::operator=(other);
      Bed::operator=(other);
     siting_sleep=other.siting_sleep;
     sofe_bed=other.sofe_bed;
    }
      cout<<"Sofe_Bed::拷贝赋值"< 
  
    return *this;
}
    ~Sofe_Bed()
    {
           cout<<"Sofe_Bed::析构"< 
  
    }
void show()
{
    cout<<"======================"< 
  
 
  
     cout<<"siting_sleep="< 
  
}
 
  
};
 
  
int main()
{
    Sofe_Bed sd;
    Sofe_Bed sb1("坐下",20,"睡觉",40,120,"又坐又睡");
    cout<<"======================="< 
  
    cout<<"sb1"< 
  
    sb1.show();
    sb1.Sofe::show();
    sb1.Bed::show();
    sd=sb1;
 sd.show();
 cout<<"======================="< 
  
 cout<<"sd"< 
  
 sd.Sofe::show();
 sd.Bed::show();
 
  
 
  
    return 0;
}
成果图

2023/12/4作业_第2张图片

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