c++详解【继承】

学过c++的人都知道,c++的三大特性:封装、继承、多态

我们今天说的是c++的继承,那么为什么要引入继承,它有什么特点呢?

首先,继承的特点是:使代码复用,为后面学习多态做铺垫。

继承分为:私有继承(private)、公有继承(public)、保护继承(protected)

分别举例介绍一下它们各自的特性吧:

一、继承

私有继承:

//Inherit.h

#pragma once
#include<iostream>
using namespace std;class Base
{
public:Base(){}void FunTest(){cout<<_pri<<endl;}int _pub;
protected:int _pro;
private:int _pri;
};class Derived:private Base         //私有继承基类Base
{
public:Derived(){}void Function(){cout<<_pub<<endl;}
private:int _d;
};
//Inherit.cpp

#include"Inherit.h"int main()
{Base b;Derived d;d._pri = 10;         //errord._pro = 20;         //errord._pub = 30;         //errord.Function();return 0;
}

通常我们把继承自基类的类称为派生类

运行结果:


由上述结果可知,对于私有继承下的派生类来说,在基类中的公有成员及成员函数继承到派生类就变为私有,基类的保护也变为派生的私有,基类的私有则不继承下去。当然私有继承下的派生类中的成员及成员函数不能在类外被访问。

保护继承:

//Inherit.h

#pragma once
#include<iostream>
using namespace std;class Base
{
public:Base(){}void FunTest(){cout<<_pri<<endl;}int _pub;
protected:int _pro;
private:int _pri;
};class Derived:protected Base
{
public:Derived(){}void Function(){cout<<_pub<<endl;}
private:int _d;
};class Third:protected Derived
{
public:void F(){cout<<_pub<<endl;}
private:int _third;
};
//Inherit.cpp

#include"Inherit.h"int main()
{Base b;Derived d;Third t;d._pri = 10;        //errord._pro = 20;        //errord._pub = 30;        //errord.Function();t.F();return 0;
}

运行结果:


由上述结果可知,对于保护继承下的派生类来说,在基类的公有成员及成员函数继承到派生类中就变为私有,基类的保护也变为派生的保护,基类的私有则不继承下去。此时如果变为保护,则在有继承关系的类与类之间可以互相访问,而在类外不可访问。

公有继承:

//Inherit.h

#pragma once
#include<iostream>
using namespace std;class Base
{
public:Base(){}void FunTest(){cout<<_pri<<endl;}int _pub;
protected:int _pro;
private:int _pri;
};class Derived:public Base
{
public:Derived(){}void Function(){cout<<_pub<<endl;}
private:int _d;
};class Third:protected Derived
{
public:void F(){cout<<_pub<<endl;cout<<_pro<<endl;}
private:int _third;
};
//Inherit.cpp

#include"Inherit.h"int main()
{Base b;Derived d;Third t;d._pri = 10;           //errord._pro = 20;           //errord._pub = 30;d.Function();t.F();return 0;
}

运行结果:


由上述结果可知,对于公有继承下的派生类来说,在基类的公有成员及成员函数继承到派生类中仍为公有(即在类外也可访问),基类的保护也变为派生的保护,基类的私有则不继承下去。

下面拿一个表格来总结一下吧~


注:若类中省略以何种方式继承时,默认为私有继承。

二、调用顺序

例1:

//Inherit.h

#pragma once
#include<iostream>
using namespace std;
class Base
{
public:Base(){cout<<"Base()"<<endl;}~Base(){cout<<"~Base()"<<endl;}
private:int _b;
};class Derived:public Base
{
public:Derived(){cout<<"Derived()"<<endl;}~Derived(){cout<<"~Derived()"<<endl;}
private:int _d;
};
//Inherit.cpp

#include"Inherit.h"
int main()
{Base b;Derived d;getchar();return 0;
}
运行结果:



调用顺序分析:

Base b;创建一个基类的对象b,则调用基类的构造函数;Derived d;创建一个派生类对象d,则调用派生类的构造函数,此时这里没有直接的调用派生类的构造函数,而是调用基类的构造函数,最后再调用派生类的构造函数。(在派生类对象调用构造函数时,是在派生类对象构造函数的初始化列表中调用了基类的构造函数)

如派生类构造函数原型为:

	Derived():Base(){cout<<"Derived()"<<endl;}
例2:

//Inherit.h

#pragma once
#include<iostream>
using namespace std;class Base
{
public:Base(){cout<<"Base()"<<endl;}~Base(){cout<<"~Base()"<<endl;}
private:int _b;
};class C
{
public:C(){cout<<"C()"<<endl;}~C(){cout<<"~C()"<<endl;}
};
class Derived:public Base
{
public:Derived():Base(){cout<<"Derived()"<<endl;}~Derived(){cout<<"~Derived()"<<endl;}
private:C _c;
};
//Inherit.cpp

#include"Inherit.h"
int main()
{Base b;Derived d;getchar();return 0;
}
运行结果:



构造函数的调用次序:

初始化列表的构造函数-->成员变量的构造函数-->派生类的构造函数

三、继承体系的作用域

要知道,继承体系中基类和派生类是在两个不同的作用域中。

类中有很多函数,那么当基类的成员函数和派生类的成员函数同名了怎么办呢?它会调用哪个函数呢?

例:

//Inherit.h

#pragma once
#include<iostream>
using namespace std;
class Base
{
public:Base(){}void Fun(){cout<<"Base::Fun()"<<endl;}
protected:int _b;
};class Derived
{
public:Derived(){}void Fun(){cout<<"Derived::Fun()"<<endl;}
private:int _d;
};
//Inherit.cpp

#include"Inherit.h"
int main()
{Base b;Derived d;d.Fun();system("pause");return 0;
}
运行结果:



当用派生类的对象调用与基类同名的函数的时候,派生类会调用自己的成员函数。把基类的函数隐藏起来。这被称作同名隐藏。(只要函数名相同,就会出现同名隐藏)

四、赋值兼容规则

关于赋值,大家再熟悉不过了吧。有同一类型的赋值,有不同类型的赋值(可能会用到强制类型转换哦)。

这里,我们要掌握的是基类与派生类之间的赋值;

例:

1、派生类对象赋给基类对象;(切割、切片)

2、基类对象不能赋给派生类对象;

3、基类的指针或引用指向派生类对象;

4、派生类的指针或引用不能指向基类的对象。(可以通过强制类型转换完成)

例:

//Inherit.h

#pragma once
#include<iostream>
using namespace std;
class A
{
public:A(){cout<<"A()"<<endl;}int _a;
};class B:public A
{
public:B(){cout<<"B()"<<endl;}int _b;
};

//Inherit.cpp

#include"Inherit.h"
int main()
{A a;B b;a = b;b = a;       //error,无法将基类对象赋给派生类对象A *pA = NULL;B *pB = NULL;pA = pB;pB = pA;     //error,无法让派生类指针给<span style="font-family: Arial, Helvetica, sans-serif;">基</span><span style="font-family: Arial, Helvetica, sans-serif;">类指针</span>
A &aa = b;B &bb = a;   //error,无法让派生类的引用给基类对象return 0;
}
上述错误的赋值和指向可以通过强制类型转化。

改:



五、几种不能继承的关系

1、友元关系不能继承。

2、对于静态成员及静态成员函数,则整个继承体系中只有一个这样的成员。无论派生出多少个子类,都只有一个这样的static成员实例。

例1:

//Inherit.h

#pragma once
#include<iostream>
using namespace std;
#include<string>
class Person
{friend void Display(Person &p,Student &s);
public:string p_name;
};
class Student:public Person
{
public:string s_no;
};void Display(Person &p,Student &s)
{cout<<p.p_name<<endl;cout<<s.p_name<<endl;cout<<s.s_no<<endl;
}
//Inherit.cpp

#include"Inherit.h"
int main()
{Person p;Student s;Display(p,s);           //error,无法识别Studentreturn 0;
}

这里说明的就是友元关系不能继承。

例2:

//Inherit.h

#pragma once
#include<iostream>
using namespace std;
#include<string>
class Person
{
public:Person(){cout<<"Person()"<<endl;++_count;}
protected:string _name;
public:static int _count;
};int Person::_count = 0;class Student:public Person
{
protected:int _num;
};class Graduate:public Student
{
protected:string _course;
};
//Inherit.cpp

#include"Inherit.h"
int main()
{Student s;Graduate g;cout<<Person::_count<<endl;cout<<Student::_count<<endl;Student::_count = 0;cout<<Student::_count<<endl;cout<<Person::_count<<endl;return 0;
}
运行结果:


注:友元函数和static成员函数都没有this指针。

六、单继承&多继承

上述讲述的都是单继承的形式,那么多继承是什么样子的呢?

例:

class A
{
public:A(){}
protected:int _a;
};class B
{
public:B(){}
protected:int _b;
};class C:public A ,public B             //实现多继承,c既可以访问A类的公有和保护成员,也可以访问B类的公有和保护成员。
{
public:C(){}
protected:int _c;
}

七、菱形继承(也称钻石继承)

形如:



代码如下:

Inherit.h

#pragma once
#include<iostream>
using namespace std;
class B
{
public:B(){}
protected:int _b;
};
class C1:public B
{
public:C1(){}
protected:int _c1;
};class C2:public B
{
public:C2(){}
protected:int _c2;
};class D:public C1,public C2
{
public:D(){}
private:int _d;
};

Inherit.cpp

#include"Inherit.h"
int main()
{B b;C1 c1;C2 c2;D d;cout<<sizeof(B)<<endl;           //打印类B中成员,所以它的size为4cout<<sizeof(C1)<<endl;          //打印类C1中的成员,并且继承了类B,所以它的size为8cout<<sizeof(C2)<<endl;          //打印类C2中的成员,并且继承了类B,所以它的size为8cout<<sizeof(D)<<endl;           //打印类D的成员,并且继承了类C1,C2,所以它的size为20return 0;
}
运行结果:



内存中是这样的形式出现的:



八、虚继承

为了解决菱形继承的二义性问题,又引出了虚继承的概念。在内存中添加了一个地址,用来存放偏移量。虽然浪费了空间,但是解决了二义性问题。

就如上述菱形继承一样,通常,我们在用D d;创建一个对象之后,用派生类的对象调用基类的成员时,不知道到底是C1还是C2继承下来的成员,这时就会出现二义性。为了解决上述问题的二义性,我们引出了虚继承。

例:

Inherit.h

class B
{
public:B(){}
protected:int _b;
};class C1:virtual public B
{
public:C1(){}
protected:int _c1;
};class C2:virtual public B
{
public:C2(){}
protected:int _c2;
};class D:public C1,public C2
{
public:D(){}
private:int _d;
};

Inherit.cpp

int main()
{B b;C1 c1;C2 c2;D d;cout<<sizeof(B)<<endl;  //只有一个成员,所以size为4cout<<sizeof(C1)<<endl; //类C1自己的成员和继承自B的成员,还有一个存放偏移量的虚地址,size为12cout<<sizeof(C2)<<endl; //类C2自己的成员和继承自B的成员,还有一个存放偏移量的虚地址,size为12cout<<sizeof(D)<<endl;  //继承自C1,C2,所以size为24return 0;
}
运行结果:



形式如图所示:



继承的内容就先说到这里啦,讲的不详细的还希望大家能给出建议哦。

欢迎大家来访~~吐舌头吐舌头






本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/382811.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

centOS6.5如何从启动界面直接进入命令行界面和如何从图形界面进入命令行界面

centOS6.5如何从启动界面直接进入命令行界面 编辑 /etc/inittab 将 id:5:initdefault: 修改为 id:3:initdefault: 下次重启就不启动X Window了 如何从图形界面进入命令行界面 startx

优酷解析 转载的

转自 https://blog.csdn.net/qq_39797956/article/details/88076404

【送给Git初学者】

好多人都听过Git吧&#xff0c;目前最流行的分布式版本管理系统。还有好多类似的cvs、svn&#xff08;速度慢、必须联网&#xff0c;这些是集中式版本控制系统&#xff09;..... 那么&#xff0c;它是用来干什么的呢&#xff1f;举个例子可能更好理解吧&#xff01; 比如你写…

虚拟机中的Linux安装VMware Tools的方法

虚拟机中的Linux安装VMware Tools的方法 http://www.jb51.net/softjc/189144.html 当.pl文件无法执行时 chmod install-vmware.pl./ install-vmware.pl 安装就可。 先以root身份登入。 VMware Tools所在位置&#xff1a;VMware 安装路径 \VMware\VMware Workstation\linux…

appium 设置参数

appium 配置好环境变量以后&#xff0c; 需要设置启动参数&#xff0c; 设备名称&#xff0c; 应用的一些信息主要有以下信息&#xff1a; {"platformName": "Android","platformVersion": "5.1.1","deviceName": "ee…

远程仓库

上节我们安装好了git&#xff0c;并配置好git&#xff0c;github之间的ssh。这节我们就开始用git管理我们的仓库吧。&#xff08;这节在windows下安装的git bash上给大家演示吧&#xff09; 首先&#xff0c;创建好一个仓库&#xff0c;主要步骤如下&#xff1a; 创建好仓库后…

linux根目录的意义和内容

1.du命令&#xff1a;du [选项] 文件     (1)功能该命令是显示指定文件以及下的所有文件占用系统数据块的情况&#xff0c;如果没有文件&#xff0c;默认为是当前工作目录     -a    显示所有文件对系统数据块的使用情况     -b    显示数据块大小时以字节…

c++详解【智能指针】

智能指针&#xff1f;是一个指针吗&#xff1f;这里给大家说的是&#xff0c;它不是一个指针&#xff0c;但它模拟了指针所具有的功能。那么&#xff0c;为什么要有智能指针的引入呢&#xff1f;看看下面的例子吧~ void FunTest() {int *p new int[10];FILE *pFile fopen(&qu…

python 使用 os的 popen(‘命令’) 如果命令行输出中 有中文乱码, 提示 'gbk' 无法解析的错误 解决办法

os.chdir(‘你的命令’) res os.popen(v.testcomman)print(tempstream.buffer.read().decode(encodingutf-8)&#xff09;

node.js async await 配合Promise对象使用

function getData(){return new Promise(function(resolve, reject){setTimeout(function(){var uname zhang;console.log(this is timeout);resolve(uname);}, 1000);}); } //await 配合 promiese 的 resolve 使用 就会真的等待 同步 async function test(){console.log(1);v…

c++【深度剖析shared_ptr】

shared_ptr解决了scoped_ptr管理单个对象的缺陷&#xff0c;且解决了防拷贝的问题。shared_ptr可以管理多个对象&#xff0c;并且实现了资源共享。 但是仍然存在一些问题&#xff0c;比如&#xff0c;我们熟悉的双向链表&#xff1a; struct Node { Node(const int& value…

centos重新安装yum

1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.下载新的CentOS-Base.repo 到/etc/yum.repos.d/ wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo 3. yum makecache GDB的安装 yum…

Electron 渲染进程,如何解决require is not defined的问题

mainWindow new BrowserWindow({webPreferences: {nodeIntegration: true}}) // nodeIntegration: true 加上这一句 就可以了 5.0以后默认是false

c++详解【new和delete】

说起new和delete&#xff0c;了解过c的人应该都知道吧&#xff0c;它是用来分配内存和释放内存的两个操作符。与c语言中的malloc和free类似。 c语言中使用malloc/calloc/realloc/free进行动态内存分配&#xff0c;malloc/calloc/realloc用来在堆上分配空间&#xff0c;free将申…

vim 的配置文件 #vim ~/.vimrc

set hlsearch set backspace2 set nu set showmode set ruler set autoindent syntax on set smartindent set tabstop4 set shiftwidth4 set expandtab imap { {}iV

关于tornado的异步耗时操作假设

tornado 如果遇到耗时的操作&#xff0c;可不可以这样 把耗时操作放在一个由 python进程池维护的 pool中&#xff0c; 用 webapi封装起来&#xff0c; 然后tornado 接收客户端请求后&#xff0c;遇到耗时操作就 与访问另一个webapi &#xff0c; webapi去调用进程池 这种模型不…

Stack/Queue与Vector/List的联系

Vector:(顺序表【数组存储】) 1.当申请的空间不足的时候&#xff0c;需要再次开辟一块更大的空间&#xff0c;并把值拷过去。 2.对于尾删和尾插是比较方便的&#xff0c;只需要改动最后一个元素即可。不会改动原有的空间。适用于多次重复的对尾部插删。 3.顺序存储&#xff…

利用SetConsoleTextAttribute函数设置控制台颜色

原文出处&#xff1a; https://blog.csdn.net/odaynot/article/details/7722240 混合颜色 #include <windows.h> #include <iostream> using namespace std;int main() {HANDLE hOut;hOut GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(hOut,FOREG…

用栈实现后缀表达式求解问题

一、问题概述&#xff1a; 人们经常书写的数学表达式属于中缀表达式&#xff0c;今天要解决的是&#xff0c;后缀表达式的求解问题。 如下图分别为举例的中缀表达式和后缀表达式&#xff1a; 二、解决思路 我们用栈存储后缀表达式中的数据部分&#xff0c;当遇到操作符时就取…