OJ Shape
2015-07-21 13:57
274 查看
问题:
128 MB
Submit: 240 Solved: 205
[Submit][Status][Web
Board]
,请完成RsubC类。RsubC类中包括Rectangle类和Circle类的数据成员,新增布尔类型的数据成员sign(sign等于0时,新面积等于Rectangle+Circle,sign等于1时,新面积等于Rectangle-Circle),新定义了求面积的成员函数area()。但是小聪没有为RsubC类写构造函数和成员函数,请帮助小聪完成RsubC类。
小强写的文件头和Shape类
#include<iostream>
#define PI 3.14
using namespace std;
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 10000;
}
小聪写的Rectangle类
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;
}
小聪写的Circle类
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;
}
小聪的测试函数:
int main()
{
RsubC rc=RsubC(1,2,3,1,1);
cout<<"RsubC area:"<<rc.area()<<endl;
return 0;
}
提示:不用提交全部程序,只提交补充部分。
代码:
运行结果:
学习心得:对bool型函数的使用还不熟练,希望这次以后能够记住怎么使用。
程序修改:在新增布尔类型的数据成员sign这个位置,可以写出如下代码:
class RsubC:public Rectangle,public Circle
{
public:
.......
double area()
{
if(sign);
return Rectangle::area()-Circle::area();
else
return Rectangle::area()+Circle::area();
private:
bool sign;
};
Problem K: Shape系列-4
Time Limit: 1 Sec Memory Limit:128 MB
Submit: 240 Solved: 205
[Submit][Status][Web
Board]
Description
小聪送给小亮和小华的形状他们都很喜欢,小亮和小华非要比一下他们两个的形状,来看看小聪更爱谁,请完成RsubC类。RsubC类中包括Rectangle类和Circle类的数据成员,新增布尔类型的数据成员sign(sign等于0时,新面积等于Rectangle+Circle,sign等于1时,新面积等于Rectangle-Circle),新定义了求面积的成员函数area()。但是小聪没有为RsubC类写构造函数和成员函数,请帮助小聪完成RsubC类。
小强写的文件头和Shape类
#include<iostream>
#define PI 3.14
using namespace std;
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 10000;
}
小聪写的Rectangle类
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;
}
小聪写的Circle类
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;
}
小聪的测试函数:
int main()
{
RsubC rc=RsubC(1,2,3,1,1);
cout<<"RsubC area:"<<rc.area()<<endl;
return 0;
}
提示:不用提交全部程序,只提交补充部分。
Input
无Output
输出小聪测试的RsubC的面积。Sample Output
RsubC area:2.86
代码:
#include<iostream> #define PI 3.14 using namespace std; 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 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 RsubC:public Rectangle,public Circle { public: RsubC(int c,double w,double h,double r,int s):Rectangle(c,w,h),Circle(c,r),sign(s){} bool t() { if(sign==1) return true; else return false; } double area() { if (t()) return Rectangle::area()-Circle::area(); else return Rectangle::area()+Circle::area(); } private: int sign; }; int main() { RsubC rc=RsubC(1,2,3,1,1); cout<<"RsubC area:"<<rc.area()<<endl; return 0; }
运行结果:
学习心得:对bool型函数的使用还不熟练,希望这次以后能够记住怎么使用。
程序修改:在新增布尔类型的数据成员sign这个位置,可以写出如下代码:
class RsubC:public Rectangle,public Circle
{
public:
.......
double area()
{
if(sign);
return Rectangle::area()-Circle::area();
else
return Rectangle::area()+Circle::area();
private:
bool sign;
};
相关文章推荐
- android自定义加载框
- 16. Magento模板及布局文件中引用Static Block的方法
- 宝典——数据结构和设计模式
- java对象转为json数据在前台展示
- ios开发学习
- ios学习之 初识UINavigationController
- 【高级算法】遗传算法解决3SAT问题(C++实现)
- 前段性能优化之js,css调用优化
- Android Otto调研
- 使用jQuery 把一个JSP文件中的所有id名字替换成以b开头,再加上原来的名字(首字母变为大写)
- plsql中文显示乱码问题
- ASP.NET网页抓取数据
- 小波说雨燕 第三季 构建 swift UI 之 UI组件集-视图集(四)Alert View视图 学习笔记
- iOS设计模式之生成器
- Mac上配置adb(Android Debug Bridge)环境变量
- 微信6.2.2版本使用文本上传框出现没有应用可执行此操作的问题
- bugzilla用户无法登录
- java数据类型讲解
- 博易鑫管家(分账户系统)资管风控软件免费使用!!
- sysdate