2919: Shape系列-5
时间限制: 1 Sec 内存限制: 128 MB提交: 251 解决: 199
题目描述
//小强写的文件头和各种类
#include<iostream>
using namespace std;
#define pi 3.14
class Shape
{
public:
Shape();
Shape(int c);
int getcolor();
double area();
protected:
int color;
};
Shape::Shape()
{
color=0;
}
Shape::Shape(int c):color(c){}
int Shape::getcolor()
{
return color;
}
double Shape::area()
{
return 1000;
}
class Rectangle:public Shape
{
public:
Rectangle();
Rectangle(int c, double w,double h);
double getwidth();
double getheight();
double area();
double price();
protected:
double width,height;
};
Rectangle::Rectangle()
{
width=10;height=5;
}
Rectangle::Rectangle(int c, double w,double h):Shape(c)
{
width=w;
height=h;
}
double Rectangle::getwidth()
{
return width;
}
double Rectangle::area()
{
return width*height;
}
double Rectangle::getheight()
{
return height;
}
double Rectangle::price()
{
return color*width*height;
}
class Circle:public Shape
{
public:
Circle();
Circle(int c,double r);
double getradius()
{
return radius;
}
double area();
protected:
double radius;
};
Circle::Circle()
{
radius=10;
}
Circle::Circle(int c,double r):Shape(c)
{
radius=r;
}
double Circle::area()
{
return radius*radius*pi;
}
// RsubC1类
class RsubC1:public Shape
{
public:
RsubC1(int c,double w,double h,double r,bool s);
double area();
private:
Rectangle rectangle;
Circle circle;
bool sign;
};
//JC和Katy的测试函数:
int main()
{
RsubC1 rc1=RsubC1(3,2,3,1,1);
RsubC1 rc2=RsubC1(1,2,1,2,0);
cout<<"rc1 area="<<rc1.area()<<endl;
cout<<"rc2 area="<<rc2.area()<<endl;
return 0;
}
输入
无
输出
输出JC和Katy测试的RsubC类的面积。
样例输出
rc1 area=2.86
rc2 area=14.56
im0qianqian_站在回忆的河边看着摇晃的渡船终年无声地摆渡,它们就这样安静地画下黄昏画下清晨......
#include<iostream> using namespace std; #define pi 3.14 class Shape { public: Shape(); Shape(int c); int getcolor(); double area(); protected: int color; }; Shape::Shape() { color=0; } Shape::Shape(int c):color(c) {} int Shape::getcolor() { return color; } double Shape::area() { return 1000; } class Rectangle:public Shape { public: Rectangle(); Rectangle(int c, double w,double h); double getwidth(); double getheight(); double area(); double price(); protected: double width,height; }; Rectangle::Rectangle() { width=10; height=5; } Rectangle::Rectangle(int c, double w,double h):Shape(c) { width=w; height=h; } double Rectangle::getwidth() { return width; } double Rectangle::area() { return width*height; } double Rectangle::getheight() { return height; } double Rectangle::price() { return color*width*height; } class Circle:public Shape { public: Circle(); Circle(int c,double r); double getradius() { return radius; } double area(); protected: double radius; }; Circle::Circle() { radius=10; } Circle::Circle(int c,double r):Shape(c) { radius=r; } double Circle::area() { return radius*radius*pi; } class RsubC1:public Shape { public: RsubC1(int c,double w,double h,double r,bool s); double area(); private: Rectangle rectangle; Circle circle; bool sign; }; RsubC1::RsubC1(int c,double w,double h,double r,bool s):Shape(c),rectangle(c,w,h),circle(c,r),sign(s) {} double RsubC1::area() { if(sign==0) return rectangle.area()+circle.area(); else return rectangle.area()-circle.area(); } int main() { RsubC1 rc1=RsubC1(3,2,3,1,1); RsubC1 rc2=RsubC1(1,2,1,2,0); cout<<"rc1 area="<<rc1.area()<<endl; cout<<"rc2 area="<<rc2.area()<<endl; return 0; }