问题:
Rectangle::Rectangle(int c,double w,double h):Shape(c)
{
width=w;height=h;
}
class Triangle: virtual public Shape
{
public:
Triangle(int c,double b,double h);
protected:
double base,height;
};
Triangle::Triangle(int c,double b,double h):Shape(c)
{
base=b;height=h;
}
class Circle:virtual public Shape
{
public:
Circle(int c,double r);
protected:
double radius;
};
Circle::Circle(int c,double r):Shape(c)
{
radius=r;
}
class MyShape:public Rectangle, public Triangle, public Circle
{
public:
MyShape(int c, double w,double h,double b,double h1,double r,double s);
double area();
double price();
private:
int sign;
};
int main()
{
MyShape ms1=MyShape(1,1,3,1,2,1,3);
MyShape ms2=MyShape(1,1,3,1,2,1,2);
MyShape ms3=MyShape(1,1,3,1,2,1,1);
cout<<"MyShape1 area:"<
}
提示:不用提交全部程序,只提交补充部分
无
输出小聪测试的MyShape类的area与price的值。
MyShape1 area:7.14MyShape1 price:14.42MyShape2 area:7.14MyShape2 price:10.28MyShape3 area:7.14MyShape3 price:7.14
代码:
#include
using namespace std;
#define PI 3.14
class Shape
{
public:
Shape(int c);
protected:
int color;
};
Shape::Shape(int c)
{
color=c;
}
class Rectangle:virtual public Shape
{
public:
Rectangle(int c,double w,double h);
protected:
double width,height;
};
Rectangle::Rectangle(int c,double w,double h):Shape(c)
{
width=w;height=h;
}
class Triangle: virtual public Shape
{
public:
Triangle(int c,double b,double h);
protected:
double base,height;
};
Triangle::Triangle(int c,double b,double h):Shape(c)
{
base=b;height=h;
}
class Circle:virtual public Shape
{
public:
Circle(int c,double r);
protected:
double radius;
};
Circle::Circle(int c,double r):Shape(c)
{
radius=r;
}
class MyShape:public Rectangle, public Triangle, public Circle
{
public:
MyShape(int c, double w,double h,double b,double h1,double r,double s);
double area();
double price();
private:
int sign;
};
MyShape::MyShape(int c, double w,double h,double b,double h1,double r,double s):Shape(c),Rectangle(c,w,h),Triangle(c,b,h1),Circle(c,r),sign(s){}
double MyShape::area()
{
double area;
area=width*Rectangle::height+0.5*base*Triangle::height+PI*radius*radius;
return area;
}
double MyShape::price()
{
double price;
if(sign==1)
price=MyShape::area()*color;
else if(sign==2)
price=MyShape::area()*color+PI*radius*radius;
else if(sign==3)
price=MyShape::area()*color+0.5*base*Triangle::height+PI*radius*radius*2;
return price;
}
int main()
{
MyShape ms1=MyShape(1,1,3,1,2,1,3);
MyShape ms2=MyShape(1,1,3,1,2,1,2);
MyShape ms3=MyShape(1,1,3,1,2,1,1);
cout<<"MyShape1 area:"<
运行结果: