OJ 继承与组合类问题之 Shape系列 5
2015-06-16 13:31
288 查看
JC和Kitty听说小亮和小华有了Rectangle和Circle并用RsubC类比较了大小,于是想借小亮和小华的Rectangle和Circle来仿制自己的形状,于是JC和Kitty完成了自己的rectangle类和circle类,他们两个也要比较一下形状大小。但是JC和Kitty没有为RsubC1类(其中的布尔类型sign等于0时,新面积等于Rectangle+Circle,sign等于1时,新面积等于Rectangle-Circle)写构造函数与area面积函数,请帮助JC和Kitty完成RsubC1类。
//小强写的文件头和各种类
#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;
}
提示:不用提交全部程序,只提交补充部分。
赶时间呢就不贴运行结果了 这个可以参照之前做过的项目存储班长信息的学生类
继承与组合用起来有点儿不熟练
另外因为输出问题导致OJ上的题老是不能通过 这个我已经真心给跪了><
一边敲代码一边气的不行...
//小强写的文件头和各种类
#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;
}
提示:不用提交全部程序,只提交补充部分。
Input
无Output
输出JC和Katy测试的RsubC类的面积。#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的测试函数: 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; }
赶时间呢就不贴运行结果了 这个可以参照之前做过的项目存储班长信息的学生类
继承与组合用起来有点儿不熟练
另外因为输出问题导致OJ上的题老是不能通过 这个我已经真心给跪了><
一边敲代码一边气的不行...
相关文章推荐
- TP里的关联查询
- 把bean的值赋给另一个bean(仅相同名称的属性)
- Hive学习笔记
- 添加鼠标右键菜单项(EditPlus为例)
- Sql Server简单加密与解密 【转】
- Linux之mailx的使用
- Theano2.1.6-基础知识之在thenao中的求导
- Linux之mailx的使用
- Linux IPC总结(全)
- 电子商务IT系统-系统框架、机器框架及人员构成
- Java NIO框架Netty课程(一) – Hello Netty
- 多字段模糊查询
- sharepoint 2010 设置pdf文件在线查看
- web打印word
- Theano2.1.6-基础知识之在thenao中的求导
- JavaScript实现的购物车效果-效果好友列表
- 向IOS模拟机上添加图片
- programming-challenges Erdös Numbers (110206) 题解
- 自定义控件之--组合控件(titlebar)
- BZOJ1185 [HNOI2007]最小矩形覆盖(旋转卡壳)