侯捷C++面向对象高级编程之---(11-13)继承 & 复合 & 委托 & 虚函数与多态 & 委托相关设计

本文探讨了面向对象编程(OOP)中的继承与组合(Composition),介绍了继承(is-a关系)和委托(has-a关系)的概念,通过实例说明了构造和析构的先后顺序,以及如何在委托模式下实现pImpl设计。同时,对比了继承+复合和委托的区别,并结合字符串计数的实例解析了相关概念的实际应用。

参考详细帖子,包含实例-1

参考详细帖子,包含实例-2

Object Oriented Programming(OOP), Object Oriented Design (OOD)

Inheritance (继承),表示 is-a

  • struct也是一种class
  • 父类的成员,子类可以继承
    在这里插入图片描述
    构造和析构
    - 构造:先父类(默认构造函数)后子类
    - 析构:先子类后父类析构函数
    - 父类析构必须时virtual,否则会出现undefined behavior
    - 编写class时,当该类可能会称为父类,就要把析构函数写成virtual(好习惯)
    -
    Inheritance (继承) with virtual

在这里插入图片描述
Composition(复合),表示 has-a

  • 复合 可以表示为 has-a 的关系,queue中有一个deque
  • 用图表示类之间的关系,实心的菱形
  • 传统c语言的struct也存在这种关系
    在这里插入图片描述
    求sizeof

在这里插入图片描述

复合关系下的构造和析构

  • 构造由内而外
  • 析构由外而内
  • 不满意编译器调用默认构造函数,就要自己在同样的位置写清楚调用哪个。
    在这里插入图片描述
    Delegation(委托)Composition by reference (其实就是pointer)
  • 相比上一个复合,委托是里面有一个其他类的指针(对应的对象和内存等到需要的时候才创建,不一定要在构造函数直接创建)
  • 图的话,用空心的菱形表示

委托相比复合:

在这里插入图片描述
补充一个弹幕的小问题:传参的时候,引用&和指针*的区别

  • &类似一个不能改变指向的地址,相当于 T* const,所以&是类型安全的,而*不是;
  • 可以用 const T* const 达到 const T&的目的 (没有T& const a这种形式)
  • &只能在定义时被初始化一次,之后不可变;*可变
  • &用.来调用,*用->来调用
  • *是一个实体,&仅仅是别名(不占内存)
  • &不能为空,*可以为空
  • sizeof中:&得到对应类型的大小,*得到指针的大小
  • 自增(++)运算意义不一样

继承 Inheritance + 复合 Composition 情况下的 构造和析构 的先后顺序
在这里插入图片描述
委托 Delegation + 继承 Inheritance :实例
在这里插入图片描述
在这里插入图片描述

设计模式的书:design pattern explained simply

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

delegation实例

注意:之所以说这个例子不好,因为虽然这里包含了类的指针,也即是委托。但是它并不是纯粹的pImpl模式,因为在sstring类里面还是包含了stringRep的成员,做这个例子只是为了实现count_n的功能而已,即表示多少sstring对象指向一个stringRep对象。

class stringRep
{
	friend class sstring;
public:
	stringRep() :count_n(0) { cout << "construct a objectStringRep" << endl; }
	~stringRep() { cout << "destruct a objectStringRep" << endl; }
	stringRep(const char* chr) :count_n(0) { this->chr = chr; cout << "construct a objectStringRep" << endl; }
	const char* getchr() const { return chr; }
	int get_n() const { return count_n; }
private:
	int count_n;
	const char* chr;
};


class sstring
{
public:
	sstring() { cout << "construct a objectString" << endl; }
	sstring(stringRep& sr) :rep(&sr) { sr.count_n++; cout << "construct a objectString" << endl; }
	~sstring() { (rep->count_n)--; cout << "destruct a objectString" << endl; }
private:
	stringRep* rep;
};


int main() {
	//delegation 实际应用

	stringRep sr("never give up!!!");
	sstring str1(sr);
	sstring str2(sr);
	sstring str3(sr);
	cout << endl << "count_n=" << sr.get_n() << endl ;
	cout << sr.getchr() << endl;
	return 0;
}
If you have ever bought any programming books, you might have noticed that there are two types of them: books that are too short to understand the topic and books that are too long making it inevitable that you get bored. We&#39;ve tried hard to avoid both of these categories with Design Patterns Explained Simply. This book is fast and simple way to get the idea behind each of the 29 popular design patterns. The book is not tied to any specific programming language and will be good for you as long as you have a basic knowledge of OOP. Most chapters are available on the website, so you can check out the simplicity of the language in the book and the way materials are presented. Why should I read this book? It&#39;s simple. It&#39;s written in clear and simple language that makes it easy to read and understand. It&#39;s short. Yes, there are no useless demos or huge code listings &mdash; just clear and easy-to-understand descriptions with many graphical examples. When you finish reading this book, you&#39;ll have good reason to go to your boss and ask him for apromotion. Why? Because using design patterns will allow you to get your tasks done twice as fast, to write better code and to create efficient and reliable software architecture. How do I become a programming ninja? The main difference between a ninja and a novice is the knowledge of secret coding tricks, as well as the awareness of most pitfalls and the ability to avoid them. Design patterns were created as a bible for avoiding problems related to software design. Doesn&rsquo;t that make it a true ninja&rsquo;s handbook? Table of Contents Creational patterns Abstract Factory Builder Factory Method Object Pool Prototype Singleton Structural patterns Adapter Bridge Composite Decorator Facade Flyweight Private Class Data Proxy Behavioral patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Null Object Observer State Strategy Template Method Visitor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DsAuto_汽车电子电控

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值