美文网首页
GeekBand C++面向对象高级编程(三)

GeekBand C++面向对象高级编程(三)

作者: Yuxuanxuan | 来源:发表于2016-03-19 15:55 被阅读31次

本笔记为GeekBand C++面向对象高级编程(上)第二周课程之总结。

组合与继承

组合(composition),类A has-a 类B

构造由内而外:


析构由外而内:


委托(Delegation)-- Composition by reference

pointer to implementation

继承(Inheritance)

类A is-a 类B

构造由内而外:derived的构造函数先调用base的构造函数,然后才执行自己

析构由外而内:derived的构造函数先执行自己,然后才调用base的析构函数

base的析构函数必须是虚函数,否则会出现undefined behavior

Inheritance with virtual function

* non-virtual:不希望derived重定义(override)它

* virtual:希望derived重定义(override)它,且已有默认定义

* pure-virtual:希望derived一定重定义(override)它,且没有默认定义

Inheritance + composition下的构造与析构


相关文章

网友评论

      本文标题:GeekBand C++面向对象高级编程(三)

      本文链接:https://www.haomeiwen.com/subject/ttrakttx.html