c++问题,在哪调用了2次构造函数,在哪调用了析构函数3次
#include<iostream.h>classPoint{protected:intx;inty;public:virtualvoidshow()=0;};class...
#include <iostream.h>
class Point
{
protected:
int x;
int y;
public:
virtual void show()=0;
};
class Rectangle:public Point
{
private:
float H;
float W;
float SArea;
public:
Rectangle(float a=0 ,float b=0):H(a),W(b){cout<<"\n初始化矩形类的一个对象";}
~Rectangle(){cout<<"\n回收点矩形类对象的内存空间";}
Rectangle(Rectangle &d)
{ this->H=d.H;
this->W=d.W;
this->SArea=d.SArea;
}
float Area()
{ SArea= H*W;
return SArea;
}
friend float operator+(Rectangle &,Rectangle &);
void show( )
{ cout<<"\nH="<<H;
cout<<"\nW="<<W;
cout<<"\nSArea="<<SArea;
}
};
float operator+(Rectangle &a,Rectangle &b)
{
return a.SArea+b.SArea;
}
int main()
{
Point *pt;
Rectangle x(3,4),y(5,6);
x.Area();
y.Area();
Rectangle t(x);
float z;
z=x+y;
pt=&x;
pt->show();
pt=&t;
pt->show();
return 0;
} 展开
class Point
{
protected:
int x;
int y;
public:
virtual void show()=0;
};
class Rectangle:public Point
{
private:
float H;
float W;
float SArea;
public:
Rectangle(float a=0 ,float b=0):H(a),W(b){cout<<"\n初始化矩形类的一个对象";}
~Rectangle(){cout<<"\n回收点矩形类对象的内存空间";}
Rectangle(Rectangle &d)
{ this->H=d.H;
this->W=d.W;
this->SArea=d.SArea;
}
float Area()
{ SArea= H*W;
return SArea;
}
friend float operator+(Rectangle &,Rectangle &);
void show( )
{ cout<<"\nH="<<H;
cout<<"\nW="<<W;
cout<<"\nSArea="<<SArea;
}
};
float operator+(Rectangle &a,Rectangle &b)
{
return a.SArea+b.SArea;
}
int main()
{
Point *pt;
Rectangle x(3,4),y(5,6);
x.Area();
y.Area();
Rectangle t(x);
float z;
z=x+y;
pt=&x;
pt->show();
pt=&t;
pt->show();
return 0;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询