第五次实验参考代码

代码比我们实验的要求要多一些,大家仔细看下

#include <iostream.h> #include <math.h> const double PI=3.14; class Geometric_shape{ public: Geometric_shape() { } virtual double Get_Girth()=0; //Öܳ¤ virtual double Get_Area()=0; //Ãæ»ý virtual double Get_Volume()=0; //Ìå»ý virtual double Get_SurArea()=0; //±íÃæ»ý virtual void print()=0; }; class Rectangle:public Geometric_shape{ protected: double length,width; public: Rectangle(double l,double w):length(l),width(w){} double Get_Girth() { return 2 * (width + length); } double Get_Area() { return width * length; } virtual double Get_Volume() { return 0.0; } virtual double Get_SurArea() { return 0.0; } void print() { cout<<"The Rectangle's Girth is:"<<Get_Girth()<<endl; cout<<"The Rectangle's Area is:"<<Get_Area()<<endl; } }; class Circle:public Geometric_shape{ protected: double radius; public: Circle(double l):Geometric_shape(),radius(l){} double Get_Girth() { return 2 * PI * radius; } double Get_Area() { return PI * radius * radius; } virtual double Get_Volume() { return 0.0; } virtual double Get_SurArea() { return 0.0; } void print() { cout<<"The Circle's Girth is:"<<Get_Girth()<<endl; cout<<"The Circle's Area is:"<<Get_Area()<<endl; } }; class Triangle:public Geometric_shape{ protected: double len1,len2,len3; public: Triangle(double a,double b,double c) { len1 = a; len2 = b; len3 = c; } double Get_Girth() { return len1 + len2 + len3; } double Get_Area() { double p = Get_Girth() / 2; return sqrt(p * (p - len1) * (p - len2) * (p - len3)); } virtual double Get_Volume() { return 0.0; } virtual double Get_SurArea() { return 0.0; } void print() { cout<<"The Triangle's Grith is:"<<Get_Girth()<<endl; cout<<"The Triangle's Area is:"<<Get_Area()<<endl; } }; class Box:public Rectangle{ protected: double height; public: Box(double l,double w,double h):Rectangle(l,w),height(h){} double Get_SurArea() { return 2 * (length * width + width * height + height * length); } double Get_Volume() { return length * width * height; } void print() { cout<<"The Box's SurArea is:"<<Get_SurArea()<<endl; cout<<"The Box's volume is:"<<Get_Volume()<<endl; } }; class Column:public Circle{ private: double height; public: Column(double l,double h):Circle(l),height(h){} double Get_SurArea() { double a; a = 2 * Get_Area() + 2 * PI * radius * height; return a; } double Get_Volume() { return Get_Area() * height; } void print() { cout<<"The Cylinder's SurArea is:"<<Get_SurArea()<<endl; cout<<"The Cylinder's Volume is:"<<Get_Volume()<<endl; } }; class Ball:public Circle{ public: Ball(double l):Circle(l){} double Get_SurArea() { return 4.0 * Get_Area(); } double Get_Volume() { return 4.0 * Get_Area() * radius /3.0; } void print() { cout<<"The Cone's SurArea is:"<<Get_SurArea()<<endl; cout<<"The Cone's Volume is:"<<Get_Volume()<<endl; } }; void fun(Geometric_shape &p) { p.print(); } void main() { Circle c(2.5); fun(c); Rectangle r(2.4,3.6); fun(c); Ball b(3.5); fun(b); Column co(3.5,5.3); fun(co); Triangle t(2.3,4.2,3.4); fun(t); Box bo(3,5.2,3.6); fun(bo); }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值