[YTU]_2920( Shape系列-6)
Description
Shape系列終于快完成了,小聰可以歇一下了。但是這個時候JC和Kitty把自己的矩形和圓形做好,想給小聰比試一下,小聰也不示弱,拿出來自己的做出的三角形和他們一分高下。他們的比較方式是新建了一個MyShape類來進行比較,請幫小聰完成MyShape類的構造函數,area與price函數(price函數的作用是計算面積與color的乘積)。 注:MyShape類數據成員sign有三個值1、2、3,它們在price函數中決定color的疊加。藍色為color,紅色為color+1,紫色為color+2 sign為1 時???????????????????????????????? ? sign為2 時???????????????????????????? sign為3時 ?????????????????????????????????????????????? #include<iostream>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;
};
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:"<<ms1.area()<<endl;
cout<<"MyShape1 price:"<<ms1.price()<<endl;
cout<<"MyShape2 area:"<<ms2.area()<<endl;
cout<<"MyShape2 price:"<<ms2.price()<<endl;
cout<<"MyShape3 area:"<<ms3.area()<<endl;
cout<<"MyShape3 price:"<<ms3.price()<<endl;
? return 0;
}
提示:不用提交全部程序,只提交補充部分
Input
無
Output
輸出小聰測試的MyShape類的area與price的值。
Sample Output
MyShape1 area:7.14MyShape1 price:14.42MyShape2 area:7.14MyShape2 price:10.28MyShape3 area:7.14MyShape3 price:7.14#include<iostream> 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() {return width*Rectangle::height+base*Triangle::height*0.5+PI*radius*radius; } double MyShape::price() {if(sign==1)return (width*Rectangle::height+base*Triangle::height*0.5+PI*radius*radius)*color;if(sign==2)return width*Rectangle::height*color+base*Triangle::height*0.5*color+PI*radius*radius*(color+1);if(sign==3)return width*Rectangle::height*color+base*Triangle::height*0.5*(color+1)+PI*radius*radius*(color+2); } 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:"<<ms1.area()<<endl; cout<<"MyShape1 price:"<<ms1.price()<<endl; cout<<"MyShape2 area:"<<ms2.area()<<endl; cout<<"MyShape2 price:"<<ms2.price()<<endl; cout<<"MyShape3 area:"<<ms3.area()<<endl; cout<<"MyShape3 price:"<<ms3.price()<<endl;return 0; }總結
以上是生活随笔為你收集整理的[YTU]_2920( Shape系列-6)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [YTU]_2919( Shape系列-
- 下一篇: [YTU]_2921( Shape系列-