继承,虚函数应用,对象向量
来源:互联网 发布:iphone电视直播软件 编辑:程序博客网 时间:2024/06/10 22:08
#include#include #include #include #include #define PI 3.1416using namespace std;class Shape//定义抽象基类{public: Shape() {}//构造函数 virtual double GetArea() const =0;//纯虚函数 virtual ~Shape() {}//虚析构函数};double TotalArea(const vector & p)//求面积和{ double total=0; for(int i=0;i GetArea(); return total;}class Circle : public Shape//圆继承基类shape{private: double radius;//半径public: Circle(double r) : radius(r) {}//构造函数,初始化半径 virtual double GetArea() const//返回圆的面积 { return radius * radius * PI; }};class Rectangle : public Shape//长方形继承基类shape{private: double height;//定义长 double width;//定义宽public: Rectangle(double h, double w) : height(h), width(w) {}//构造函数,初始化长和宽 virtual double GetArea() const//返回长方形的面积 { return height * width; }};class Triangle : public Shape//三角形继承基类shape{private: double a, b, c;//定义边长 double p;//海伦公式public: Triangle(double A, double B, double C) : a(A), b(B), c(C)//构造函数,初始化边长 { p = (a + b + c) / 2; } virtual double GetArea() const//返回三角形的面积 { return sqrt(p * (p - a) * (p - b) * (p - c)); }};int main(){ string order;//预处理命令 vector p;//抽象基类指针 double radius, height, width, a, b, c;//定义基本变量 while (cin >> order) { char Ord;//实际命令 if (order == "circle") Ord = 'c'; if (order == "rectangle") Ord = 'r'; if (order == "triangle") Ord = 't'; switch (Ord) { case 'c': cin >> radius; p.push_back(new Circle(radius)); break; case 'r': cin >> height >> width; p.push_back(new Rectangle(height, width)); break; case 't': cin >> a >> b >> c; p.push_back(new Triangle(a, b, c)); break; default: break; } } cout.precision(4); cout << fixed << TotalArea(p) << endl; for(int i=0;i
0 0
- 继承,虚函数应用,对象向量
- 虚函数表以及单继承多继承对象模型
- 12-面向对象(继承-子父类中成员函数特点-覆盖) 13-面向对象(继承-子父类中成员函数特点-覆盖的应用
- [C++对象模型]虚继承与虚函数表
- [C++对象模型][9]虚继承与虚函数表
- [C++对象模型][9]虚继承与虚函数表
- C++虚函数、虚继承、对象内存模型
- [C++对象模型][9]虚继承与虚函数表
- C++对象模型 多重继承与虚函数表
- C++对象模型 多重继承与虚函数表
- [C++对象模型][7]单继承与虚函数表
- [C++对象模型][8]多重继承与虚函数表
- C++对象模型 多重继承与虚函数表
- [C++对象模型][8]多重继承与虚函数表
- [C++对象模型][7]单继承与虚函数表
- [C++对象模型][8]多重继承与虚函数表
- Android SO逆向-对象的继承和虚函数
- 菱形虚拟继承&虚函数表&对象模型
- 版本控制Revision control
- ArcGIS API For Javascript之服务端配置与应用
- oracle--处理数据
- JPA映射持久化对象(Entity)
- 面向对象-final关键字
- 继承,虚函数应用,对象向量
- Hadoop 学习研究(二): 压缩实现与详解
- linux内核的整体结构
- slf4j+log4j2的整合配置
- Android APK程序的smali动态调试
- 确定可回收内存
- unity shader(待更新)
- truffle-init-webpack 项目
- SQLServer 2008以上误操作数据库恢复方法——日志尾部备份