[YTU]_2921( Shape系列-7)
来源:互联网 发布:冒充淘宝客服诈骗 编辑:程序博客网 时间:2024/06/06 04:07
Description
小强做的Shape类在本次的测试中出了点状况,发现原来是其中的area函数的问题,请大家根据题意,帮助小强完成改动后的Shape类。
小强写的各种类
class Rectangle:public Shape
{
public:
Rectangle(int c,double w,double h);
double getwidth();
double getheight();
double area();
double price();
protected:
double height;
double width;
};
Rectangle::Rectangle(int c,double w,double h):Shape(c)
{
width=w;
height=h;
}
double Rectangle::getwidth()
{
return width;
}
double Rectangle::getheight()
{
return height;
}
double Rectangle::area()
{
return height*width;
}
double Rectangle::price()
{
return height*width*color;
}
class Circle:public Shape
{
public:
Circle(int c,double r);
double getradius();
double area();
double price();
protected:
double radius;
};
Circle::Circle(int c,double r):Shape(c)
{
radius=r;
}
double Circle::getradius()
{
return radius;
}
double Circle::area()
{
return PI*radius*radius;
}
double Circle::price()
{
return PI*radius*radius*color;
}
class Triangle: public Shape
{
public:
Triangle(int c,double b,double h);
double area();
protected:
double base,height;
};
Triangle::Triangle(int c,double b,double h):Shape(c)
{
base=b;height=h;
}
double Triangle::area()
{
return 0.5*base*height;
}
int main()
{
Shape *pt[3];
Rectangle rr(1,1,1);
Circle cc(2,1);
Triangle tt(2,1,3);
pt[0]=&rr;
pt[1]=&cc;
pt[2]=&tt;
cout<<"Rectangle area:"<<pt[0]->area()<<endl;
cout<<"Circle area:"<<pt[1]->area()<<endl;
cout<<"Triangle area:"<<pt[2]->area()<<endl;
return 0;
}
{
public:
Rectangle(int c,double w,double h);
double getwidth();
double getheight();
double area();
double price();
protected:
double height;
double width;
};
Rectangle::Rectangle(int c,double w,double h):Shape(c)
{
width=w;
height=h;
}
double Rectangle::getwidth()
{
return width;
}
double Rectangle::getheight()
{
return height;
}
double Rectangle::area()
{
return height*width;
}
double Rectangle::price()
{
return height*width*color;
}
class Circle:public Shape
{
public:
Circle(int c,double r);
double getradius();
double area();
double price();
protected:
double radius;
};
Circle::Circle(int c,double r):Shape(c)
{
radius=r;
}
double Circle::getradius()
{
return radius;
}
double Circle::area()
{
return PI*radius*radius;
}
double Circle::price()
{
return PI*radius*radius*color;
}
class Triangle: public Shape
{
public:
Triangle(int c,double b,double h);
double area();
protected:
double base,height;
};
Triangle::Triangle(int c,double b,double h):Shape(c)
{
base=b;height=h;
}
double Triangle::area()
{
return 0.5*base*height;
}
int main()
{
Shape *pt[3];
Rectangle rr(1,1,1);
Circle cc(2,1);
Triangle tt(2,1,3);
pt[0]=&rr;
pt[1]=&cc;
pt[2]=&tt;
cout<<"Rectangle area:"<<pt[0]->area()<<endl;
cout<<"Circle area:"<<pt[1]->area()<<endl;
cout<<"Triangle area:"<<pt[2]->area()<<endl;
return 0;
}
提示:不用提交全部程序,只提交补充部分(包括头文件和π的定义)。
Input
无
Output
小强测试的各个类面积的数据
Sample Output
Rectangle area:1Circle area:3.14Triangle area:1.5#include <iostream>#define PI 3.14using namespace std;class Shape{public: Shape(int c); virtual double area()=0;protected: double color;};Shape::Shape(int c){color=c;}class Rectangle:public Shape { public: Rectangle(int c,double w,double h); double getwidth(); double getheight(); double area(); double price(); protected: double height; double width; }; Rectangle::Rectangle(int c,double w,double h):Shape(c) { width=w; height=h; } double Rectangle::getwidth() { return width; } double Rectangle::getheight() { return height; } double Rectangle::area() { return height*width; } double Rectangle::price() { return height*width*color; } class Circle:public Shape { public: Circle(int c,double r); double getradius(); double area(); double price(); protected: double radius; }; Circle::Circle(int c,double r):Shape(c) { radius=r; } double Circle::getradius() { return radius; } double Circle::area() { return PI*radius*radius; } double Circle::price() { return PI*radius*radius*color; } class Triangle: public Shape{public: Triangle(int c,double b,double h); double area();protected: double base,height;};Triangle::Triangle(int c,double b,double h):Shape(c){ base=b;height=h;}double Triangle::area(){ return 0.5*base*height;} int main() { Shape *pt[3]; Rectangle rr(1,1,1); Circle cc(2,1); Triangle tt(2,1,3); pt[0]=&rr; pt[1]=&cc; pt[2]=&tt; cout<<"Rectangle area:"<<pt[0]->area()<<endl; cout<<"Circle area:"<<pt[1]->area()<<endl; cout<<"Triangle area:"<<pt[2]->area()<<endl;return 0; }
阅读全文
0 0
- [YTU]_2921( Shape系列-7)
- YTU 2920: Shape系列-7
- YTU 2915: Shape系列-1
- YTU 2916: Shape系列-2
- YTU 2917: Shape系列-3
- YTU 2918: Shape系列-5
- YTU 2918: Shape系列-4
- YTU 2922: Shape系列-8
- [YTU]_2915(Shape系列-1)
- [YTU]_2916(Shape系列-2)
- [YTU]_2917(Shape系列-3)
- [YTU]_2918( Shape系列-4)
- [YTU]_2919( Shape系列-5)
- [YTU]_2920( Shape系列-6)
- [YTU]_2922(Shape系列-8)
- Android系列: 自定义图形shape
- OJ嘻唰唰 Shape系列-4
- OJ嘻唰唰Shape系列-6
- ios 添加监听
- 集成U-Share 分享
- vert.x笔记:5.vert.x集成dubbo服务
- 【UGUI】按钮需要绑定带参数的事件或绑定多个事件
- linux等用过的命令
- [YTU]_2921( Shape系列-7)
- Tensorflow: Math变量初始化
- vert.x笔记:6.vert.x集群化部署
- 【ionic】我的第一个ionic程序
- 在springmvc中使用itext生成pdf文件
- 音乐客户端的计时 App
- Java 提升篇必备
- Tensorflow GPU win7
- Ubuntu