一种错误的观念:

子类继承父类,只把父类的公有成员继承下来,私有的不会继承。

事实上无论是如何继承,都会把父类的所有成员继承下来。

 #include<iostream>
using namespace std; class Base {
private:
int x;
}; class D :private Base{
public:
int y;
}; int main()
{
cout << sizeof(Base) << endl;
cout << sizeof(D) << endl;
return ;
}

输出结果:4  8

继承关系要看2点,见下图

对于继承关键字,我门只需要写一次,但是用的时候却要看2次。见上图2个小人,一个看子类内部通过继承关键字如何看代父类。一个是子类对象通过继承关键字如何看代父类。

不管使用何种继承方式,父类私有数据成员,子类内部都是不能直接访问的。可以通过父类的共有方法间接访问父类私有数据。这里有一点需要注意,假如是private继承父类,父类里面protected数据或方法、public数据或方法、private方法都是可以直接访问的。  这时候如果站在子类内部那个小人的角度看问题,private和protected表现行为差不多。那protected和private区别在哪?

区别在孙子类那里,如果是private继承,Base父类在儿子A那里都是私有的,到了孙子B那里父类无论是数据还是方法都访问不了了。相当于private关键字割断了继承关系,整个继承家族到儿子辈就绝后了。如果是protected就可以保障继承关系不断。

private指定的属性 或 方法,将不能被继承。

protected指定的属性 或 方法,将在类外部不可见,但可以被继承。

 派生类的构造函数与析构函数调用顺序

注:图片中 同名覆盖改为同名隐藏

#include<iostream>
using namespace std; class Base1
{
public:
Base1()
{
cout << "Create Base1" << endl;
}
~Base1()
{
cout << "Free Base1" << endl;
}
}; class Base2
{
public:
Base2()
{
cout << "Create Base2" << endl;
}
~Base2()
{
cout << "Free Base2" << endl;
}
}; class Base3
{
public:
Base3()
{
cout << "Create Base3" << endl;
}
~Base3()
{
cout << "Free Base3" << endl;
}
}; class D :public Base2, public Base1, public Base3
{
public:
D()
{
cout << "Create D" << endl;
}
~D()
{
cout << "Free D" << endl;
}
private:
Base1 b1;
Base2 b2;
Base3 b3;
}; int main(int argc, char *argv[])
{
D d;
return ;
}

如果你继承的父类不提供默认或者缺省的构造函数,我们就必须使用参数列表的形式对父类进行构造。参数列表相当于再调用父类的构造函数。

千万不要把参数列表那里的父类构造函数放到子类构造函数里面,那样代表先完成子类构造再完成父类构造。爸爸还没生出来来,怎么会有儿子呢?

#include<iostream>
using namespace std; class Base1
{
public:
Base1(int d=):x(d)
{
cout << "Create Base1" << endl;
}
~Base1()
{
cout << "Free Base1" << endl;
}
private:
int x;
}; class Base2
{
public:
Base2(int d = ):y(d)
{
cout << "Create Base2" << endl;
}
~Base2()
{
cout << "Free Base2" << endl;
}
private:
int y;
}; class Base3
{
public:
Base3(int d = ):z(d)
{
cout << "Create Base3" << endl;
}
~Base3()
{
cout << "Free Base3" << endl;
}
private:
int z;
}; class D :public Base2, public Base1, public Base3
{
public:
D(int data):Base1(data), Base2(data), Base3(data),b1(data), b2(data), b3(data)
{
cout << "Create D" << endl;
}
~D()
{
cout << "Free D" << endl;
}
private:
Base1 b1;
Base2 b2;
Base3 b3;
}; int main(int argc, char *argv[])
{
D d();
return ;
}

参数列表那里的构造函数顺序任意写。决定构造函数顺序只有2处:①继承声明时的顺序②类内部数据成员的顺序。

对于多继承,某一数据成员可能在多个父亲中存在定义。在子类中访问父类数据成员时会存在二义性。下面代码演示,这段代码编译不过

#include<iostream>
using namespace std; class B1
{
public:
B1(int d=):n(d)
{}
~B1()
{}
public:
int n;
}; class B2
{
public:
B2(int d = ):n(d)
{}
~B2()
{}
public:
int n;
}; class D :public B2, public B1
{
public:
D():x()
{}
~D()
{}
private:
int x;
}; int main(int argc, char *argv[])
{
D d;
d.n=;
return ;
}

这种情况需要指明到底是哪个父类的数据成员

#include<iostream>
using namespace std; class B1
{
public:
B1(int d=):n(d)
{}
~B1()
{}
public:
int n;
}; class B2
{
public:
B2(int d = ):n(d)
{}
~B2()
{}
public:
int n;
}; class D :public B2, public B1
{
public:
D():x()
{}
~D()
{}
private:
int x;
}; int main(int argc, char *argv[])
{
D d;
d.B1::n=;
return ;
}

钻石继承

#include<iostream>
using namespace std; class B0
{
public:
B0(int d=):m(d)
{}
~B0()
{}
public:
int m;
}; class B1:public B0
{
public:
B1(int d = ):n(d)
{}
~B1()
{}
public:
int n;
}; class B2 :public B0
{
public:
B2(int d = ) :n(d)
{}
~B2()
{}
public:
int n;
}; class D :public B2, public B1
{
public:
D():x()
{}
~D()
{}
private:
int x;
}; int main(int argc, char *argv[])
{
D d;
d.B1::n=;
d.B1::m = ;
return ;
}

类B1,B2都有m,所以D中要想使用m必须指定具体是哪个类的m

使用virtual关键字,让整个钻石继承使用一个数据成员。这种继承叫虚拟继承

#include<iostream>
using namespace std; class B0
{
public:
B0(int d=):m(d)
{}
~B0()
{}
public:
int m;
}; class B1: virtual public B0
{
public:
B1(int d = ):n(d)
{}
~B1()
{}
public:
int n;
}; class B2 : virtual public B0
{
public:
B2(int d = ) :n(d)
{}
~B2()
{}
public:
int n;
}; class D :public B2, public B1
{
public:
D():x()
{}
~D()
{}
private:
int x;
}; int main(int argc, char *argv[])
{
D d;
d.B1::n=;
d.m = ;
return ;
}

在派生类对象的创建中,首先是虚基类的构造函数并按它们声明的顺序构造。第二批是非虚基类的构造函数按它们声明的顺序调用。第三批是成员对象的构造函数。最后是派生类自己的构造函数被调用

#include<iostream>
using namespace std; class Base1
{
public:
Base1()
{
cout << "Create Base1" << endl;
}
~Base1()
{
cout << "Free Base1" << endl;
}
}; class Base2
{
public:
Base2()
{
cout << "Create Base2" << endl;
}
~Base2()
{
cout << "Free Base2" << endl;
}
}; class Base3
{
public:
Base3()
{
cout << "Create Base3" << endl;
}
~Base3()
{
cout << "Free Base3" << endl;
}
}; class D :public Base2, virtual public Base1, virtual public Base3
{
public:
D()
{
cout << "Create D" << endl;
}
~D()
{
cout << "Free D" << endl;
}
private:
Base1 b1;
Base2 b2;
Base3 b3;
}; int main(int argc, char *argv[])
{
D d;
return ;
}

C++——Inheritence的更多相关文章

  1. JAVA 1.9 面向对象之封装

    1. 面向对象程序设计的三大基本特征:继承(Inheritence).封装(Encapsulation).多态(Polymorphism)2. 封装:类包含了数据与方法,将数据与方法放在一个类中就构成 ...

  2. Java 编程入门(词汇表)

    抽象类(abstract class):抽象类不能创建对象,主要用来创建子类.Java中的抽象类使用 abstract 修饰符定义. 抽象数据类型(abstract data type ADT):抽象 ...

  3. Java性能提示(全)

    http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...

  4. 常用CSS Reset汇总

    什么是Css Reset呢? 在 HTML标签在浏览器里有默认的样式,不同浏览器的默认样式之间也会有差别.在切换页面的时候,浏览器的默认样式往往会给我们带来麻烦,影响开发效率.所以解决的方法就是一开始 ...

  5. Java SE 第九讲---面向对象特征之封装1

    1.面向对象程序设计的三大基本特征:继承(Inheritence).封装(Encapsulation).多态(Polymorphism) 2.封装:类包含数据与方法,将数据与方法放在一个类中就构成了封 ...

  6. Java SE ---类,方法,对象等

    1,面向对象程序设计的三大基本特征:继承(Inheritence).封装(Encapsulation).多态(Polymorphism)     2,如何定义类?            修饰符 cla ...

  7. 关于Java多态的总结.

    [圣思源笔记]JAVA SE Lesson 11. 类是一种抽象的概念,对象是类的一种具体表示形式,是具体的概念.先有类,然后由类来生成对象(Object).对象又叫做实例(Instance).2. ...

  8. WINAPI 变量(2861个)

    WINAPI 变量(2861个)   这是从 c:\Program Files\Windows Kits\8.1\Include\um\WinUser.h 这个文件 中提取的 CTRL+F 查看变量所 ...

  9. Windows API 常量定义

    Windows 常量定义在winuser.h中可以找到,如果了安装了visual studio 2010,winuser.h所在目录为C:\Program Files (x86)\Microsoft ...

随机推荐

  1. ubuntu18.04手动安装二进制MySQL8.0

    wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz tar xvJf ...

  2. sriov-网络问题Debug记录

    我司容器云平台使用了sriov的底层网络模型,这个网络驱动的好处是配置少,转发效率高,但是缺点也很明显,出了问题比较难Debug. 现就工作中出现的问题记录如下: 容器删除后,或者docker进程异常 ...

  3. 最新 医渡云java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.医渡云等10家互联网公司的校招Offer,因为某些自身原因最终选择了医渡云.6.7月主要是做系统复习.项目复盘.LeetCo ...

  4. 用Gson实现json与对象、list集合之间的相互转化

    先写一个Person实体类,导入Gson包 String jsonData="{\"userid\":\"1881140130\"}";// ...

  5. Bean配置

    1.xml配置(摘抄自:https://www.cnblogs.com/zyx1301691180/p/7665971.html) 一.setter方法配置Bean: 1.创建一个 Spring Be ...

  6. SQL语言(二)

    SQL约束与策略 create table student( id int primary key, //主键约束 name ) not null, //非空约束 idCard ) unique, / ...

  7. Matlab R2017b 关联 .m 和 .fig 文件

    1. 前言 安装「Matlab R2017b」后,无法关联.m和.fig文件,每次需要在MATLAB里边打开,而不能之间点击.m文件打开,十分麻烦. 2. 解决方案 1.首先,在Matlab R201 ...

  8. C之typedef应用

    1.0关于typedef关键字的基础: https://www.cnblogs.com/anSn/p/8783347.html 1.1 typedef 修饰“函数类型” 的调用方法: 1)我们写一段普 ...

  9. Python程序计算ax^2+bx+c=0方程根

    程序用来计算ax^2+bx+c=0的两个根,有些异常暂时无法处理: #!/usr/bin/python # -*- coding: utf-8 -*- #当程序存在中文时,注释表明使用utf-8编码解 ...

  10. mininet安装配置

    mininet安装配置 安装mininet mininet使用 在VM中运行mininet 安装VMware,在VMware中打开下载好的mininet虚拟机映像 启动虚拟机,虚拟机的初始账号密码均为 ...