[C ++] refresher (vii) Detailed inheritance in C ++ classes

Comments on the article link classes and objects in C ++: https://blog.csdn.net/qq_38962621/article/details/100104442

Class inheritance syntax

Class inheritance allows a new class generated by way of inheritance, class inherits from the base class , the class Chengzi Ji following classes become a derived class, the class inherits the following wording:

class derivedClass : public/protect/private baseClass
{
// statements
}

There qualifier before the base class for a visit, do not write when the default is private, but we mainly use public, also known as public inheritance, three different access qualifier difference:

  • Public inheritance ( public): When a class is derived from the time the public base class, public members of the base class is derived public members of the class, protected members of the base class is derived to protect members of the class, the private members of the base class can not be directly derived class access , but can be accessed by the public and protected members call the base class
  • Protected inheritance ( protecte): When a class derived from the base class protection, the public and protected members of the base class will become protected members of the derived class
  • Private inheritance ( private): When a class is derived from a base class selfishness, the public and protected members of the base class will become private members of the derived class

For example, we define a base class and then define a derived class:

class baseClass
{
private:
    baseInt;
    baseDouble;
public:
    baseClass(const int pInt = 1, cosnt double pDouble = 2.5);
    int function(double d);
}
 
baseClass :: baseClass(const int pInt = 1, cosnt double pDouble = 2.5)
{
    baseInt = pInt;
    baseDouble = pDouble;
}
 
baseClass :: function(double d)
{
    cout << d;
    return 1;
}
 
 
class derivedClass : public baseClass
{
private:
	derivedChar;
public:
    derivedClass(const int pInt, const double pDouble, const char pChar);
    void anotherFunction();
}
 
derivedClass :: derivedClass(const int pInt, const double pDouble, const char pChar) : baseClass(pInt,pDouble)
{
	derivedChar = pChar;
}
 
void derivedClass :: anotherFunction()
{
	cout << "I am the derived class.";
}

From this example, we see, the base class has two private variables, baseIntand baseDouble, two methods, a constructor is used to assign two private variables, a number for charging. Following Chengzi Ji class derived class, so the derived class derivedClassto have two public methods of the base class, but it can not access two private variables of the base class. The derived class has its own definition of a new private variable, and has its own constructor and one for the function of the number of charge. Inherited visible role is to organize and easily reuse and manage project design code.

The relationship between base and derived classes

Inheriting class is "is-a" relationship, or a "is-a-kind-of", i.e., a derived object is a base class object.

  • Derived classes can call the base class protectedand publicmodify the member variables and methods, and the derived class can also define your own variables and methods.
  • Meanwhile, the derived class can be overloaded method of the base class, and that is to declare a base class member variable of the same name, but in a derived class be redefined. If the base and derived classes also have the same name with the variable parameters are the same return values, but define different functions, when using the base class object that calls the function call is a function of the base class, the function is called in the object using the derived class, call is defined in the derived class.
  • Base pointer can point to a derived class objects without explicit type conversions; can refer to a base class derived object references without explicit type conversions. Rule references and pointers assigned to the type of match type of C ++ exceptions to the requirement for inheritance. However, this is just one way of exception, not the base class object and assign the address and pointer references derived classes. Base pointer or reference to the base class can only be used to invoke the method.

Polymorphic public inheritance

After polymorphic i.e. a derived class inherits from a base class, and would like to define a base class of the same name, parameter list, return the value of the function, but the function is defined, but different from the base class, that is, a derived class overload base class methods.

First, the derived class can override base class, the derived class if the same base class function, and the function definition, do not need to declare the function in a derived class, a function that is required on the common base class. If the derived class to the base class want a new definition, they need to be again in a derived class declaration and definition, the definition also needs to show that the definition is a function of that class. Such baseClass::function()and derivedClass::function()such.

Virtual method (virtual method), require the use of keywords virtualfunction modifying the base class, as follows:

virtual void function(int i);

Its role is as follows: When a same base class and derived class has been defined, the method in which class we need to determine the call is, especially when the method is by reference or pointer instead of the object invocation.

  • If there is no keyword virtual, the program selection method according to a reference or pointer type
  • If the keyword virtual, the program will be selected according to the method of a reference or pointer to the type of object
  • If there are derived classes override base class methods, generally requires the destructor virtual base class is provided to guarantee that calls the destructor is released in the proper order when a derived class object
// 不使用virtual
BaseClass baseClass();
DerivedClass derivedClass();
BaseClass & reference1 = baseClass; // 指向baseClass的类型是BaseClass的引用变量
BaseClass & reference2 = derivedClass; // 指向derivedClass的但是类型是BaseClass的引用变量
reference1.function(); // 会根据引用的类型即BaseClass调用BaseClass下的function方法
reference2.function(); // 会根据引用的类型即BaseClass调用BaseClass下的function方法
 
// 使用virtual
BaseClass baseClass();
DerivedClass derivedClass();
BaseClass & reference1 = baseClass; // 指向baseClass的类型是BaseClass的引用变量
BaseClass & reference2 = derivedClass; // 指向derivedClass的但是类型是BaseClass的引用变量
reference1.function(); // 会根据引用指向的类型即BaseClass调用BaseClass下的function方法
reference2.function(); // 会根据引用指向的类型即DerivedClass调用BaseClass下的function方法

Abstract base class

Abstract base class (abstract base class, ABC) is a special base class, conceptually, a common method for all derived classes of the abstract summary statements (defined above) to a class, the lower class of this design It can be regarded as an abstract base class. But the real abstract base class should be included at least a pure virtual function class (pure virtual function) of this kind can not declare corresponding object , only as a base class.

It is a pure virtual function is given only in the abstract base class prototype, but the portion of the definition given function, like an interface, with all pure virtual functions of the derived classes defined according to the needs to achieve their class. Writing pure virtual function is a virtual function to the back end = 0

virtual double pureVirtualFunction(int i) const = 0;

Application of this manner, the total of all the derived classes, but yet each have different methods to achieve an abstract base class, but do not provide prototype be defined (only pure virtual function allows C ++ does not give the definition), then such that each derived class defines its own analysis.

Private inheritance

Private inheritance of a base class that is inherited using private inheritance modifier modified, if not modified access qualifier, the default is private inheritance, private inheritance is a "has-a" relationship.

class DerivedClass : private BaseClass{ }
class DerivedClass : BaseClass{ }

Private inheritance makes the public members of the base class are protected members become private members of the derived class, which makes those base class methods can no longer be used to instantiate an object of a derived class, but can only be a member function in the derived class internal class uses. That portion derived class inherits the base class interface.

Here we compare the differences between the three kinds of inheritance:

feature Public inheritance Protected inheritance Private inheritance
Become a member of the public Public members of the derived class Protected members of the derived class Private members of the derived class
Become protected members Protected members of the derived class Protected members of the derived class Private members of the derived class
Private members become It can only be accessed through the interface base class It can only be accessed through the interface base class It can only be accessed through the interface base class
Implicit whether upconversion can Only in a derived class Can not

Multiple Inheritance

Multiple Inheritance (Multiple Inheritance) is the relationship "is-a", which allows a class to inherit from more than one class, simply inherited class can be separated by commas, like this:

class DerivedClass : public BaseClass1, public BaseClass2 {……}
class DerivedClass : public BaseClass1, BaseClass2 {……} // BaseClass2 is a private base

Multiple inheritance each inherited base class need to set access qualifier, you can use different access qualifiers as needed, not written by defaultprivate

Such as setting a base class Worker representation of workers, then workers can be a singer can also be a waiter, we use two classes inherit from this base class, Singer and Waiter, finally, we can define a class that is both a singer and sometimes waiter, so it also inherited from Singer and Waiter, their relationship is like below this:

class Worker {……}
class Singer : public Worker {……}
class Waiter : public Worker {……}
class SingingWaiter : public Singer, public Waiter {……}

A multiple inheritance

Virtual base class

First, multiple inheritance leads to a problem that, when an instance of a SingingWaiter inherited from Singer and Waiter, also indirectly inherited twice Worker, that is to say a SingingWaiter example of such a structure should be as follows:

When not in use virtual base class

Such problem is caused when we address the derived class object pointer from a base class can not distinguish which is the base class is assigned, resulting in ambiguity:

SingingWaiter sw;
Worker * psw = &sw;

Because Worker sw contains two objects, so as to have two addresses can be selected, so the correct wording should be:

Worker * psw1 = (Waiter *) &sw;
Worker * psw2 = (Singer *) &sw;

Therefore, the virtual base class (Virtual Base Classes) would solve this problem. Virtual base class that is derived from a plurality of base class objects inherit a class object, use the virtual keyword in the declaration of class inheritance, virtual public and order does not matter:

class Singer : virtual public Worker {……}
class Waiter : public virtual Worker {……}
class SingingWaiter : public Singer, public Waiter {……}

Now, SingingWaiter object can contain only one copy of the Worker object, in essence, the inherited Singer and Waiter share a Worker object, rather than the introduction of each copy of a Worker object, so you can use polymorphism.

在使用虚基类的时候


Please indicate the source, permanently updated links in this article: https://blogs.littlegenius.xin/2019/08/28/【C- refresher] seven types of inheritance /

Published 44 original articles · won praise 46 · views 9156

Guess you like

Origin blog.csdn.net/qq_38962621/article/details/100128530