0
点赞
收藏
分享

微信扫一扫

C++ ------>继承__02

各位好友, 本期 继续推进 继承 !

开启新领域  --->继承(多态)之前, 还需要再扩展一些 继承 --->相关应用 ~~

一 . 继承 ~~ 友元

------>友元关系 不能被继承, 即,基类友元 不能访问子类私有成员 与保护成员 !

------>代码如下 :>

//继承 ~~ 友元
//
#include <iostream>
#include <string>

using std :: cout;
using std :: endl;
using std :: string;

class Student;
class Person
{
public:
  friend void DisPlay(const Student& s, const Person& p);
  
protected:
	string _name;  
};

class Student
{
public:
  //屏蔽
  //friend void DisPlay(const Student& s, const Person& p);
protected:
	int _stuid;  
};

void DisPlay(const Student& s, const Person& p)
{
	cout << p._name << endl;
  cout << s._stuid << endl;
}

int main()
{
  Person p;
  
  Student stu;
  
  DisPlay(stu, p);
	return 0;
}

为了方便好友们, 有更好的观感体验, 现附上 彩色 代码图样 :>

C++ ------>继承__02_数据冗余探究

二 . 继承 ~~ 静态成员

------------>基类定义了 Static 静态成员, 整个的 继承体系当中 只有一个这样的 成员, 无论派生出多少个子对象, 有且只有一个 Static (静态)成员 。

---------->代码如下 :>

//静态成员 属于基类 与派生类
//派生类 仅仅只是获得了静态成员的使用权
#include <iostream>

using std :: cout;
using std :: endl;

class Person
{
public:
	Person() {++_count}
  
	static int _count = 0;
protected:
	string _name;  
};

	Person ::_count = 0;

class Student : public Person
{
protected:
	int _stuid;
};

class Graduated : public Student
{
protected:
	string _seminarCourse;
};

int main()
{
  Person p;
  
  Student st1;
  
  Student stu2;
	return 0;
}

为了方便好友们, 有更好的观感体验, 现附上 彩色 代码图样 :>

C++ ------>继承__02_继承__友元__静态成员_02

三 . 菱形继承 ~~ 菱形虚拟继承(重难点

-------->单继承 :>一个派生类只有一个基类 ;

C++ ------>继承__02_菱形虚拟继承_03

-------->多继承 :>一个派生类有两个及以上的基类

C++ ------>继承__02_二义性解决_04

-------->菱形继承 :>由多继承触发;菱形继承 ------>问题 数据冗余 ~~ 二义性 

C++ ------>继承__02_继承__友元__静态成员_05

-------->代码如下 :>

//菱形继承
//
#include <iostream>
#include <string>

using std :: cout;
using std :: endl;
using std :: string;

class Person
{
public:
  string _name;
  int _age;
};

class Student : public Person
{
protected:
		int _stuid;
};

class Doctor : public Person
{
protected:
	string _reserch_direction;		
};

class Professor : public Student, public Doctor
{
protected:
	string _majorCourse;
};

int main()
{
  Professor as;
  
  as.Person::_age;
  
  as.Doctor::_age;
  
  as._age;
	return 0;
}

为了方便好友们, 有更好的观感体验, 现附上 彩色 代码图样 :>

--------->NO 1.

C++ ------>继承__02_继承__友元__静态成员_06

--------->NO 2. (数据冗余)

//探究菱形继承__数据冗余
//
#include <iostream>

using std :: cout;
using std :: endl;

class A
{
public:
  int _a;
};

class B : virtual public A
{
public:
	int _b;
};

class C : virtual public A
{
public:
	int _c;
}

class D : public B, public C
{
public:
	int _d;
};

int main()
{
  D d;
  
  d.B :: _a = 1;
  d.C :: _a = 2;
  
  d._b = 3;
  d._c = 4;
  d._d = 5;
  
  d._a = 0;
	return 0;
}

--------->调试环节 :>

C++ ------>继承__02_菱形虚拟继承_07

--------->NO 1.

C++ ------>继承__02_二义性解决_08

--------->NO 2.

C++ ------>继承__02_继承__友元__静态成员_09

--------->扩展 _a 指定大小 :>

--------->NO 1.

C++ ------>继承__02_菱形虚拟继承_10

--------->NO 2.注意 :>B, C 对象 “腰部”位置添加了 虚拟继承 标识符 Virtual

C++ ------>继承__02_菱形虚拟继承_11

------->数据冗余  --->底层原因 :>

------->菱形虚拟继承 :>

C++ ------>继承__02_菱形虚拟继承_12

------->小端机__验证  --->相对距离 :>

------->虚机表 (寻找偏移量) :>

C++ ------>继承__02_数据冗余探究_13

------->小结 :>

A . 用指针 指向_地址的好处

由于一个基类可以有多个对象, 并且每一个对象都是有地址的

这个时候,指针指向 基类预留空间的起始位置即可 ,可以有效避免 一个基类的空间内存储多个对象的地址

B . 偏移量的意义 :>

编译器 可以更方便__切割 ,切片  --->寻找位置

只需要进行 相加偏移量即可, 不需要刻意进行区分 ; 但是 编译器 寻找__派生类 ~~ 基类 --->共同拥有的对象 时候, 会在一定程度上有性能损失

各位好友, 本期内容 已完结 !

下一模块 ,要掌握 一块硬骨头” ---->继承下 ,多态 ! 敬请期待 😊😊


举报

相关推荐

c++ 继承

C++ | 继承

c++(继承)

C++~继承

C++——继承

【C++】继承

0 条评论