习题 12.5 编写一个程序,定义抽象基类Shape,由它派生出5个派生类:Cricle,Square, Rectangle, Trapezoid ,Triangle。用虚函数分别计算图形面积,并求所有图形面积的和.要求用基类指针数组,使它的每一个元素指向一个派生类对象。

代码:

#include<iostream>
#include<iomanip>
using namespace std;class Shape
{
public:Shape() {}virtual ~ Shape() {}virtual void  printArea() const = 0;virtual double getArea() const = 0;
};class Circle : public Shape    //Circle
{
public:Circle(){}Circle(double r):radius(r){}virtual void  printArea() const;virtual double getArea() const{return 3.14159 * radius * radius;}private:double radius;
};void  Circle::printArea() const
{cout << "Circle's area : " << setiosflags(ios::fixed) << setprecision(3) << (3.14159 * radius * radius)<< endl;
}class Rectangle : public Shape    //Rectangle
{
public:Rectangle() {}Rectangle(double h,double w) :height(h) , width(w) {}virtual void  printArea() const;virtual double getArea() const{return height * width;}private:double height;double width;
};void  Rectangle::printArea() const
{cout << "Rectangle's area : " << setiosflags(ios::fixed) << setprecision(3) << height * width << endl;
}class Triangle : public Shape       //Triangle
{
public:Triangle() {}Triangle(double h,double b) :height(h),bottom(b) {}virtual void  printArea() const;virtual double getArea() const{return  height * bottom;}private:double height;double bottom;
};void Triangle::printArea() const
{cout << "Triangle's area : " << setiosflags(ios::fixed) << setprecision(3) << height * bottom << endl;
}class Trapezoid : public Shape     //Trapezoid
{
public:Trapezoid() {}Trapezoid(double t,double b,double h) :top(t),bottom(b),height(h) {}virtual void  printArea() const;virtual double getArea() const{return  (top + bottom) * height / 2;}private:double top;double bottom;double height;
};void  Trapezoid::printArea() const
{cout << "Trapezoid's area : " << setiosflags(ios::fixed) << setprecision(3) << (top + bottom) * height /2 << endl;
}class Square : public Shape       //Square
{
public:Square() {}Square(double s) :s_length(s){}virtual void  printArea() const;virtual double getArea() const{return  s_length * s_length;}private:double s_length;
};void Square::printArea() const
{cout << "Square 's area : " << setiosflags(ios::fixed) << setprecision(3) << s_length * s_length << endl;
}int main()
{Circle c(2);Rectangle r(2, 3);Triangle tri(1, 4);Trapezoid tra(1, 2, 3);Square s(5);Shape* p[5] = { &c,&r,&tri,&tra,&s };int i;double area;for (i = 0; i != 5; ++i){p[i]->printArea();}for (i = 0,area = 0; i != 5; ++i){area += p[i]->getArea();}cout << "总面积为:" << area << endl;return 0;
}

运行结果:

习题 12.5 编写一个程序,定义抽象基类Shape,由它派生出5个派生类:Cricle,Square, Rectangle, Trapezoid ,Triangle。用虚函数分别计算图形面积。相关推荐

  1. c++程序设计十二章第5题5 写一个程序,定义抽象基类shape由它派生出5个派生类:Circle(圆形),Square(正方形),Rectangle(矩形),Trapezoid(梯形),Tr

    写一个程序,定义抽象基类shape由它派生出5个派生类:Circle(圆形),Square(正方形),Rectangle(矩形),Trapezoid(梯形),Triangle(三角形).用虚函数分别计 ...

  2. 编写一个程序,定义抽象基类Shape,由它派生出5个派生类:Circle(圆形)、Square(正方形)、Rectangle(矩形)、Trapezoid(梯形)、Triangle(三角形)。用虚函数分

    编写一个程序,定义抽象基类Shape,由它派生出5个派生类:Circle(圆形).Square(正方形).Rectangle(矩形).Trapezoid(梯形).Triangle(三角形).用虚函数分 ...

  3. python求鸡兔同笼 鸡兔总数鸡兔腿_编写一个程序解鸡兔同笼问题:已知鸡兔总数为a,鸡兔腿总数为b,计算鸡兔各有多少只?...

    [单选题]42. Wie alt ist deine Schwester? Ist sie noch klein? - Ja, sie ist acht.(1998) [单选题]46. Er beha ...

  4. 【例12 编写一个程序,为一个给定的年份找出其对应的中国生肖】

    例12:编写一个程序,为一个给定的年份找出其对应的中国生肖. package day2; import java.util.Scanner; /*** 编写一个程序,为一个给定的年份找出其对应的中国生 ...

  5. 编写一个程序,实现将c语言源程序中的注释全部删除

    <程序设计基础-c语言>杨莉 刘鸿翔 ISBN-978-7-03-032903-5 p257 习题8 6.编写一个程序,实现将c语言源程序中的注释全部删除 #include<stdl ...

  6. Java黑皮书课后题第7章:*7.29(游戏:挑选四张牌)编写一个程序,从一副52张牌中选出4张,然后计算它们的和。Ace King Quee Jack分别表示1、13、12和11,显示得到和24的次

    *7.29(游戏:挑选四张牌)编写一个程序,从一副52张牌中选出4张,然后计算它们的和.Ace King Quee Jack分别表示1.13.12和11,显示得到和为24的选牌次数 题目 题目描述 破 ...

  7. 课后习题5.13 编写一程序,将两个字符串连接起来,结果取代第一个字符串。 (1)用字符数组,不用stract函数(即自己写一个具有stract函数功能的函数); (2)用标准库中的stract函数;

    课后习题5.13 编写一程序,将两个字符串连接起来,结果取代第一个字符串. (1)用字符数组,不用stract函数(即自己写一个具有stract函数功能的函数): (2)用标准库中的stract函数: ...

  8. Java黑皮书课后题第3章:3.12(回文数字)编写一个程序,提示用户输入一个三位的整数,然后确定它是否是回文数

    3.12(回文数字)编写一个程序,提示用户输入一个三位的整数,然后确定它是否是回文数 题目 题目描述 示例 破题 代码 题目 题目描述 3.12(回文数字)编写一个程序,提示用户输入一个三位的整数,然 ...

  9. Java黑皮书课后题第1章:1.12(以千米计的平均速度)假设一个跑步者1小时40分35秒跑了24英里。编写一个程序显示以每小时为多少千米为单位的平均速度值(1英里等于1.6千米)

    Java黑皮书课后题第1章:1.12(以千米计的平均速度) 题目 题目描述 破题 代码块 修改日志 题目 题目描述 1.12(以千米计的平均速度)假设一个跑步者1小时40分35秒跑了24英里.编写一个 ...

最新文章

  1. Linux下cp命令 -f无效的解决方法
  2. 使用cookie登录时记录登录名
  3. MVC判断用是否登录了平台
  4. Report Builder 3.0连接ORACEL数据库
  5. Android——DDMS简单介绍
  6. PHP小程序码扫码登录网站,WeAuth微信小程序实现PC网站扫码授权登录
  7. 一文把三个经典求和问题吃的透透滴。
  8. Linux高性能集群搭建(2)---NFS共享文件系统安装配置
  9. linux下安装oracle instant client,linux下安装Oracle instant client
  10. 关于java就业的简历模板,工作一年java简历模板
  11. SQL Server 游标
  12. 华为浏览器工具箱 html修改,华为手机修改浏览器模式为电脑浏览模式的方法
  13. 计算机单位大小知识,计算机存储设备及存储容量单位Byte,KB,MB,GB,TB的介绍
  14. Preferences DataStore------JAVA
  15. mysql连接查询和in的效率取舍
  16. MySQL中三种表关系的建立
  17. 创新实验室实习生每周工作总结【实习第四周】
  18. Android基础之十八显示gif动画,动态图片
  19. OC5228 100V多功能LED恒流驱动器-高辉调光 65536:1 调光比
  20. 树和二叉树知识点思维导图

热门文章

  1. 独孤九剑第五式-朴素贝叶斯模型
  2. 域名 IP 对应关系
  3. mac系统修改锁屏快捷键
  4. 慢特征分析(SFA)
  5. MySQL常见的性能优化
  6. IDEA 恢复出厂设置
  7. 推荐两种开发者独立博客域名起名方式
  8. python 弹窗 模拟熊猫烧香病毒 适合老师开学特别的自我介绍
  9. 精通Git(三)——Git分支机制
  10. 探秘VOOPOO创新基因 解码跨越式发展