Detailed explanation of c++ inheritance knowledge (1)

Inheritance benefits:
Reduce duplication of code
Syntax:
class subclass (derived class): inheritance method parent class (base class)
inheritance method:
public inheritance, protected inheritance, private inheritance

#include<iostream>
using namespace std;
/*
class baise  
{
public:
	int ma;
protected:
	int mb;
private:
	int mc;
};
//定义一个父类,里面有3种
//儿子以公共方式继承父类
class son :public baise
{
//注意:
	//父类中所有非静态成员属性会被子类继承下去
	//父类中私有成员属性是被编译器·隐藏起来了,但是确实是被继承下来了
	int mb;
	void fun()
	{
		ma = 100; // 父类公共内容  以公共继承方式,类内 类外可以访问,也是公共权限
		mb = 100; // 父类保护内容  以公共继承方式,类内可以访问,类外不能访问,也是保护权限
		//mc = 100;父类的私有内容,以公共继承方式,访问不到
	}
};
void test01()
{
	son s1;
	s1.ma = 100;
	//s1.mb = 100; 保护权限,类外不能访问

	cout << sizeof(s1) << endl;
}

//Inherit the parent class with protected permissions

class son2 :protected baise
{
    
    
	void func()
	{
    
    
		ma = 100;//父类公共内容  以保护继承方式,类内可以访问,类外不能,是保护权限
		mb = 100; //父类保护内容  以保护继承方式,类内可以访问,类外不能,是保护权限
		//mc = 100;父类的私有内容,以保护继承方式,访问不到
	}
};
void test02()
{
    
    
	son2 s2;
	//以保护权限继承,类外都不能访问
	/*s2.ma = 100;
	* s2.mb=100;
	*/
/*
}

//以私有权限继承父类
class son3 :private baise
{
	void func()
	{
		ma = 100;//父类公共内容  以私有继承方式,类内可以访问,类外不能,是私有权限
		mb = 100; //父类保护内容  以私有继承方式,类内可以访问,类外不能,是私有权限
		//mc = 100;父类的私有内容,以保护继承方式,访问不到
	}
};
void test03()
{
	son3 s3;
	//s2.ma = 100;
	//s2.mb=100;

}
*/

//Process members with the same name in inheritance

class base
{
    
    
public:
	base()
	{
    
    
		ma = 100;
	}
	int ma;
};
class sonn :public base
{
    
    
public:
	sonn()
	{
    
    
		ma = 10;
	}
	int ma;
};
//同名成员
void testt()
{
    
    
	sonn s;
	cout << "子类中的成员.访问" << s.ma << endl;
	cout << "父类中的成员base::作用域访问" << s.base::ma << endl;
}

//function with the same name

  • If not in the subclass, only in the parent class, the direct call will call the function in the parent class

  • Call directly, the call is the member of the same name in the subclass

  • If a member function with the same name as the parent class appears in the subclass, all functions with the same name in the parent class will be hidden, unless the scope is added

  • Summarize:

  • Subclass objects can directly access members of the same name in the subclass

  • Subclass objects plus scope can access members with the same name in the parent class

  • When the subclass and the parent class have a member function with the same name, the subclass will hide the member function with the same name in the parent class, and add scope to access the function with the same name in the parent class

//Static member processing with the same name
//Static member attribute with the same name

class person
{
    
    
public:
	static int ma;
	static void func()
	{
    
    
		cout << "person的成员函数的调用" << endl;
	}
	static void func(int a)
	{
    
    
		cout << "person(int a)的成员函数的调用" << endl;
	}
};
int person::ma = 100;
class personson :public person
{
    
    
public:
	static int ma;
	static void func()
	{
    
    
		cout << "personson的成员函数的调用" << endl;
	}
};

int personson::ma = 200;

void test01()
{
    
    
	//1.通过对象访问
	cout << "1.通过对象访问" << endl;
	personson p; //实例化
	cout <<"personson下:"<< p.ma << endl;
    cout << "person下"<<p.person::ma << endl;
	
	//2.通过类名访问
	cout << "2.通过类名访问" << endl;
	//第一个::代表通过类名方式访问
	//第二个::代表访问父类作用域下
	cout<<"person下"<< personson::person::ma << endl;
	cout<<"personson下" << personson::ma << endl;
}

//Static member function with the same name

void test02()
{
    
    
	//通过对象访问
	personson p;
	p.func();
	p.person::func();

	//通过类名方式访问
	personson::person::func();
	personson::func();
	//错误personson::func(10); 在personson下没有类似void func(int a)的函数
	personson::person::func(10);
}

//Multiple inheritance syntax
Syntax: class subclass: inheritance method parent class 1, inheritance method parent class 2,...

  • In multiple inheritance, if the same name appears in the parent class, the scope must be added to distinguish it when used in the subclass
class base1
{
    
    
public:
	base1()
	{
    
    
		ma = 100;

	}
	int ma;
};
class base2
{
    
    
public:
	base2()
	{
    
    
		mb = 200;
		ma = 200;
	}
	int mb;
	int ma;
};

//Now we need a subclass to inherit base1 base2

class base3 :public base1, public base2
{
    
    
public:
	base3()
	{
    
    
		mc = 300;
		md = 400;
	}
	int mc;
	int md;
};
void test03()
{
    
    
	base3 b;
	cout << sizeof(b) << endl;
	//错误-》因为名字重复要用作用域加以区分cout << b.ma << endl;
	cout << "base1下" << b.base1::ma << endl;
	cout << "base2下" << b.base2::ma << endl;
}

//Using virtual inheritance can solve the diamond inheritance problem
//The main problem is that subclasses inherit two copies of the same data, resulting in waste of resources and meaninglessness

class animal
{
    
    
public:
	int age;
};
//加入virtual后变成虚继承,animal叫虚基类
//从而统一
class monkey:virtual public animal{
    
    };
class xingxing:virtual public animal{
    
    };
//不管猴子还是猩猩,都是动物,所以虚继承animal类
class persons:public monkey,public xingxing{
    
    };
/*
* 对象模型:
* class persons   size(8):
        +---
 0      | +--- (base class monkey)
 0      | | +--- (base class animal)
 0      | | | age
        | | +---
        | +---
 4      | +--- (base class xingxing)
 4      | | +--- (base class animal)
 4      | | | age
        | | +---
        | +---
        +---
*/


//虚继承之后对象模型:
/*
* class persons   size(12):
        +---
 0      | +--- (base class monkey)
 0      | | {vbptr}
        | +---
 4      | +--- (base class xingxing)
 4      | | {vbptr}
        | +---
        +---
        +--- (virtual base animal)
 8      | age
        +---

persons::$vbtable@monkey@:
 0      | 0
 1      | 8 (personsd(monkey+0)animal)

persons::$vbtable@xingxing@:
 0      | 0
 1      | 4 (personsd(xingxing+0)animal)
vbi:       class  offset o.vbptr  o.vbte fVtorDisp
          animal       8       0       4 0
*/

/*

  • explain:
  • vbptr virtual base class pointer
  • v stands for virtual
  • b stands for base
  • ptr stands for pointer
  • 8 | age
  • 0 | ±-- (base class monkey)
    0 | | {vbptr}
    persons::$vbtable@monkey@:
    0 | 0
    1 | 8 (personsd(monkey+0)animal)

The offset is 8 0+8=8

4 | ±-- (base class xingxing)
4 | | {vbptr}
persons::$vbtable@xingxing@:
0 | 0
1 | 4 (personsd(xingxing+0)animal)

The offset is 4 4+4=8
so the same data will be found, only one copy
*/

void test04()
{
    
    
	persons p;
	p.monkey::age = 100;//monkey::作用域下
	p.xingxing::age = 200;//xingxing::作用域下
	cout << p.monkey::age << endl;
	cout << p.xingxing::age << endl;
	cout << p.age << endl;
}
int main()
{
    
    
	//testt();
	//test01();
	//test02();
	//test03();
	test04();
	system("pause");
	return 0;
}

When there are protected permissions and private permissions in the parent class, will it be inherited to the subclass?
Logically speaking, all non-static member properties in the parent class will be inherited by subclasses, so the private member properties in the parent class are indeed inherited, but they are hidden by the compiler.

You can use the development command prompt tool to view the object and model
(clearer and clearer).
First, jump to the disk at the location, such as D: disk, etc.
cd to the file path at the specified location (see the file name you wrote) and
enter
cl / d1 reportSingleClassLayout class name file name

Finally, let me explain:
what is the order of construction and destruction in inheritance? :
Construct the parent class first, and then construct the subclass.
Destruct the subclass first, and then destruct the parent class.

Guess you like

Origin blog.csdn.net/2202_75623950/article/details/130835858