[C++] OOP - Virtual Functions and Abstract Base Classes

 

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", 50);
    print_total(cout, base, 10);    // call Quote::net_print

    Bulk_quote derived("0-201-2", 50, 5, 0.19);
    print_total(cout, derived, 10);    // call Bulk_quote::net_print

    base = derived;      // copy the Quote part of derived into base
    base.net_print(30);    // 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(42);

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 = 0;    // an pure virtual function
protected:
    size_t quantity = 0;
    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

 

转载于:https://www.cnblogs.com/TonyYPZhang/p/6561821.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值