/*********************************************************************************************************************************
模块说明:虚函数和抽象基类的应用
*********************************************************************************************************************************/
#include<iostream>
using namespace std;/*********************************************************************************************************************************
//模块说明:声明抽象基类Shape
//模块详情:【1】Shape类有三个成员函数,没有数据成员,三个成员函数都声明为虚函数,其中shapeName声明为纯虚函数,因此Shape是一个抽象基类.【2】shapeName函数的具体作用是输出具体的形状。这个信息与相应的派生类密切相关的,显然这不应是在基类中定义,而应在派生类中定义。故声明为纯虚函数【3】Shape虽然是抽象基类,但是也可以包括某些成员的定一部分,其中area()和volume()包括函数体,其返回值为0.【4】由于考虑到在Point类中不在对area()和volume()函数重新定义,因此没有把这两个函数也声明为纯虚函数。【5】在Point类中继承了Shape类的area()和volume()函数。
********************************************************************************************************************************/
class Shape
{
public:virtual float area()const{return 0.0;}               //虚函数virtual float volume()const{return 0.0;}           //虚函数virtual void shapeName()const = 0;            //纯虚函数
};
/******************************************************************************************************************
//模块说明 声明类Point   Point是Shape的公有派生类【1】Point从Shape继承了3个成员函数,由于“点”没有面积和体积的,因此不必重新定义area和volume,【2】虽然在Point中用不到这两个函数,但是Point类仍然从Shape类中继承了这两个函数,以便Point类的派生类继承它们
*******************************************************************************************************************/
class Point:public Shape
{
public:   Point(float x=0,float y=0);           //有默认参数的构造函数  void setPoint(float,float);           //设置坐标值  float getX()const{return x;}            //读X坐标,getX函数为常成员函数  float getY()const{return y;}            //读Y坐标,getY函数为常成员函数virtual void shapeName()const{cout<<"Point:";}       //对虚函数进行再定义friend ostream& operator<<(ostream&,const Point&);  //友元重载运算符<<
protected:  float x,y;
};  /******************************************************************************************************************
模块说明: 下面是定义Point类的成员函数
*******************************************************************************************************************/
Point::Point(float a,float b)                    //Point构造函数
{  x  =  a;  y  =  b;
}    void Point:: setPoint(float a,float b)          //设置x和y的坐标
{  x  =  a;  y  =  b;
}  ostream& operator<<(ostream&output,const Point&p)     //重载运算符“<<”,使之能输出点的坐标
{  output<<"["<<p.x<<","<<p.y<<"]"<<endl;  return output;
}  /*******************************************************************************************************************
模块说明: 声明派生类Circle
***********************************************************************************************************************/
class Circle:public Point
{
public:  Circle(float x=0,float y=0,float r = 0);         //构造函数  void setRadius(float);                           //设置半径  float getRadius()const;                          //读取半径  virtual float area()const;                               //计算圆的面积  virtual void shapeName()const{cout<<"Circle";}friend ostream &operator<<(ostream&,const Circle&);
protected:  float radius;  };
/*******************************************************************************************************************
模块说明: 下面是定义Circle类的成员函数
***********************************************************************************************************************/
Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}     //定义构造函数
void Circle::setRadius(float r)                                 //设置半径值
{radius = r;
}float Circle::getRadius()const{return radius;}                  //读取半径的值
float Circle::area()const{return 3.14159*radius*radius;}            //计算圆面积  ostream &operator<<(ostream&output,const Circle &c)
{  output<<"["<<c.x<<","<<c.y<<"],r="<<c.radius<<endl;  return output;
}
/***********************************************************************************************************************
//模块说明: 声明Circle的派生类Cylinder
*************************************************************************************************************************/
class Cylinder:public Circle
{
public:  Cylinder(float x=0,float y=0,float r=0,float h=0);          //构造函数  void setHeight(float);                                          //设置圆柱的高  // float getHeight()const;                                        //读取圆柱的高  virtual float area()const;  virtual float volume()const;                                   //计算圆柱体积  virtual void shapeName()const{cout<<"Cylinder:";}                //重载虚函数friend ostream& operator<<(ostream&output,const Cylinder&);    //重载运算符
protected:    float  height;
};  /*******************************************************************************************************************
模块说明: 下面是定义Cylinder类的成员函数
***********************************************************************************************************************/
Cylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h){}           //构造函数
void Cylinder::setHeight(float h){height=h;}                                            //设置圆柱的高
float Cylinder:: area()const{return 2*Circle::area()+2*3.14159*radius*height;}      //计算圆柱体的表面积
float Cylinder::volume()const{return Circle::area()*height;}                            //计算圆柱体积
ostream& operator<<(ostream&output,const Cylinder&cy)                                 //重载运算符
{  output<<"["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height<<endl;  return output;
}
/********************************************************************************************************************************
模块说明:主函数
******************************************************************************************************************************** */
int main()
{  Point point(3.2,4.5);Circle circle(2.4,1.2,5.6);Cylinder cylinder(3.5,6.4,5.2,10.5);  point.shapeName();cout<<point<<endl;circle.shapeName();cout<<circle<<endl;cylinder.shapeName();cout<<cylinder<<endl<<endl;Shape *pt;                 //【!】【!】定义基类指针pt = &point;pt->shapeName();cout<<"x="<<point.getX()<<",y="<<point.getY()<<"\narea="<<pt->area()<<"\nvolume="<<pt->volume()<<"\n\n";pt = &circle;pt->shapeName();cout<<"x="<<circle.getX()<<",y="<<circle.getY()<<"\narea="<<pt->area()<<"\nvolume="<<pt->volume()<<"\n\n";pt = &cylinder;pt->shapeName();cout<<"x="<<cylinder.getX()<<",y="<<cylinder.getY()<<"\narea="<<pt->area()<<"\nvolume="<<pt->volume()<<"\n\n";system("pause");  return 0;
}  

c++------------之---【虚函数和抽象基类的应用】相关推荐

  1. python虚函数_Python进阶话题杂谈(十三)纯虚函数与抽象基类

    纯虚函数与抽象类都是Python面向对象中重要的编程范式,用于对继承类做强制性接口实现约定. abc模块包含了一系列与抽象基类与纯虚函数相关的方法.Python中通过修改元类进行抽象基类的设定.这里P ...

  2. [C++] - 纯虚函数 抽象基类 接口类

    翻译自:https://www.learncpp.com/cpp-tutorial/126-pure-virtual-functions-abstract-base-classes-and-inter ...

  3. C++ day24 继承(四)抽象基类,纯虚函数,protected

    文章目录 关键字protected(带来方便同时带来危险,最好不用) 抽象基类和纯虚函数(is-a关系用公有继承实现有时候也不太合适) 用圆和椭圆的笨拙派生为例,挑拨is-a和公有继承的搭档关系 替代 ...

  4. C++中为什么要引入抽象基类和纯虚函数?

    为什么要引入抽象基类和纯虚函数? 主要目的是为了实现一种接口的效果. 抽象类是一种特殊的类,它是为了抽象和设计的目的为建立的,它处于继承层次结构的较上层. ⑴抽象类的定义:带有纯虚函数的类为抽象类. ...

  5. C++抽象基类和纯虚成员函数

    公共接口是指一系列成员函数的集合,支持该接口的类必须以合适的方式重新定义这些成员函数,否则就无法创建对象.在C++中,可以通过抽象基类来实现公共接口. 纯虚函数 纯虚成员函数的声明语法如下: virt ...

  6. C++引入抽象基类和纯虚函数的作用和目的

    为什么要引入抽象基类和纯虚函数? 主要目的是为了实现一种接口的效果. 原文链接:https://blog.csdn.net/weibo1230123/article/details/82014322 ...

  7. c++ 纯虚成员函数+抽象基类

    公共接口是指一系列成员函数的集合,支持该接口的类必须以合适的方式重新定义这些成员函数,否则就无法创建对象.C++ 中可以通过抽象基类来实现公共接口,为了介绍抽象基类,我们需要先来了解一下纯虚成员函数. ...

  8. C++之抽象基类与纯虚函数

    // Vitual_Base.cpp : Defines the entry point for the console application. //抽象基类与纯虚函数#include " ...

  9. C++之继承探究(十):抽象基类与纯虚函数

    前文:C++之继承探究(九):多态的代价 抽象基类与纯虚函数   ♠\spadesuit♠ 纯虚函数:虚函数只有声明,函数体=0,就是一个纯虚函数,纯虚函数没有函数体,不需要实现.在子类里实现纯虚函数 ...

最新文章

  1. linux卸载mpfr,Red Hat Linux在安装gcc时遇到的问题汇总
  2. Buck开关电源拓扑结构分析
  3. WOLF ISP CCIE 方向优惠最后10天,价格低至13800!!
  4. 用C语言实现linux的ping,用C语言实现Ping程序功能
  5. for循环中取出最大最小 累加_从零开始学Python - 第006课:循环结构
  6. 中国代工厂的困惑:把大牌t恤卖到99块3件,还会有人买吗?
  7. 搭建hexo博客并部署到github上
  8. 苹果首款自研芯片Mac成本可能上升 因设计改变
  9. Windows域控设置客户端禁用运行/cmd命令行【全域策略生效】
  10. 求职干货:再也不怕面试官问斐波那契数列了!
  11. 蓝桥杯 ADV-79 算法提高 时间转换
  12. 机器学习基础(五十九)—— 高级优化算法(梯度下降、L-BFGS、共轭梯度)
  13. 【RobotStudio学习笔记】(七)工件坐标
  14. 《深入剖析Android系统》第9章RIL补充配图
  15. Hibernate 简单的CURD操作
  16. 【知识图谱系列】基于2D卷积的知识图谱嵌入
  17. JVisualVM的使用实录
  18. matlab实现参数方程求导(paradiff函数)
  19. UCOS操作系统——中断和时间管理(七)
  20. 如何更改python界面颜色_pycharm修改界面主题颜色的方法 pycharm怎么恢复默认设置...

热门文章

  1. mysql重复数据处理_MySQL 处理重复数据
  2. 模拟usb拔出插入_拔U盘的时候到底要不要点“安全删除USB”?微软官方给了答案...
  3. Spring AOP之注解配置篇
  4. 一键生成安卓证书_【带壳截图+电影台词 生成器】
  5. django分页的东西, 不详细, 但是也足够了。
  6. Network-Monitor项目中观察者模式解析
  7. Spring Boot-热部署和Debugger使用(三)
  8. python 数据结构与算法
  9. 嵌入式软件设计第九次实验报告-140201235-陈宇
  10. Java的Reflection机制