C++---class inheritance and derivation

1. Derivation of the class

1) Definition:
In the inheritance relationship, the inherited class is called the base class (or parent class), and the new class defined through the inheritance relationship is called the derived class (or subclass).
2) Features: A
derived class can not only inherit the members of the original class, but also add new members;
3) The general form of inheritance: For
example, there is already a class

class employee{
    
    
	//....
}

Need class tearcher to inherit this class;
then:

class teacher:public employee{
    
    
	//.....
}

4) Subclasses can rewrite the member methods of the parent class, requiring the function return type, function name, and parameter list to be consistent;
code:

#include<stdio.h>
#include<iostream>
using namespace std;

class employee{
    
    //父类 
protected:
	char *name;
	int age;
	char sex;
public:
	//....
	void print(){
    
    //父类中的一个方法 
		cout<<"name:"<<name<<endl;
		cout<<"age:"<<age<<endl;
		cout<<"sex:"<<sex<<endl;
	}
};

class teacher:public employee//子类 继承父类,父类的成员自动被继承 
{
    
    
private:
	char *title;
	//...
public:
	//...		
	void print(){
    
    
		cout<<"name:"<<name<<endl;
		cout<<"age:"<<age<<endl;
		cout<<"sex:"<<sex<<endl;
		cout<<"title:"<<title<<endl;
	}
};

int main(){
    
    
	//... 
	
	
	return 0;
}

2. The relationship between the derived class and the base class

The definition of derived classes:

class 派生类名:[继承方式] 基类名

The inheritance method of C++ class: private, protected, public inheritance;
different inheritance methods can make the derived class have different characteristics when inheriting the members of the base class.
1.
Private inheritance:
the public and protected members of the parent class become private in the child class;
2.
Public inheritance:
the member attributes in the parent class remain unchanged in the child class;
3.
Protected inheritance:
the members in the parent class In the subclass, it becomes protected;
4.
No matter what inheritance, the private members of the parent class cannot be inherited! ! ! Reflects the idea of ​​data encapsulation;
5.
The default inheritance method of a class defined by the class keyword is private, and the default inheritance method of a class defined by struct is public. Be consistent with the default access method in the class. Record it.
**
code:

#include<stdio.h>
#include<iostream>

using namespace std;


class Base{
    
    
	private:
		int a,b;
	public:
		int getA(){
    
    
			return a;
		}
};

class Derived:public Base{
    
    
	public:
		int getD(){
    
    //调用父类的public方法; 
			getA();
		}		
};

int main()
{
    
    
	
	//...	
	return 0;
}

3. The constructor and destructor of the derived class

**
1) The constructor of the derived class:
when creating the object of the derived class, because the object of the derived class contains the data members of the class, the constructor of the derived class must initialize the data members of its own definition, and must also perform the function of the base class The data members in are initialized. In other words, the constructor of the derived class is responsible for calling the constructor of the base class.
2) The general form of the derived class constructor:

Derived class constructor function name (parameter list): base class constructor function name (parameter list) {...}

Use the initialization list to initialize the data members of the base class.
code:

#include<iostream>

using namespace std;

class Base{
    
    
	int x,y;
public:
	Base(int a,int b){
    
    
		x=a;
		y=b;
		cout<<"construct Base"<<endl;
	}
	~Base(){
    
    
		cout<<"destruct Base"<<endl;
	}
};

class SonClass:public Base{
    
    
		
public:
	SonClass(int z):Base(z,z)
	{
    
    
		cout<<"construct SonClass"<<endl;	
	}	
	~SonClass(){
    
    
		cout<<"destruct SonClass"<<endl;
	}
};

int main()
{
    
    
	SonClass oo(8);
	return 0;
} 

operation result:

construct Base
construct SonClass
destruct SonClass
destruct Base

1. Subclass constructor execution steps: first execute the parent class constructor, and then execute your own constructor;
2. Subclass destructor execution process: first execute your own destructor, and then execute the parent class's destructor;

This idea is similar to the stack, first-in-last-out principle;
**

4. Multiple inheritance and virtual base classes

**
1. Single inheritance:
a derived class has only one parent class, this form is single inheritance, and its class hierarchy can be expressed as a tree.
2. Multiple inheritance:
C++ allows a subclass to have multiple parent classes, which is called multiple inheritance. The base classes and derived classes of multiple inheritance can be organized into a directed graph structure.
(The declaration method of multiple inheritance derived classes is similar to that of single inheritance, except that it is a list at the time of declaration);

class Derived class: <derived method> base class 1, <derived method> base class 2,...,<derived method> base class n
{ //... }

3. Virtual base class:
member copy of a base class that can solve multiple inheritance;
implementation method:
class Derived class: virtual <derived method> Base class...{ //... } code:


#include<iostream>

using namespace std;

class A{
    
    
public:
	int x,y;
};

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

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

class D:public B,public C{
    
    
	
};
//D d;此时可以直接使用d.x,不再有二义性! 
int main()
{
    
    
	
	D d;
	cin>>d.x;
	cin>>d.y;
	cout<<"x="<<d.x<<endl;
	cout<<"y="<<d.y<<endl;
	return 0;
}

Multiple inheritance will lead to the problem of diamond inheritance (ambiguity), and the use of virtual base classes can solve it!

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/114990610