c++面向对象高级编程 学习六 虚函数

虚函数:在成员函数前面加上virtual,函数就变成了虚函数

继承函数:子类可以调用父类的函数,叫做继承了函数,即函数的调用权

三种函数:
non-virtual 函数: 你不希望 derived class 重新定义 (override, 重写) 它.
virtual 函数: 你希望 derived class 重新定义 (override, 重写) 它 , 且你对它已有默认定义 。
pure virtual 函数: 你希望 derived class 一定要重新定义 (override 重写)它 , 你对它没有默认定义 。
纯虚函数:const=0

objectID()函数是non-virtual 函数,它可以生成ID号,不希望子类去重写,父类的定义已足够
error()函数,如果操作失败,比如可以打印一串错误的标识,其设置为虚函数,是因为若子函数有不同的想法不同的打印信息,可进行重写。重新定义后,子类调用时即是调用自己重写的函数。
draw()函数是纯虚函数,必须让子类重写定义。因为随着类的不同,draw的方式也不同,所以需要子类自己去重写。
在这里插入图片描述
通过子类的对象调用父类的函数
框架将可以写好的部分写好,将不能决定的部分写成虚函数,让子类自己去定义,子类再调用父类函数的时候,遇到自己定义的部分,会去执行自己定义的函数,如图中灰色线路径所示。

在这里插入图片描述
程序示例:
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/446618.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++ 数据结构 线性链表

#pragma once 减少头文件组合,降低编译出错的概率作用等效于 #ifndef FUNC_H #define FUNC_H代码主体#endif 线性表的定义 排队问题 简单的线性表 (物理 或者逻辑结构)1,数组2,链表线性表相关操作:1,线性表初始化2&a…

英语口语 week12 Thursday

英语文章 As the pace of life quickens with technological advancements, people are occupied by all kinds of trivial matters. They seem forever on the go. There’s no difficulty in imagining that the hustle and bustle of everyday life can make us lose focus…

H.264/AVC视频编解码技术详解 第一章 视频信息与压缩编码

H.264/AVC视频编解码技术详解系列笔记 是对 H.264/AVC视频编解码技术详解 课程的学习 文章目录人与世界的交互视频信号的表示方法视频压缩编码视频信息为什么可以被压缩?视频压缩编码的分类:视频压缩编码的基本技术人与世界的交互 从远古时代开始&#…

英语口语 week13 Monday

英语文章 Competitions between businesses can be very aggressive in contemporary society. However, the competition for talented personnel is thought to be the key to business competition. Wise employers consider their employees as the company’s core asset…

文件系统的由来

启蒙篇 文件的由来 磁盘上保存的是一对十六进制的数据,如何切分数据形成不同的文件,也就是如何确定一个文件的起始和终止位置?将相关数据打包在一起形成一个文件,比如从什么位置开始到什么位置结束,是一张图片、一段…

英语口语 week13 Wednesday

英语文章 Despite his extraordinary success in writing fairy tales,Hans Christian Andersen preferred to living in a way of simplicity and frugality. He often wore an old hat when he went out. One day, a well-dressed man stopped Andersen on the street, inte…

操作系统 IO管理

学什么? I/O input / output 输入:鼠标 键盘 手柄 触摸屏 摄像头 MTC 扫描仪输出:显示器 打印机 耳机 音响 既是输入也是输出:光驱 网卡 磁盘 U盘硬件:设备如何把数据返回到PC机,但是不同种类的设…

c++面向对象高级编程 学习九 pointer-like classes

c的class设计出来有两种形式,一种像指针,一种像函数 智能指针里包含普通指针,要写 * 和 -> 的函数 sp->method(); //sp-> 经 T* operator*() const 函数,得到px //由于 箭头符号(->)作用下去…

const int *a和int*const a 的区别详解

补充知识 “const int i”与“int const i”之间的区别对变量来说,const 关键字可以限定一个变量的值不允许改变,从而保护被修饰的东西,防止意外修改,在一定程度上可以提高程序的安全性和可靠性。 代码 const int * int i1 10…

c++面向对象高级编程 学习十 function-like classes

本节是设计一个class,使它的行为像一个函数。 如果一个东西能接受小括号()操作符,那么这个东西就称之为函数,或像函数的东西。 下图为三个函数对()的重载,这三个类均为像函数的类,它们可接受()操作符, 标…

英语口语 Week14 Monday

英语文章 Thailand, a country in Southeast Asia with an area of about 514,000 square kilometers, has been increasingly prosperous in its tourism industry in the past few decades. Its capital is Bangkok and its major languages are Thai, Chinese and English.…

c++面向对象高级编程 学习十一 类模板、函数模板、成员模板

namespace经验谈&#xff1a; 团队中函数或类的名字可能会冲突&#xff0c;因此使用namespace进行区分。 类模板&#xff1a; template<typename T> 函数模板&#xff1a; template<class T>&#xff0c;此处class可改成typename 函数模板在使用的时候&#xff0…

操作系统面试 总结

以下文章来源于程序员cxuan &#xff0c;作者cxuan 原文链接什么是操作系统 操作系统是管理硬件和软件的一种应用程序。操作系统是运行在计算机上最重要的一种软件&#xff0c;它管理计算机的资源和进程以及所有的硬件和软件。它为计算机硬件和软件提供了一种中间层&#xff…

英语口语week 14 Thursday

英语文章 A couple decided to go out to celebrate their wedding anniversary, so they called a babysitter. When the babysitter arrived, the two children had already been asleep. The babysitter soon got bored and went to the kitchen where she blended some wh…

c++面向对象高级编程 学习十二 模板

模板特化&#xff1a; 模板是一种泛化的形式&#xff0c;特化是将参数类型进行指定&#xff0c;写出特化的版本&#xff0c;当在调用下图cout<<hash()(1000);的时候&#xff0c;由于特化中有struct hash{ }的版本&#xff0c;因此会直接调用特化部分。 模板偏特化&…

英语口语 week14 Friday

英语文章 Shopping is taking place every second. However, the prices of the same goods may differ from store to store. A name-brand dress may cost several hundred pounds at a boutique, but only half the price in a discount store or a big chain store. Moreo…

数据结构 树

定义 树是节点的优先集合度&#xff1a;孩子的数量&#xff0c;度为0 就是终端节点&#xff0c;不为零就是根节点有序树&#xff1a;有顺序&#xff0c;不可以替换无序树&#xff1a;无顺序&#xff0c;可以替换深度 和 树的深度相反&#xff0c;第一层深度为1 树的深度为 3 二…

英语口语 Week15 TuesDay

英语文章 One day, when Bella was doing sports in the school yard, the squirrel fled out of her sleeve. Threading its way through a considerable number of people, the squirrel disappeared in the distance After a sequence of movements, it hopped onto the ar…

c++面向对象高级编程 学习十四 引用

文章目录referencereference的常见用途reference 变量有三种形式&#xff1a;值&#xff0c;指针&#xff0c;引用 int x0; //值 int* p&x;//指向整型的指针&#xff0c;地址&#xff0c;指针在之后的程序中可以指向其他变量 int& rx;//引用&#xff0c;此处表示 r代…

google浏览器 隐藏功能开启

网址 chrome://flags/ 1&#xff0c;多线程下载 2&#xff0c;暗黑模式3&#xff0c;标签缩略图4&#xff0c;PWA 渐进式web应用 网页即应用5&#xff0c;阅读模式&#xff0c;排除广告&#xff0c;点击阅读模式去除干扰chrome://net-internals6&#xff0c;解决有问题的代理IP…