Modern C++ 学习笔记——C++面向对象编程

往期精彩:

Modern C++ 学习笔记——C++中的面向对象编程

关键字:面向对象、设计模式、面向接口、IoC

面向对象编程(OOP)

在谈起C++函数式编程复兴之前,先聊一下大家更熟悉的“面向对象编程”(Object-oriented programming,OOP)这个编程范式。
学习过C++之后一定都知道面向对象的三大特性:封装、继承、多态,正是C++提供了如此强大的特性,让C++程序员更熟悉面向对象命令式的编程。面向对象编程是中具有对象概念的编程范式,同时也是一种程序开发的抽象方针。它将对象作为程序的基本单元,将程序和数据封装其中,以提高软件的重用性、灵活性和扩展性,对象里的程序可以访问及经常修改对象相关连的数据。

面向对象程序设计可以看作一种在程序中包含各种独立而又互相调用的对象的思想,这与传统的思想刚好相反:传统的程序设计主张将程序看作一系列函数的集合,或者直接就是一系列对电脑下达的指令。面向对象程序设计中的每一个对象都应该能够接受数据、处理数据并将数据传达给其它对象,因此它们都可以被看作一个小型的“机器”,即对象。

说起面向对象无论如何都绕不过GoF的著作《设计模式:可复用面向对象软件的基础》,在书中最开始的部分就对面向对象的核心做出了总结,这也是书中23种经典设计模式的核心理念:

  • 面向接口编程,而不是面向实现编程
    • 使用值不需要知道数据类型、结构、算法细节。
    • 使用者不需要知道实现细节,只需要知道提供的接口。
    • 利用抽象、封装、动态绑定、多态。
    • 符合面向对象的特质和理念。
  • 优先使用对象组合,而不是类继承
    • 继承需要给子类暴露一些父类的设计和实现细节。
    • 父类实现的改变会造成子类也需要改变。
    • 我们以为继承主要是为了代码重用,但实际上在子类中需要重新实现很多父类的方法。
    • 继承更多的应该是为了多态。

示例一:桥接模式

假设有如下描述:

  • 家具:木头桌子、木头椅子、塑料桌子、塑料桌子
  • 属性:燃点、密度、价格、重量

我们如下所展示的一样设计类:

  • 有“材质类”Material,具有属性燃点和密度
  • 有“家具类”Furniture,具有属性价格和体积
  • 在 Furniture 中耦合了 Material。而具体的 Material 是 Wood 还是 Plastic,这在构造对象的时候注入到 Furniture 里就好了。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Modern C++ Programming Cookbook by Marius Bancila English | 15 May 2017 | ASIN: B01MQDKPV8 | 590 Pages | AZW3 | 800.97 KB Over 100 recipes to help you overcome your difficulties with C++ programming and gain a deeper understanding of the working of modern C++ About This Book Explore the most important language and library features of C++17, including containers, algorithms, regular expressions, threads, and more, Get going with unit testing frameworks Boost.Test, Google Test and Catch, Extend your C++ knowledge and take your development skills to new heights by making your applications fast, robust, and scalable. Who This Book Is For If you want to overcome difficult phases of development with C++ and leverage its features using modern programming practices, then this book is for you. The book is designed for both experienced C++ programmers as well as people with strong knowledge of OOP concepts. What You Will Learn Get to know about the new core language features and the problems they were intended to solve Understand the standard support for threading and concurrency and know how to put them on work for daily basic tasks Leverage C++'s features to get increased robustness and performance Explore the widely-used testing frameworks for C++ and implement various useful patterns and idioms Work with various types of strings and look at the various aspects of compilation Explore functions and callable objects with a focus on modern features Leverage the standard library and work with containers, algorithms, and iterators Use regular expressions for find and replace string operations Take advantage of the new filesystem library to work with files and directories Use the new utility additions to the standard library to solve common problems developers encounter including string_view, any , optional and variant types In Detail C++ is one of the most widely used programming languages. Fast, efficient, and flexible, it is used to solve many problems. The latest versions of C++ have seen programmers change the way they code, giving up on the old-fashioned C-style programming and adopting modern C++ instead. Beginning with the modern language features, each recipe addresses a specific problem, with a discussion that explains the solution and offers insight into how it works. You will learn major concepts about the core programming language as well as common tasks faced while building a wide variety of software. You will learn about concepts such as concurrency, performance, meta-programming, lambda expressions, regular expressions, testing, and many more in the form of recipes. These recipes will ensure you can make your applications robust and fast. By the end of the book, you will understand the newer aspects of C++11/14/17 and will be able to overcome tasks that are time-consuming or would break your stride while developing. Style and approach This book follows a recipe-based approach, with examples that will empower you to implement the core programming language features and explore the newer aspects of C++.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值