c/c++入门教程 - 2.4.6 继承、公共继承、保护继承、私有继承、virtual虚继承(概念、语法、方式、构造和析构顺序、同名成员处理、继承同名静态成员处理、多继承语法、菱形继承、钻石继承)

目录

4.6 继承

4.6.1 继承的基本语法

4.6.2 继承方式

4.6.3 继承中的对象模型

4.6.4 继承中构造和析构顺序

4.6.5 继承同名成员处理方式

4.6.6 继承同名静态成员处理方式

4.6.7 多继承语法

4.6.8 菱形继承


4.6 继承

继承是面向对象三大特性之一。

有些类与类之间存在特殊的关系,例如下图中:

 

定义这些类时,下级别的成员除了拥有上一级的共性,还有自己的特性。

这个时候我们就可以考虑利用继承的技术,减少重复代码

 

4.6.1 继承的基本语法

例如我们看到很多网站中,都有公共的头部,公共的底部,甚至公共的左侧列表,只有中心内容不同。

接下来我们分别利用普通写法和继承的写法来实现网页中的内容,看一下继承存在的意义以及好处。

 

继承的好处:减少重复代码。

语法:class 子类 : 继承方式 父类

子类也称为派生类,父类也称为基类

派生类中的成员,包含两大部分

一类是从基类继承过来的,一类是自己增加的成员。

从基类继承过来的表现其共性,而新增的成员体现其个性。

 

4.6.2 继承方式

继承的语法:class  子类  :  继承方式  父类

class  子类  :  继承方式  父类

继承方式一共有三种

  • 公共继承。
  • 保护继承。
  • 私有继承。

 

图上的不可访问,不是不会被继承,而是被隐藏了访问不到。证明见下一小节。

#include <iostream>
using namespace std;
#include <string>

// 父类 / 基类
class Base
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};

// public公共继承
class Son1 : public Base
{
	void func()
	{
		m_A = 100;	// 父类中的公共权限成员,到子类中依然是公共权限
		m_B = 100;	// 父类中的保护权限成员,到子类中依然是保护权限
		//m_C = 100;	// 父类中的私有权限成员,子类访问不到
	}
};

void test01 (void)
{
	Son1 son;
	son.m_A = 200;	
	//son.m_B = 200;	//son对象中的m_B是保护权限,类外访问不到
}

class Son2 : protected Base
{
	void func()
	{
		m_A = 100;	// 父类中的公共权限成员,到子类中是保护权限
		m_B = 100;	// 父类中的保护权限成员,到子类中是保护权限
		//m_C = 100;	// 父类中的私有权限成员,子类访问不到
	}
};

void test02(void)
{
	Son2 son;
	//son.m_A = 200;	//son对象中的m_A是保护权限,类外访问不到
	//son.m_B = 200;	//son对象中的m_B是保护权限,类外访问不到
}

class Son3 : private Base
{
	void func()
	{
		m_A = 100;	// 父类中的公共权限成员,到子类中是私有权限
		m_B = 100;	// 父类中的保护权限成员,到子类中是私有权限
		//m_C = 100;	// 父类中的私有权限成员,子类访问不到
	}
};

void test03(void)
{
	Son3 son;
	//son.m_A = 200;	//son对象中的m_A是私有权限,类外访问不到
}

int main() {

	test01();
	test02();

	system("pause");
	return 0;
}

 

4.6.3 继承中的对象模型

问题:从父类继承过来的成员,哪些属于子类对象中。

 

#include <iostream>
using namespace std;
#include <string>

// 父类 / 基类
class Base
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};

// public公共继承
class Son1 : public Base
{
public:
	int m_D;
};

void test01 (void)
{
	Son1 son;

	// 父类中所有非静态成员属性都会被子类继承下去。
	// 父类中私有成员属性,是被编译器给隐藏了,因此是访问不到。而不是没有被继承
	cout << "Base大小为:" << sizeof(Base) << endl;
	cout << "Son1大小为:" << sizeof(Son1) << endl;
}

int main() {

	test01();

	system("pause");
	return 0;
}
-------------------------------------------------------------------------------
Base大小为:12
Son1大小为:16
请按任意键继续. . .

 

总结:子类继承父类中私有成员属性,是被编译器给隐藏了,因此是访问不到。而不是没有被继承

父类中的私有成员也是被子类继承下去了,只是由编译器给隐藏后访问不到

 

!!!可利用开发人员命令提示工具查看对象模型。

跳转程序所在盘符:C/D/E/F:

跳转文件路径:cd 具体路径

查看命令:cl /d1 reportSingleClassLayout类名 文件名

 

 

4.6.4 继承中构造和析构顺序

子类继承父类后,当创建子类对象,也会调用父类的构造函数。

问题:父类和子类的构造和析构顺序是谁先谁后?

示例:

#include <iostream>
using namespace std;
#include <string>

// 父类 / 基类
class Base
{
public:
	Base()
	{
		cout << "Base构造函数" << endl;
	}

	~Base()
	{
		cout << "Base析构函数" << endl;
	}
};

// public公共继承
class Son : public Base
{
public:
	Son()
	{
		cout << "Son构造函数" << endl;
	}

	~Son()
	{
		cout << "Son析构函数" << endl;
	}
};

void test01 (void)
{
	//Base base;
	// 继承中的构造和析构顺序如下:
	// 先构造父类,再构造子类
	// 先析构子类,再析构父类
	Son son;
}

int main() {

	test01();

	system("pause");
	return 0;
}
--------------------------------------------------------------------
Base构造函数
Son构造函数
Son析构函数
Base析构函数
请按任意键继续. . .

 

总结:继承中,先调用父类构造函数,再调用子类构造函数,析构顺序与构造相反。

 

4.6.5 继承同名成员处理方式

问题:当子类和父类出现同名的成员,如何通过子类对象,访问到子类或父类中同名的数据呢?

  • 访问子类同名成员,直接访问即可
  • 访问父类同名成员,需要加作用域

 

#include <iostream>
using namespace std;
#include <string>

// 父类 / 基类
class Base
{
public:
	Base()
	{
		m_A = 100;
	}

	void func()
	{
		cout << "Base-func调用" << endl;
	}

	void func(int a)
	{
		cout << "Base-func重载" << endl;
	}

	int m_A;
};

// public公共继承
class Son : public Base
{
public:
	Son()
	{
		m_A = 200;
	}

	void func()
	{
		cout << "Son-func调用" << endl;
	}

	int m_A;
};

void test01 (void)
{
	Son s1;
	// 直接访问,访问的是子类同名成员
	cout << s1.m_A << endl;
	// 通过作用域,访问的是父类同名成员
	cout << s1.Base::m_A << endl;

	// 出现同名成员,优先调用子类成员
	s1.func();
	// 通过作用域,可以访问父类同名成员
	s1.Base::func();

	// 如果子类中出现和父类同名的成员函数,子类的同名成员会隐藏掉父类中所有同名成员函数(包括所有重载)
	// 如果想访问到父类中被隐藏的同名成员函数,需要加作用域
	s1.Base::func(100);
}

int main() {

	test01();

	system("pause");
	return 0;
}
------------------------------------------------------------------------------
200
100
Son-func调用
Base-func调用
Base-func重载
请按任意键继续. . .

 

  • 总结:

1.子类对象可以直接访问到子类中同名成员。

2.子类对象加作用域可以访问到父类同名成员。

3.当子类与父类拥有同名的成员函数,子类会隐藏父类中同名成员函数,加作用域可以访问到父类中同名函数。

 

4.6.6 继承同名静态成员处理方式

问题:继承中同名的静态成员在子类对象上如何进行访问?

静态成员和非静态成员出现同名,处理方式一致。

  • 访问子类同名成员,直接访问即可。
  • 访问父类同名成员,需要加作用域。

示例:

#include <iostream>
using namespace std;
#include <string>

// 父类 / 基类
class Base
{
public:
	static void func()
	{
		cout << "Base-func调用" << endl;
	}
	static void func(int a)
	{
		cout << "Base-func重载" << endl;
	}
	static int m_A;
};
int Base::m_A = 100;

// public公共继承
class Son : public Base
{
public:
	static void func()
	{
		cout << "Son-func调用" << endl;
	}
	static int m_A;
};
int Son::m_A = 200;

void test01 (void)
{
	Son s1;
	// 1.通过对象访问
	cout << "通过对象访问:" << endl;
	cout << "Son  下的m_A = " << s1.m_A << endl;
	cout << "Base 下的m_A = " << s1.Base::m_A << endl;

	// 2.通过类名访问
	cout << "通过类名访问:" << endl;
	cout << "Son  下的m_A = " << Son::m_A << endl;
	cout << "Base 下的m_A = " << Son::Base::m_A << endl;
}

// 同名静态成员函数
void test02()
{
	Son s1;
	// 1.通过对象访问
	cout << "通过对象访问:" << endl;
	s1.func();
	s1.Base::func();

	// 2.通过类名访问
	cout << "通过类名访问:" << endl;
	Son::func();
	Son::Base::func();

	// 子类出现和父类同名静态成员函数,也会隐藏父类中所有同名成员函数
	// 如果想访问父类中被隐藏同名成员,需要加作用域
	Son::Base::func(100);
}

int main() {

	test01();
	test02();

	system("pause");
	return 0;
}
----------------------------------------------------------------------------
通过对象访问:
Son  下的m_A = 200
Base 下的m_A = 100
通过类名访问:
Son  下的m_A = 200
Base 下的m_A = 100
通过对象访问:
Son-func调用
Base-func调用
通过类名访问:
Son-func调用
Base-func调用
Base-func重载
请按任意键继续. . .

 

总结:同名静态成员处理方式和非静态处理方式一样,只不过有两种访问的方式(通过对象 和 通过类名)

 

4.6.7 多继承语法

C++允许一个类继承多个类

语法:

class 子类 : 继承方式 父类1, 继承方式 父类2...

多继承可能会引发父类中有同名成员出现,需要加作用域区分。

C++实际开发中不建议用多继承

#include <iostream>
using namespace std;
#include <string>

// 父类 / 基类
class Base1
{
public:
	Base1()
	{
		m_A = 100;
	}
	int m_A;
};

// 父类 / 基类
class Base2
{
public:
	Base2()
	{
		m_A = 200;
	}
	int m_A;
};

// 父类 / 基类
class Base3
{
public:
	Base3()
	{
		m_C = 300;
	}
	int m_C;
};

// 语法:class 子类 : 继承方式 父类1, 继承方式 父类2...
class Son : public Base1, public Base2, public Base3
{
public:
	Son()
	{
		m_D = 400;
	}
	int m_D;
};

void test01 (void)
{
	Son s1;
	
	//cout << s1.m_A;// 报错,要指定作用域
	cout << s1.Base1::m_A << endl;
	cout << s1.Base2::m_A << endl;
}

int main() {

	test01();

	system("pause");
	return 0;
}
-------------------------------------------------------------------
100
200
请按任意键继续. . .

 

 

4.6.8 菱形继承

菱形继承概念

两个派生类继承同一个基类。

又有某个类同时继承着两个派生类。

这种继承被称为菱形继承,或者钻石继承。

 

菱形继承 也叫 钻石继承。

利用虚继承,解决菱形继承的问题:

#include <iostream>
using namespace std;
#include <string>

// 父类 / 基类
class Animal
{
public:
	int m_Age;
};

// 利用虚继承,解决菱形继承的问题
// 继承前加virtual关键字后,变为虚继承
// 此时公共的父类Animal称为虚基类
class Sheep : virtual public Animal {};
class Camel : virtual public Animal {};
class SheepCamel : public Sheep, public Camel {};

void test01 (void)
{
	SheepCamel s1;
	s1.Sheep::m_Age = 100;
	s1.Camel::m_Age = 200;
	s1.m_Age = 300;    // 不加virtual编译会报错

	cout << s1.Sheep::m_Age << endl;
	cout << s1.Camel::m_Age << endl;
	cout << s1.m_Age << endl;    // 不加virtual编译会报错
}

int main() {

	test01();

	system("pause");
	return 0;
}

---------------------------------------------------------------------------
300
300
300
请按任意键继续. . .

 

子类不加 virtual时:

 

子类加 virtual后:

 

vbptr -> virtual base pointer,虚基类指针。会指向 vbtable,虚基类表,就是图上8的位置。

利用虚继承,解决菱形继承的问题。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值