C++: Inheritance

insert image description here

1. One of the three major characteristics of object-oriented: inheritance

  • inheritance isHierarchy at the class domain level, the purpose of its syntax design is toImprove code reusability(Inheritance is the reuse of the class as a whole) at the same time make the project designHierarchy is clearer, the existing scenario is as follows: For example, now we need to design two classes: Student class and Teacher class, but these two classes contain some same members (such as _name, _age and some same interfaces, etc.), so we can Abstract a Person class (the class contains the same members of the Student class and the Teacher class), and then let the Student class and the Teacher class inherit the Person class to form the following class domain hierarchy:
    insert image description here
class Person
{
    
    
public:
	void Print()
	{
    
    
		cout << "name:" << _name << endl;
		cout << "age:" << _age << endl;
	}
private:
	string _name = "peter";
	int _age = 10;
};

class Student : public Person
{
    
    
protected:
	int _stuid; // 学号
};

class Teacher : public Person
{
    
    
protected:
	int _jobid; // 工号
};
  • After the inheritance relationship is determined,All members of the parent class will be inherited to the subclass.
  • parent class private memberexistin subclassyesinaccessible(But it still exists in the subclass, that is, it will occupy memory space when instantiated)
  • Inheritance classification andinherited class membersAccess-qualified properties in subclasses:
    insert image description here
  • It can be found that after the members of the parent class are inherited, theirAccess to restricted attribute changesFollow the law of "authority only narrows but not expands"
  • In the inheritance system, both subclasses and parent classes haveseparate scope, when members with the same name appear in the subclass and the parent class, the members of the subclass will block the access to the members of the parent class with the same namedirect interview(At this point, if you want to access the members with the same name in the parent class, you needSpecify class domain to access):
    insert image description here
  • It should be noted that if the same name appears in the parent class and the subclassmember function, the above-mentionedshield relationshipanddoes not constitute function overloading

Some other syntax rules in inheritance

  1. Friend relationships cannot be inherited, ieA friend of a parent class cannot become a friend of a child class
  2. If a static member is defined in the parent class stactic, then inOnly one instance of this member exists in the entire inheritance hierarchy

2. Object assignment conversion between parent class and subclass

  1. subclass objectcan be assigned toobject of the parent classorparent class reference.
  2. pointer to subclass objectcan be assigned topointer to the parent object.
    insert image description here
  • The above assignment process is equivalent toCut out the parent class part in the subclassThe object (or reference or pointer) assigned to the parent class can be visualized as follows:
    insert image description here
  • The syntax is in the inheritance hierarchyCopy assignment between class objectswill use
  • parent objectcannot be assigned tosubclass object(or quote),pointer to the parent objectcannot be assigned topointer to subclass object.

3. Inheritance system and default member functions of classes – mind map

insert image description here

4. Memory model of inherited objects

insert image description here

  • The memory model of the Derive class object:
    insert image description here

5. Defects of Inheritance – The Diamond Inheritance Problem

  • Diamond inheritance diagram:
    insert image description here
  • class code:
class Person
{
    
    
public :
	string _name ; 
};
class Student : public Person
{
    
    
protected :
	int _num ; 
};
class Teacher : public Person
{
    
    
protected :
	int _id ; 
};
class Assistant : public Student, public Teacher
{
    
    
protected :
	string _majorCourse ; 
};
  • Diamond inheritance results inMember variables with the same name appear repeatedly in the class,causeAmbiguityandmemory space wasteThe question, the memory model of the inheritance relationship above:
    insert image description here
  • In order to solve the problem of data redundancy and ambiguity in diamond inheritance, C++ designedvirtual inheritancesyntax to make up for the defects of diamond inheritance: in the diamond inheritance relationship, inContains repeated subclassesofintermediate subclassAdd keywords before the inheritance method virtual:
    insert image description here
  • After virtualmodification, the memory model of the Assistant class will change:
    insert image description here
  • in actual engineeringAvoid designing architectures with diamond-shaped inheritance (A subclass inherits multiple parent classesshould be avoided as much as possible), which is a syntax flaw

awareness of inheritance

  • compared to directUse one class as a member variable of another class, Inheritance destroys the encapsulation between classes to a certain extent, and violates the concept of software engineering architecture design.The principle of high cohesion and low coupling(Between subclass and superclassstrong dependencies, high degree of coupling), so when one class can be directly used as a member variable of another class, it is preferredAdapter formThe reuse method, and then consider using inheritance syntax,Inheritance is only necessary when polymorphism needs to be introduced

insert image description here

Guess you like

Origin blog.csdn.net/weixin_73470348/article/details/130991763