包含一个及一个以上的纯虚函数的类就是抽象基类,抽象基类没有也不能必要定义对象。

**#include <iostream>
using namespace std;
class Shape
{
public:virtual float area() const {return 0.0;}//虚函数virtual float volume() const {return 0.0;}  //虚函数virtual void shapeName() const =0;     //声明一个纯虚构函数//说明存在纯虚构函数的基类不能够定义对象,多以只能为抽象类
};
///////////////////////////////////////////////////////////////////////////////////////////////////
class Point :public Shape
{
public:Point(float=0,float=0);      //定义构造函数void setPoint(float,float);float getX() const {return x;}float getY() const {return y;}virtual void shapeName() const{cout<<"Point";}   //对纯虚函数进行重新定义,以便实现函数的多态性friend ostream & operator<<(ostream &,const  Point &);
protected:float x,y;};
Point::Point(float a,float b)
{x=a;
y=b;
}
void Point::setPoint(float a, float b) {x=a;y=b;}
ostream & operator <<(ostream & output,const Point &p)
{output<<"["<<p.x<<","<<p.y<<"]";return output;
}
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 &);    //重定义,第二个参数是对Circle常数据成员的引用
protected:float radius;};
Circle::Circle(float x, float y, float r):Point(x,y),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;return output;
}
//声明Cylinder类
class Cylinder:public Circle
{
public:Cylinder(float x=0,float y=0,float r=0,float h=0);void setHeight(float);virtual float area() const;virtual float volume() const;virtual void shapeName() const {cout<<"Cylinde:";}    //对虚函数进行重定义friend ostream& operator<<(ostream&,const Cylinder &);protected:float height;};
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;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<<cylinder<<endl<<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;cout<<"x="<<cylinder.getX()<<",y="<<cylinder.getY()<<"\narea="<<pt->area()<<"\nvolume="<<pt->volume()<<"\n\n";return 0;
}
**

函数的多态性以及虚函数相关推荐

  1. C++之多态性与虚函数

    面向对象程序设计中的多态性是指向不同的对象发送同一个消息,不同对象对应同一消息产生不同行为.在程序中消息就是调用函数,不同的行为就是指不同的实现方法,即执行不同的函数体.也可以这样说就是实现了&quo ...

  2. C++ 的多态性与虚函数

    腾讯二面时被问到关于C++的多态性和虚函数,当时脑袋发热没回答好,其实根本原因是这是很久之前看的,而且一直没有应用上,所以理所当然说不出个所以然来.趁着国庆假期,把C++草草捡了些回来,现在主要说下多 ...

  3. c 结构体在声明时赋值_C/C++编程笔记:C++入门知识,C++多态性和虚函数解析

    本篇要学习的内容和知识结构概览 多态性 编译时的多态性称为静态联编. 当调用重载函数时, 在编译期就确定下来调用哪个函数. 运行时的多态性称为动态联编. 在运行时才能确定调用哪个函数, 由虚函数来支持 ...

  4. C++(23)--多态性与虚函数

    多态性与虚函数 1.静态多态-重载 2.动态多态-重写 2.1 向上转换/向下转换 3.虚函数的工作原理 4.纯虚函数和抽象类 5.补充项目(都市浮生记)-卒 <老九学堂C++课程>学习笔 ...

  5. C++ 多态性之虚函数抽象类纯虚函数

    一. 虚函数 1.什么是虚函数:虚函数的定义是在基类中进行的,被 virtual 修饰的,当基类中的某个成员函数被声明为"虚函数"后,可以在一个或多个派生类中重新定义该函数,重新定 ...

  6. C++面试题-面向对象-多态性与虚函数

    C++面试题-面向对象-多态性与虚函数 问:在C++程序中调用被C编译器编译后的函数,为什么要加extern "C"? 答:C++语言支持函数重载,C语言不支持函数重载.函数被C+ ...

  7. 头歌C++面向对象 - 类的多态性与虚函数

    C++ 面向对象 - 类的多态性与虚函数 一.实训目的 1.掌握虚函数的基本使用. 2.掌握虚析构函数的使用. 3.掌握纯虚函数和抽象类的使用. 二.实训内容 1.人与复读机 设计人类.英语学生类和复 ...

  8. C++ 面向对象 - 类的多态性与虚函数

    任务描述 本关任务:设计人类.英语学生类和复读机类三个类. 相关知识 为了完成完成本关任务,你需要掌握虚函数的基本使用. 多态性 在面向对象的方法中,多态性是指向不同对象发送同一个消息,不同对象在接收 ...

  9. 任务三 类的多态性与虚函数

    文章目录 第1关:人与复读机 相关知识 多态性 虚函数 重写父类虚函数 编程要求 人与复读机 第2关:复读机的毁灭 相关知识 多态性的体现 虚析构函数 编程要求 复读机的毁灭 第3关:计算图像面积 相 ...

最新文章

  1. 不要在脱离这个市场的情况下讨论商业模式
  2. SEO中HTML标签权重
  3. 数据结构与算法 | 循环队列
  4. 【数位DP】CF 54C,509C,431D,628D,855E,1245F,95D
  5. 前端学习(1950)vue之电商管理系统电商系统之渲染添加父类的对话框
  6. 【 iview 实践指南】之如何优雅地在Table中嵌套Input(代码篇)
  7. php分区表,【MYSQL】分区表
  8. html文件用safari打开方式,在html中打开(在Safari中)
  9. 折线图设置圆点_Seaborn可视化 折线图seaborn.lineplot
  10. Mac如何设置文本中的单双引号样式?
  11. SpringMvc 文件上传注意事项
  12. 万字长文丨大白话带你由浅入深Python编程语言
  13. 中国人口最多的姓氏排行
  14. 向日葵服务器怎么修改密码,向日葵远程服务器ip
  15. Android appWidget——每日一句应用开发
  16. maximo-API下载连接
  17. 重新编译Spark2.4.0 Parcels包
  18. 一篇文章带你解决 MongoDB 连接 localhost 和 127.0.0.1 可以连接,但是改成具体的IP地址就无法连接
  19. 4PS-4CS--4RS
  20. 8421BCD码 5421BCD码 余三码 格雷码 余三循环码之间的关系,转换以及简易方法

热门文章

  1. hdu 4983(欧拉函数)
  2. hdu-1104-Remainder(BFS打印路径+数论)(%与mod的区别)
  3. hdu 1052 Tian Ji -- The Horse Racing
  4. Exception Handling Best Practices in .NET
  5. Leetcode-937-Reorder Log Files-(Easy)
  6. 各常用分类算法的优缺点总结:DT/ANN/KNN/SVM/GA/Bayes/Adaboosting/Rocchio
  7. 线上日志集中化可视化管理:ELK
  8. 如何将freemarker文件转化为html文件
  9. C#.NET 大型企业信息化系统 - 防黑客攻击 - SSO系统加固优化经验分享
  10. struts2.1.8,hibernate3.3.2,spring2.5 整合需要哪些jar包