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;         //error
	d._pro = 20;         //error
	d._pub = 30;         //error
	d.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;        //error
	d._pro = 20;        //error
	d._pub = 30;        //error
	d.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;           //error
	d._pro = 20;           //error
	d._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,无法识别Student
	return 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为4
	cout<<sizeof(C1)<<endl;          //打印类C1中的成员,并且继承了类B,所以它的size为8
	cout<<sizeof(C2)<<endl;          //打印类C2中的成员,并且继承了类B,所以它的size为8
	cout<<sizeof(D)<<endl;           //打印类D的成员,并且继承了类C1,C2,所以它的size为20
	return 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为4
     cout<<sizeof(C1)<<endl; //类C1自己的成员和继承自B的成员,还有一个存放偏移量的虚地址,size为12
     cout<<sizeof(C2)<<endl; //类C2自己的成员和继承自B的成员,还有一个存放偏移量的虚地址,size为12
     cout<<sizeof(D)<<endl;  //继承自C1,C2,所以size为24
     return 0;
}
运行结果:



形式如图所示:



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

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






  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值