收藏 分销(赏)

编写一个程序计算三角-正方形及圆形面积.doc

上传人:仙人****88 文档编号:7727193 上传时间:2025-01-14 格式:DOC 页数:3 大小:31.04KB 下载积分:10 金币
下载 相关 举报
编写一个程序计算三角-正方形及圆形面积.doc_第1页
第1页 / 共3页
编写一个程序计算三角-正方形及圆形面积.doc_第2页
第2页 / 共3页


点击查看更多>>
资源描述
1、编写一个程序计算三角,正方形和圆形的面积。 分析:依题意,可以抽象出一个基类CBase,在其中说明一个虚函数,用来求面积,并利用单接口、多实现版本设计各个图形求面积的方法。 #include<iostream> #include<math.h> #define PI 3.14159 using namespace std; class Shape{ public: virtual double getArea() = 0; protected: double area; }; class Triangle:public Shape{ public: Triangle(double a,double b,double c){ this->a = a; this->b = b; this->c = c; } virtual double getArea(){ double p = (a+b+c)/2; this->area = sqrt(p*(p-a)*(p-b)*(p-c)); return this->area; } private: double a; double b; double c; }; class Rectangle:public Shape{ public: Rectangle(double width,double height){ this->width = width; this->height = height; } virtual double getArea(){ this->area = this->width*this->height; return this->area; } private: double width; double height; }; class Circle:public Shape{ public: Circle(double radius){ this->radius = radius; } virtual double getArea(){ this->area = PI*radius*radius; return this->area; } private: double radius; }; int main(){ Shape *pTg = new Triangle(3.0,4.0,5.0); Shape *pRtg = new Rectangle(4.0,5.0); Shape *pCle = new Circle(5.0); cout<<"Area of Triangle is "<<pTg->getArea()<<endl; cout<<"Area of Rectangle is "<<pRtg->getArea()<<endl; cout<<"Area of Circle is "<<pCle->getArea()<<endl; system("pause"); return 0; }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服