[C++] OOP - Base and Derived Classes
There is a base class at the root of the hierarchy, from which the other class inherit, directly or indirectly.
These inheriting classes are known as derived calsses.
The key ideas in Object-Oriented Programming are data abstraction, inheritance and dynamic binding.
Using data abstraction, we only expose the interface and hide the detail implementation.
Through inheritance, we define classes that model relationships among similar types.
Through dynamic binding, we can use the objects of these types and ignore the detail how they differ.
Dynamic binding
In C++, dynamic binding happens when a virtual function is called through a reference(or pointer) to a base class.
class Quote{
public:
virtual double net_price(size_t n) const;
}
class Bulk_quote : public Quote{
public:
double net_price(size_t n) const;
}
void print_total(ostream & os, const Quote & item, size_t n){
os << item.net_price(n);
}
print_total(cout, base, ); // base has Quote type, call Ouote version
print_total(cout, bulk, ); // bulk has Bulk_quote, call bulk_quote version
The decision as to which version to run depends on the type of argument at run time. Therefore, dynamic binding is known as runtime binding.
Basic class ordinarily should define a virtual destructor. Virtual destructor are needed even if they do not work.
Any non-static member function, other than constructor, maybe virtual. The virtual keyword only appear on the declaration inside the class, and may not appear on the definition that apper outside the class body.
A function that is virtual in base class is implicitly virtual in the derived classess as well.
Member functions that are not declared as virtual are resolved at compile time, not run time.
If a derived class do not override the virtual from its base, then like any other member, the derived class inherit the version defined in the base class.
A derived class contains subobject corresponding to its base class. We can an object of a derived class as if it was an object of its base class. We can bind an reference(or pointer) to the base-class part of a derived class.
Quotes item; // an object of base type
Bulk_quote bulk; // an object of derived type
Quotes *p = &item; // p points to a Quote object
p = &bulk; // p points the Quote part of bulck
Quote & ref = bulk; // ref bound to the Quote part of bulck
The fact that the derived object contains subobjects for its base classes is the key to how inheritance work.
Like any ohter code that create an objec to the base-class type, a derived class must use a base-class constructor to initialize its base-class part.
Bulk_quote(const string& bookNo, double price, size_t qty, double disc): Quote(bookNo, price), min_qty(qty), discount(dis) {}
Process flow:
- The Quote constructor initializes the base-class part of the Bulk_quote. E.g. the bookNO and price members
- execute the empty Quote constructor body
- Initilize the direct member min_qty and discount
- execute the empty function body of Bulk_quote constructor
The keyword final prevent a class from being used as a base class
class NoDerived final { /* */};
class DerivedType : NoDerived { /* */}; // error NoDerived is final
The static type is the type of a variable or other expression. The static type of an express is always known at compile time - it is the type with which a variable is declared or an expression yields.
The dynamic type is the type of an object. The dynamic type is the type of object in momery that the variable or expression represents. The dynamic type may not know until run time.
There is a implicit(automatic) conversion from base to derived. A base object may not contain members defined by the derived class.
Quote base;
Bulk_quote * bulk = &base; // error: cannot convert base to derived
Bulk_quote & bulkRef = base; // error: cannot convert base to derived
If we know the conversion is safe, we can use static_cast to make the conversion.
When we initialize an object of base type from an object of derived type, only the base part of the derived object is copied, moved or assigned. The derived part of the derived object is ignored.
Reference:
C++ Primer, Fifth Edition, chapter 15 Object-Oriented Programming
[C++] OOP - Base and Derived Classes的更多相关文章
- How can I protect derived classes from breaking when I change the internal parts of the base class?
How can I protect derived classes from breaking when I change the internal parts of the base class? ...
- 【转载】#330 - Derived Classes Do Not Inherit Constructors
A derived class inherits all of the members of a base class except for its constructors. You must de ...
- Virtual functions in derived classes
In C++, once a member function is declared as a virtual function in a base class, it becomes virtual ...
- [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. Howe ...
- C++ Core Guidelines
C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...
- C++ Knowledge series 1
Programming language evolves always along with Compiler's evolvement. 1. The C++ Object Model: Strou ...
- <Effective C++>读书摘要--Inheritance and Object-Oriented Design<一>
1.Furthermore, I explain what the different features in C++ really mean — what you are really expres ...
- Swift 中的静态方法继承
Base and Derived Classes: class BaseClass{ class func staticMethod(){ println("BaseClass.static ...
- 【足迹C++primer】52、,转换和继承虚函数
转换和继承,虚函数 Understanding conversions between base and derived classes is essential to understanding h ...
随机推荐
- Swift_100个Swift必备Tips 王巍 PDF
Swift_100个Swift必备Tips 王巍 PDF GitHub下载地址
- Swift_枚举
Swift_枚举 点击查看源码 空枚举 //空枚举 enum SomeEnumeration { // enumeration definition goes here } 枚举基本类型 //枚举基本 ...
- 关于css属性calc对于ie的态度
做的一个项目,布局的时候用到了max-height:calc(100% - 15px);在谷歌.火狐浏览器,进行下拉的时候,它的父元素会出现垂直滚动条,但是在IE就不可以. 然后在网上找了找,说在它的 ...
- 常用的JavaScript设计模式(二)Factory(工厂)模式
Factory通过提供一个通用的接口来创建对象,同时,我们还可以指定我们想要创建的对象实例的类型. 假设现在有一个汽车工厂VehicleFactory,支持创建Car和Truck类型的对象实例,现在需 ...
- 【tp5.1】通过PHPExcel实现导入excel表格
1.上github下载PHPExcel,链接:https://github.com/PHPOffice/PHPExcel 2.下载解压后,将Classes改名为PHPExcel如图 3.将文件夹复制到 ...
- Git 与 SVN对比详解
一.Git vs SVNGit 和 SVN 孰优孰好,每个人有不同的体验. Git是分布式的,SVN是集中式的 这是 Git 和 SVN 最大的区别.若能掌握这个概念,两者区别基本搞懂大半.因为 Gi ...
- 修改注册表删除Windows资源管理器 “通过QQ发送” 右键菜单项
运行regedit 展开至:HKEY_CLASSES_ROOT\AllFilesystemObjects\shellex\ContextMenuHandlers 删除QQShellExt项
- C++的特点
C和C++ C主要是应用在在驱动层,是面向过程的编程语言,对类型的定义不是很严格.C++主要是应用与应用层,是C语言的一个加强版,可以完全兼容C语言,并且还有很多C语言不具备的特性,如,C++是一种面 ...
- Jsp刷新分页模板,很全
1.用来实现上一页下一页,我直接写到查询页面上 <%--page的分页--%> <style type="text/css"> a { color: # ...
- Ruby & Rails学习资料
------------------教程------------- Ruby风格指南(代码规范) https://github.com/bbatsov/ruby-style-guide 笨方法學 Ru ...