Shape系列-6
时间: 1ms 内存:128M
描述:
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;
}提示:不用提交全部程序,只提交补充部分
输入:
无
输出:
输出小聪测试的MyShape类的area与price的值。
示例输入:
示例输出:
MyShape1 area:7.14
MyShape1 price:14.42
MyShape2 area:7.14
MyShape2 price:10.28
MyShape3 area:7.14
MyShape3 price:7.14
提示:
参考答案(内存最优[0]):
#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 w1,double r,double s);
double area();
double price();
private:
int sign;
};
MyShape::MyShape(int c, double w,double h,double b,double w1,double r,double s)
:Shape(c),Rectangle(c,w,h),Triangle(c,b,w1),Circle(c,r)
{
sign=s;
}
double MyShape::area()
{
double a;
a=width*Rectangle::height+
(base*Triangle::height)/2+PI*radius*radius;
return a;
}
double MyShape::price()
{
double p,ra,ta,ca;
ra=width*Rectangle::height;
ta=width*Triangle::height/2;
ca=PI*radius*radius;
if(sign==1)
p=color*(ra+ta+ca);
else if(sign==2)
p=color*ra+color*ta+(color+1)*ca;
else p=color*ra+(color+1)*ta+(color+2)*ca;
return p;
}
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<<"MyShape area:"<<ms1.area()<<endl;
cout<<"MyShape price:"<<ms1.price()<<endl;
cout<<"MyShape area:"<<ms2.area()<<endl;
cout<<"MyShape price:"<<ms2.price()<<endl;
cout<<"MyShape area:"<<ms3.area()<<endl;
cout<<"MyShape price:"<<ms3.price()<<endl;
return 0;
}
参考答案(时间最优[0]):
#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 w1,double r,double s);
double area();
double price();
private:
int sign;
};
MyShape::MyShape(int c, double w,double h,double b,double w1,double r,double s)
:Shape(c),Rectangle(c,w,h),Triangle(c,b,w1),Circle(c,r)
{
sign=s;
}
double MyShape::area()
{
double a;
a=width*Rectangle::height+
(base*Triangle::height)/2+PI*radius*radius;
return a;
}
double MyShape::price()
{
double p,ra,ta,ca;
ra=width*Rectangle::height;
ta=width*Triangle::height/2;
ca=PI*radius*radius;
if(sign==1)
p=color*(ra+ta+ca);
else if(sign==2)
p=color*ra+color*ta+(color+1)*ca;
else p=color*ra+(color+1)*ta+(color+2)*ca;
return p;
}
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<<"MyShape area:"<<ms1.area()<<endl;
cout<<"MyShape price:"<<ms1.price()<<endl;
cout<<"MyShape area:"<<ms2.area()<<endl;
cout<<"MyShape price:"<<ms2.price()<<endl;
cout<<"MyShape area:"<<ms3.area()<<endl;
cout<<"MyShape price:"<<ms3.price()<<endl;
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。