The concept of inheritance in C++

Table of contents

1. Introduction to inheritance

2. Inheritance method

          2.1 Composition of derived classes

         2.2 Inheritance method of derived classes

3. Object model in inheritance

4. The order of construction and destruction in inheritance

5. Inherit the processing method of the same name

        5.1 Ordinary members

        5.2 Static members

6. Multiple inheritance


1. Introduction to inheritance

What originally existed was the base class, also called the parent class. A new data type defined using an existing data type is called a derived class, also called a subclass.

The base class defines the members that are common to all subclasses of the class, and the derived class defines the members that are unique to each class.
There are three different inheritance methods for derived classes in C++, public, protect, and private. (Default is private)
 

Single inheritance

The format of single inheritance is to derive subclasses from the same base class.

class Father{};
class Son1:public Father{}; // public 方式继承
class Son2 :protected Father{}; // protected 方式继承
class Son3 :private Father{}; // private 方式继承
multiple inheritance

Multiple inheritance is a derived class that derives from multiple base classes.

class Base1{};
class Base2{};
class SubMulBase :public Base1, public Base2{};

2. Inheritance method

 2.1 Composition of derived classes

A derived class consists of two parts, one is the attributes inherited from the parent class, and the other is the attributes added to the current class.

class Base
{
public:
	int m_A;

};

class SubBase :public Base
{
public :
	int m_B;
private:
	int m_C;
};

 2.2 Inheritance method of derived classes

grammar:

class 子类: 继承方式 父类

There are three inheritance methods:

  • public inheritance
  • protected inheritance
  • Private inheritance:
    The scope of each inherited attribute in the derived derived class (the same is true for methods).
class Father
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};

class Son:protectedFather
{
};

Then, the attributes in the subclass at this time are actually: (private ones cannot be accessed). 

class Son:protectedFather
{
protected:
	int m_A;
	int m_B;
};


3. Object model in inheritance  

 

class Base
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C; //私有成员只是被隐藏了,但是还是会继承下去
};

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

void test01()
{
	cout << "sizeof Son = " << sizeof(Son) << endl;
}

Output result: 

 

 

 

4. The order of construction and destruction in inheritance

class Base 
{
public:
	Base()
	{
		cout << "Base构造函数!" << endl;
	}
	~Base()
	{
		cout << "Base析构函数!" << endl;
	}
};

class Son : public Base
{
public:
	Son()
	{
		cout << "Son构造函数!" << endl;
	}
	~Son()
	{
		cout << "Son析构函数!" << endl;
	}

};

 

 

5. Inherit the same name processing method

 5.1 Ordinary members

  • To access members of the subclass with the same name, you can access them directly
  • Accessing members of the parent class with the same name requires adding a scope
class Base {
public:
	Base()
	{
		m_A = 100;
	}

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

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

public:
	int m_A;
};


class Son : public Base {
public:
	Son()
	{
		m_A = 200;
	}

	//当子类与父类拥有同名的成员函数,子类会隐藏父类中所有版本的同名成员函数
	//如果想访问父类中被隐藏的同名成员函数,需要加父类的作用域
	void func()
	{
		cout << "Son - func()调用" << endl;
	}
public:
	int m_A;
};

void test01()
{
	Son s;

	cout << "Son下的m_A = " << s.m_A << endl;
	cout << "Base下的m_A = " << s.Base::m_A << endl;

	s.func();
	s.Base::func();
	s.Base::func(10);

}
int main() {

	test01();

	system("pause");
	return EXIT_SUCCESS;
}

 

Summarize:

  1. Subclass objects can directly access members with the same name in the subclass
  2. Subclass objects plus scope can access members of the parent class with the same name
  3. When a subclass and a 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. Adding a scope can access the function with the same name in the parent class.

 

5.2 Static members

  • To access members of the subclass with the same name, you can access them directly
  • Accessing members of the parent class with the same name requires adding a scope
class Base {
public:
	static void func()
	{
		cout << "Base - static void func()" << endl;
	}
	static void func(int a)
	{
		cout << "Base - static void func(int a)" << endl;
	}

	static int m_A;
};

int Base::m_A = 100;

class Son : public Base {
public:
	static void func()
	{
		cout << "Son - static void func()" << endl;
	}
	static int m_A;
};

int Son::m_A = 200;

//同名成员属性
void test01()
{
	//通过对象访问
	cout << "通过对象访问: " << endl;
	Son s;
	cout << "Son  下 m_A = " << s.m_A << endl;
	cout << "Base 下 m_A = " << s.Base::m_A << endl;

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

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

	cout << "通过类名访问: " << endl;
	Son::func();
	Son::Base::func();
	//出现同名,子类会隐藏掉父类中所有同名成员函数,需要加作作用域访问
	Son::Base::func(100);
}
int main() {

	//test01();
	test02();

	system("pause");

	return 0;
}

6. Multiple inheritance

Syntax: class subclass: inheritance method parent class 1, inheritance method parent class 2...

Multiple inheritance may cause members with the same name to appear in the parent class, and scope differentiation is required.
In multiple inheritance, if members with the same name appear in the parent class, scopes must be added when using the subclass.
 

 

 

Guess you like

Origin blog.csdn.net/weixin_63246738/article/details/131890682