C++习题 抽象基类
来源:互联网 发布:淘宝客采集软件怎么用 编辑:程序博客网 时间:2024/04/29 23:38
Problem G: C++习题 抽象基类
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 101 Solved: 81
[Submit][Status][Web Board]
Description
编写一个程序,声明抽象基类Shape,由它派生出3个派生类: Circle(圆形)、Rectangle(矩形)、Triangle(三角形),用一个函数printArea分别输出以上三者的面积(结果保留两位小数),3个图形的数据在定义对象时给定。
Input
圆的半径
矩形的边长
三角形的底与高
Output
圆的面积
矩形的面积
三角形的面积
Sample Input
12.64.5 8.44.5 8.4
Sample Output
area of circle = 498.76area of rectangle = 37.80area of triangle = 18.90
HINT
主函数已给定如下,提交时不需要包含,会自动添加到程序尾部
/* C++代码 */
int main()
{
float r,a,b,w,h;
cout<<fixed<<setprecision(2);
cin>>r;
Circle circle(r);
cout<<"area of circle = ";
printArea(circle);
cin>>a>>b;
Rectangle rectangle(a,b);
cout<<"area of rectangle = ";
printArea(rectangle);
cin>>w>>h;
Triangle triangle(w,h);
cout<<"area of triangle = ";
printArea(triangle);
return 0;
}
#include<iostream>#include<cstdio>using namespace std;class Shape{public: virtual float printArea() const=0;};class Circle:public Shape{public: Circle(float r):radius(r) {} virtual float printArea()const { return 3.141592*radius*radius; }private: float radius;};class Rectangle:public Shape{public: Rectangle(float l,float w):length(l),width(w) {} virtual float printArea()const { return length*width; }private: float length; float width;};class Triangle:public Shape{public: Triangle(float h,float w):hight(h),width(w) {} virtual float printArea()const { return 0.5*hight*width; }private: float hight; float width;};void printArea(Circle&t){ cout<<t.printArea()<<endl;}void printArea(Rectangle&t){ cout<<t.printArea()<<endl;}void printArea(Triangle&t){ cout<<t.printArea()<<endl;}
0 0
- YTU-OJ-Problem C: C++习题 抽象基类
- C++习题 抽象基类
- c++习题1_G_抽象基类
- [YTU]_2477( C++习题 抽象基类)
- 抽象类和接口习题
- 习题--抽象类、继承、接口综合
- Javase—抽象类与接口习题
- 抽象类(C++)
- [c++]抽象类
- c+-----------------------------抽象类
- C#_抽象类和抽象方法
- 一道抽象向量组习题
- C#:抽象类的学习
- 数据抽象----类(c++)
- C#----接口与抽象类
- C#---抽象类与接口
- C#:超基础 抽象类
- 【C#】抽象类和接口
- 英文环境的Ubuntu14.04安装sogou拼音输入法
- 括号匹配(栈练习)
- 点阵字库和矢量字库
- jmeter beanshell使用说明
- ACM--类模板 比较大小
- C++习题 抽象基类
- String xml 解析
- 16 oj 输出日期时间 友元类
- appledoc相关内容汇总
- Pow(x, n)
- C/C++ Lib库文件nm调试之符号表
- 浅谈android的selector背景选择器
- 判断当前table中的tr是不是最后一行
- 用chnroutes来节省vpn流量