第五题
来源:互联网 发布:淘宝卖什么产品利润高 编辑:程序博客网 时间:2024/05/04 02:08
#include <iostream>using namespace std;class Shape{public: virtual double area() const =0; };class Circle:public Shape{public:Circle(double r):radius(r){} virtual double area() const {return 3.14159*radius*radius;}; protected: double radius; };class Square:public Shape{public: Square(double s):side(s){} virtual double area() const {return side*side;} protected: double side;};class Rectangle:public Shape{public: Rectangle(double w,double h):width(w),height(h){} virtual double area() const {return width*height;} protected: double width,height; };class Trapezoid:public Shape{public: Trapezoid(double t,double b,double h):top(t),bottom(t),height(h){} virtual double area() const {return 0.5*(top+bottom)*height;} protected: double top,bottom,height; };class Triangle:public Shape{public: Triangle(double w,double h):width(w),height(h){} virtual double area()const {return 0.5*width*height;} protected: double width,height; };int main(){ Circle circle(10); Square square(3.5); Rectangle rectangle(4.5,8.0); Trapezoid trapezoid(2.0,4.5,3.2); Triangle triangle(4.5,8.0); Shape *pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle}; double areas=0.0; for(int i=0;i<5;i++) {areas=areas+pt[i]->area();} cout<<"totol of all areas="<<areas<<endl; cout<<"circle,square,rectangle,trapezoid,triangle"<<endl; for(int j=0;j<5;j++) { cout<<"area="<<pt[j]->area()<<endl;; } return 0;}
<img src="http://img.blog.csdn.net/20150526174002476?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbW9sdXpodWk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
0 0
- 第五题
- 第五题
- 第五题
- 第五题
- 第五题
- 第五题
- 第五题
- 第五题
- 第五题
- 第五题
- 第五题
- 第五题
- 第五题
- 第五题
- 第五题
- 第五题
- 第五题
- 第五题
- java中级-4-Collection集合类知识串讲(3)-Map及其子类
- 文件通信
- 安装VM用于安装Ubuntu,学习Linux开始
- JSONObject与JSONArray的使用
- 简单的手势操作
- 第五题
- RUP4+1架构方法
- 文件备份与恢复技术
- UITableViewCell的自定义
- 微软等公司数据结构+算法面试100题
- leetcode_wordladder
- JSF、Richfaces学习
- Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
- 黑客专用文本编辑器Emacs中文手册