Ordinarily, if we do not use a function, we do not need to supply a definition of the function.

However, we must define every virtual function, regardless of whether it is used, bacuase compiler has no way to determine whether a virtual function is used.

    Quote base("0-201-1", );
print_total(cout, base, ); // call Quote::net_print Bulk_quote derived("0-201-2", , , 0.19);
print_total(cout, derived, ); // call Bulk_quote::net_print base = derived;   // copy the Quote part of derived into base
base.net_print(); // call Quote::net_print

When we call a virtual function on an expression that has a plain type - nonreference and nonpointer -- type, that call is bound at compile time. Both base and derived are plain - nonreference and nonpointer - type

Virtual are resolved at run time only if the call is called through a reference or pointer.

When a derived class override a virtual function, it may, but is not requied to, repeat the virtual keyword. Once a function is declared as virtual, remaining virtual in all the derived classes implicitly.

Virtual functions that have default arguments should use the same value in the base and derived classes.

In some cases, we want to force to call a particular version of the virtual.

    double discounted = baseP->Quote::net_price();

Call the Quote version net_price regardless of the type of object to which baseP actually points. This call can be resolved at compile time.

We can specify a virtual function as a pure virtual function by writing =0 in the place of the function body. The =0 appear only on the declaration of the virtual function in the class body.

A class containing( or inherit without overriding) a pure virtual function is known as an abstract class. We cannot (directly) create a object of a type that is an abstract class.

A abstract class declares the interface of subsequent class to override.

class Disc_quote : public Quote{
public:
Disc_quote() = default;
Disc_quote(const string & bookNo, double price, size_t qty, double disc) : Quote(bookNo, price), quantity(qty, discount(disc) { }
double net_price(size_t) const = ; // an pure virtual function
protected:
size_t quantity = ;
double discount = 0.0;
} Disc_quote discount; // error: define an Disc_quote object

Now we can reimplement Bulk_quote to inherit from Disc_quote rather than inherit directly from Quote:

class Bulk_quote: public Disc_quote{
public:
Bulk_quote() = default;
Bulk_quote(const string& bookNo, double price, size_t qty, double dis): Disc_quote(bookNo, price, qty, dis){ }
double net_price(size_t) const override;
};

Reference:

C++ Primer, Fifth Edition, chapter 15 Object-Oriented Programming

[C++] OOP - Virtual Functions and Abstract Base Classes的更多相关文章

  1. python 用abc模块构建抽象基类Abstract Base Classes

    见代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/08/01 16:58 from abc import ABCMet ...

  2. virtual base classes

    virtual base classes用来实现菱形继承解决多个重复subobject的问题 //: C09:VirtualBase.cpp // Shows a shared subobject v ...

  3. 纯虚函数(pure virtual function )和抽象类(abstract base class)

    函数体=0的虚函数称为“纯虚函数”.包含纯虚函数的类称为“抽象类” #include <string> class Animal // This Animal is an abstract ...

  4. Virtual functions in derived classes

    In C++, once a member function is declared as a virtual function in a base class, it becomes virtual ...

  5. Standard C++ Programming: Virtual Functions and Inlining

    原文链接:http://www.drdobbs.com/cpp/standard-c-programming-virtual-functions/184403747 By Josée Lajoie a ...

  6. [CareerCup] 13.3 Virtual Functions 虚函数

    13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...

  7. [译]GotW #5:Overriding Virtual Functions

       虚函数是一个很基本的特性,但是它们偶尔会隐藏在很微妙的地方,然后等着你.如果你能回答下面的问题,那么你已经完全了解了它,你不太能浪费太多时间去调试类似下面的问题. Problem JG Ques ...

  8. virtual方法和abstract方法

    在C#的学习中,容易混淆virtual方法和abstract方法的使用,现在来讨论一下二者的区别.二者都牵涉到在派生类中与override的配合使用. 一.Virtual方法(虚方法) virtual ...

  9. C#面向对象基础:virtual方法,abstract方法,区别

    virtual 关键字用于修饰方法.属性.索引器或事件声明,并使它们可以在派生类中被重写.默认情况下,类中的方法是非虚的,非虚的方法不能在子类中被覆盖(override),但是可以隐藏(new),但这 ...

随机推荐

  1. 解决vue变量未渲染前代码显示问题

    在网络加载缓慢或者刷新的时候总会有那么一瞬间出现vue的模板代码,实在很影响美观,对于我这种有强迫症的人来说实在是忍无可忍,后来经过查找资料,终于发现了解决方法,可以使用vue现成的指令来解决这个问题 ...

  2. 算法基础-dfs

    最近在学dfs(深度优先搜索),dfs与树的遍历差不多,就是先从一个点开始一直搜索,直到走不动为止.现在推荐一个简单的dfs题, 百炼的2815, ########################## ...

  3. [读书笔记] Spring MVC 学习指南 -- 第一章

    控制反转(Inversion of Control, IoC)/ 依赖注入: 比如说,类A依赖于类B,A需要调用B的某一个方法,那么在调用之前,类A必须先获得B的一个示例引用. 通常我们可以在A中写代 ...

  4. DOM节点操作阶段性总结

    HTML中能看到的所有东西都是dom树中的一个节点,注意是“所有”,使用childNodes()可以看到,回车(换行)也是一个节点. 从上图可以看到,select中有四个option,但是有9个节点. ...

  5. PHP 通过命令异步执行PHP程序

    通过PHP执行系统命令调用PHP执行程序,让进程挂起到后台执行,不影响用户页面交互. 控制器调用命令,不用等待,后台创建一个进程执行程序. system(“nohup php command.php ...

  6. python中的rabbitmq

    介绍 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议.MQ全称为Message Queue, 消息队列(MQ)是一种应用 ...

  7. C语言程序设计:现代方法(第2版)第二章全部习题答案

    前言 本人在通过<C语言程序设计:现代方法(第2版)>自学C语言时,发现国内并没有该书完整的课后习题答案,所以就想把自己在学习过程中所做出的答案分享出来,以供大家参考.这些答案是本人自己解 ...

  8. go学习笔记-标准库

    标准库 名称 摘要 archive tar tar包实现了tar格式压缩文件的存取. zip zip包提供了zip档案文件的读写服务. bufio bufio 包实现了带缓存的I/O操作. built ...

  9. java入门---对象和类&概念详解&实例

        Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载     这篇文章,我们主要来看下: 对象:对象是类的一个实例(对象不是找个女朋友),有状态 ...

  10. mybatis入门(一):jdbc的缺点

    mybatis的基础内容 1.mybatis的框架原理 2.mybatis开发dao两种方法: a.原始dao开发方法(程序需要编写dao接口和dao实现类) b.mybatis的mapper接口(相 ...