分享兴趣,传播快乐,增长见闻,留下美好!
亲爱的您,这里是LearningYard新学苑。
今天小编为大家带来“深入C++(十九)——继承”,欢迎您的访问。
Share interest, spread happiness, increase knowledge, and leave beautiful.
Dear, this is the LearingYard Academy!
Today, Deep into C++ (19) – Inheritance ,welcome to visit!
一、 认识继承
1. Understand inheritance
继承,子类继承父类的属性和方法。子类含有父类中的属性,也可调用父类中的方法。
Inheritance, where children inherit the properties and methods of their superclass. Subclasses have properties from their superclass and can invoke methods from their superclass.
有一个学习编程的网页,有顶部的导航栏,也有侧边的导航栏,还有底部信息,有中间的学习资料。侧边的导航栏,是学习视频的分类。选择哪个类,中间就会出现哪种学习视频。比如,选择Java,中间会出现与Java有关的各种学习视频。当然顶部、底部和侧面都会一直显示,只有中间的内容有所改变。
There is a web page for learning programming, with a top navigation bar, a side navigation bar, and bottom information, with learning materials in the middle. The navigation bar on the side, is to learn the classification of the video. Which class is selected, which learning video will appear in the middle. For example, if you choose Java, there will be a variety of learning videos about Java. Of course, the top, bottom, and sides are always displayed; only the content in the middle changes.
通过继承,将顶部、底部和侧边这些不变的模块放入父类进行编写。
With inheritance, you put the invariant top, bottom, and side modules into a parent class to be written.
而改变的学习内容在各个子类中分别编写。
The changed learning content is written separately in each subclass.
运行,公共部分全部显示,只有学习内容不同。
Run, the common part is all displayed, only the learning content is different.
二、 继承方式
2. Inheritance mode
继承的基本格式:“class 子类 :继承方式 父类”。继承方式有三种,public(公共继承)、protected(保护继承)和private(私有继承)。
Basic form of inheritance: “class subclass: inherits by superclass”. There are three types of inheritance: public, protected, and private.
父类的私有属性和私有方法,子类无论何种继承方式都无法继承。目前学习过的知识中,能访问类中私有属性和私有方法的,只有该类及其对象和该类的友元。
Children cannot inherit private properties and methods from their superclass, regardless of inheritance. In what you have seen so far, the only people who can access the private properties and methods of a class are the class, its objects, and its friends.
公共继承。继承的属性权限不会改变。
Public inheritance. Inherited property permissions do not change.
保护继承。继承的属性权限全为保护。
Protect inheritance. Inherited property permissions are all protected.
私有继承。继承的属性权限全为私有。
Private inheritance. Inherited property permissions are all private.
三、 对象模型
3. Object model
有个问题,当子类继承父类的属性后,子类对象的所需内存空间是多大?是4字节,还是12字节,亦或是16字节?
Heres a question: When a child class inherits a property from its parent class, how much memory is required for an object of the child class? Is it 4 bytes, or 12 bytes, or 16 bytes?
运行,看结果。16,表明继承的属性和新增的属性全部都占用内存空间,且父类中的私有属性实际上也被继承了,只是被编译器隐藏了,无法访问。
Run it and see the result. 16 indicates that the inherited and newly added properties all take up memory, and that the private properties of the superclass are actually inherited but hidden from access by the compiler.
还可通过visual studio自带开发人员工具,生成类布局报告。
You can also use visual studios built-in developer tools to generate class layout reports.
通过代码,输出Son的布局。可以看到,16个字节,父类的所有属性都被子类继承。
Through the code, the layout of the Son is output. As you can see, with 16 bytes, all the properties of the parent class are inherited by the child class.
今天的分享就到这里了,
如果您对文章有独特的想法,
欢迎给我们留言。
让我们相约明天,
祝您今天过得开心快乐!
Thats all for todays sharing.
If you have a unique idea about the article,
please leave us a message,
and let us meet tomorrow.
I wish you a nice day!
翻译:网易有道翻译
本文由LearningYard学苑整理并发出,如有侵权请后台留言沟通.
文案|Dongyang
排版|Dongyang
审核|hong
Learning Yard 新学苑