A brief discussion on C++ | Class inheritance

 

Introduction: 

Inheritance is one of the three major characteristics of object-oriented. There are special relationships between some classes, such as the following figure:



We found that when defining these classes, the members of the lower level not only have the common features of the upper level, but also have their own characteristics.
At this time we can consider using inheritance technology to reduce duplicate code.

1. Inherit basic syntax 

Syntax: class subclass: inheritance method parent class 

Advantages: Reduce duplicate code

Subclasses are also called derived classes, and parent classes are also called base classes.

Code: 

#include <iostream>
using namespace std;
class person {   //定义person基类
public:
	int age;
	int height;
	int weight;
	string name;

	void show() {
		cout << "age=" << age << endl;
		cout << "height=" << height << endl;
		cout << "weight=" << weight << endl;
		cout << "name=" << name << endl;
	}
};

class women :public person {
public:
	string xingbie;
	void shou_x() {
		cout << "xingbie=" << xingbie<<endl;
	}
};

void fun() {
	women p;
	p.age = 18;
	p.height = 180;
	p.name = "tom";
	p.weight = 160;
	p.xingbie = "女";
	p.show();
	p.shou_x();
}
int main() {
	fun();
	return 0;
}

 The members in the derived class include two parts:
1. The class is inherited from the base class, and the class is the member added by itself.
2. Those inherited from the base class reflect its commonality, while the newly added members reflect its individuality.

 2. Inheritance method

 Inheritance method:

public: public inheritance

protected: protected inheritance

private: private inheritance

Code:

#include <iostream>
using namespace std;
class father {
public:
	int A;
protected:
	int B;
private:
	int C;
};

class son1 :public father {    //公有继承
public:
	void fun() {
		cout << "A=" << A << endl;  //公有变为公有
		cout << "B=" << B << endl;  //保护变为保护
		//cout << "C=" << C << endl;//私有不可访问
	}
};

class son2 :protected father {    //保护继承
public:
	void fun() {
		cout << "A=" << A << endl;  //公有变为保护
		cout << "B=" << B << endl;  //保护变为保护
		//cout << "C=" << C << endl;//私有不可访问
	}
};


class son3 :private father {    //私有继承
public:
	void fun() {
		cout << "A=" << A << endl;  //公有变为私有
		cout << "B=" << B << endl;  //保护变为私有
		//cout << "C=" << C << endl;//私有不可访问
	}
};


void fun() {
	son1 p1;
	son2 p2;
	son3 p3;
	p1.A = 10;
	p1.fun();
}
int main() {
	fun();
	return 0;
}

 All private members are missing, the public remains unchanged, the members are preserved, and the private members remain private.

 3. Object model in inheritance

In inheritance, although the private variables of the base class cannot be accessed, they have been inherited and are just hidden.

 Code:

#include <iostream>
using namespace std;
class father {
public:
	int A;
protected:
	int B;
private:
	int C;
};
class son :private father {
public:
	int age;
};
int main() {
	cout << sizeof(son) << endl;
	return 0;
}

4. Inheritance construction and destruction sequence

When a subclass inherits a parent class, a parent class will be created first. The order of destruction is exactly the opposite of the order of construction.

 Code:

#include <iostream>
using namespace std;
class father {
public:
	father() {
		cout << "father的构造哈数" << endl;
	}
	~father() {
		cout << "father的析构函数" << endl;
	}
};
class son :public father {
public:
	son() {
		cout << "son的构造函数" << endl;
	}
	~son() {
		cout << "son的析构函数" << endl;
	}
};
void fun() {
	son a;
}
int main() {
	fun();
	return 0;
}

5. Inherit the processing method of members with the same name

When the member variables and member functions in the base class have the same name as the member variables and functions of the derived class, the base class calls the member functions and member variables of the derived class by default. To call member functions and member variables of the base class, you need to add the scope of the base class;

Code:

#include <iostream>
using namespace std;
class father {
public:
	int A;
	father() {
		A = 999;
	}
	void fun() {
		cout << "father的fun调用" << endl;
	}
	void fun(int a) {
		cout << "father的fun调用" << ' ' << a << endl;
	}
};
class son :public father{
public:
	int A;
	son() {
		A = 99;
	}
	void fun() {
		cout << "son的fun调用" << endl;
	}
	void fun(int a) {
		cout << "father的fun调用" << ' ' << a << endl;
	}
};
void fun() {
	son p;
	cout << "son" << ' ' << p.A << endl;
	cout << "father" << ' ' << p.father::A << endl;
	p.fun();
	p.fun(11);
	p.father::fun();
	p.father::fun(90);
}
int main() {
	fun();
	return 0;
}

 If a member function with the same name as that of the base class appears in the derived class, the derived class will hide all member functions of the base class with the same name. It should be noted that situations that could have been distinguished by function overloading but were hidden by derived classes:

Code: 

#include <iostream>
using namespace std;
class father {
public:
	int A;
	father() {
		A = 999;
	}
	
	void fun() {
		cout << "father的fun调用" << endl;
	}
	void fun(int a,int b) {
		cout << "father的fun调用" << ' ' << a << endl;
	}
};
class son :public father{
public:
	int A;
	son() {
		A = 99;
	}
	void fun() {
		cout << "son的fun调用" << endl;
	}
};
void fun() {
	son p;
	cout << "son" << ' ' << p.A << endl;
	cout << "father" << ' ' << p.father::A << endl;
	p.fun();
	p.fun();
	p.father::fun();
	//p.fun(90,89);//报错
	p.father::fun(89, 123);
}
int main() {
	fun();
	return 0;
}

6. Inherit the processing method of static members with the same name

 When member variables and functions with the same name are static, you need to pay attention to how to call them through the class name.

Code:

#include <iostream>
using namespace std;
class father {
public:
	static int A;
	static void fun() {
		cout << "father的fun调用" << endl;
	}
	void fun(int a) {
		cout << "father的fun调用" << a<<' ' << endl;
	}
};
int father::A=10;   //static成员变量,类内声明,类外定义
class son :public father {
public:
	static int A;
	static void fun() {
		cout << "son的fun调用" << endl;
	}
};
int son::A = 20;
void fun() {
	son p;
	//1.利用对象调用成员变量和函数
	cout << p.A << endl;
	cout << p.father::A << endl;
	p.fun();
	p.father::fun();

	cout << "**************************" << endl;
	//2.利用类名调用成员变量和函数
	cout << son::A << endl;
	cout << father::A << endl;
	cout << son::father::A << endl;   

	son::fun();
	father::fun();
	son::father::fun();    
	//father::fun(10);  //报错,此方法只能调用static类型


}
int main() {
	fun();
	return 0;
}

7. Multiple inheritance syntax

C++ allows one class to inherit from multiple classes


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 actual C++ development, multiple inheritance is not recommended.

 Code:

#include <iostream>
using namespace std;
class base1 {
public:
	int A;
	base1() {
		A = 10;
		cout << "base1构造函数调用" << endl;
	}
};


class base2 {
public:
	int A;
	base2() {
		A = 20;
		cout << "base2构造函数调用" << endl;
	}
};

class son :public base1, public base2 {
public:
	int B;
	int C;
	son() {
		B = 100;
		C = 200;
		cout << "son构造函数调用" << endl;
	}
};

void fun() {
	son b;
	cout << b.base1::A << endl;
	cout << b.base2::A << endl;
	cout << b.B << endl;
	cout << b.C << endl;
}
int main() {
	fun();
	return 0;
}

 

 Pay attention to the calling order of the constructor. Father 1 calls it first, then Father 2 calls it, and finally son calls it.

Summary: In multiple inheritance, if the same name appears in the parent class, a scope must be added when using the subclass.

8. Diamond inheritance

Diamond inheritance concept:
Two derived classes inherit the same base class,
and a class inherits two derived classes at the same time.
This kind of inheritance is called diamond inheritance, or diamond inheritance.

1. Sheep inherits the data of animals, and camels also inherit data of animals. When alpacas use data, ambiguity will occur.
2. Alpaca inherits two copies of the animal's data. In fact, we should be clear that we only need one copy of this data.

 Code:

#include <iostream>
using namespace std;
class dongwu {
public:
	int A;
};
class yang :public dongwu {};
class tuo:public dongwu {};
class son :public tuo, public yang {};
void fun() {
	son p;
	p.yang::A = 100;
	p.tuo::A = 200;
	//当菱形继承,两个父亲拥有相同名的成员时,要加作用域加以区分;
	cout << p.yang::A << endl;
	cout << p.tuo::A << endl;
}
int main() {
	fun();
	return 0;
}

The inheritance situation at this time is like this. 

 Using virtual inheritance can solve the problem of diamond inheritance (two copies of the same data, a waste of memory)

Code:

#include <iostream>
using namespace std;
class dongwu {
public:
	int A;
};
//virtual虚继承,dongwu称为虚基类
class yang :virtual public dongwu {};
class tuo:virtual public dongwu {};
class son :public tuo, public yang {};
void fun() {
	son p;
	yang p1;
	tuo p2;
	p.yang::A = 100;
	p.tuo::A = 200;
	p1.A = 90;
	p2.A = 190;
	//当菱形继承,两个父亲拥有相同名的成员时,要加作用域加以区分;
	cout << p.yang::A << endl;
	cout << p.tuo::A << endl;
	cout << p1.A << endl;
	cout << p2.A << endl;

}
int main() {
	fun();
	return 0;
}

 When inheriting, only one copy is inherited. At this time, son inherits the pointers of two virtual base class tables, and the virtual base class table stores the offset to the member.

Summary:
·The main problem caused by diamond inheritance is that subclasses inherit two copies of the same data, resulting in a waste of resources and meaninglessness. ·The problem of diamond inheritance can be solved by using virtual inheritance.

Guess you like

Origin blog.csdn.net/m0_73731708/article/details/132927486
Recommended