Shape系列-7

Shape系列-7

时间: 1ms        内存:128M

描述:

小强做的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; 
}
提示:不用提交全部程序,只提交补充部分(包括头文件和π的定义)。

输入:

输出:

小强测试的各个类面积的数据

示例输入:

示例输出:

Rectangle area:1
Circle area:3.14
Triangle area:1.5

提示:

参考答案(内存最优[1084]):

#include<stdio.h>
int main()
{
	printf("Rectangle area:1\nCircle area:3.14\nTriangle area:1.5");
}

参考答案(时间最优[0]):

#include<iostream> 
#define PI 3.14 
using namespace std; 
class Shape 
{ 
public:  
 Shape(); 
 Shape(int c); 
 int getcolor(); 
 double virtual area(); 
protected: 
 int color; 
}; 
Shape::Shape() 
{ 
 color=0; 
} 
Shape::Shape(int c) 
{ 
 color=c; 
} 
int Shape::getcolor() 
{ 
 return color; 
} 
double Shape::area() 
{ 
 return 10000; 
} 
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;  
} 

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注