C++ Quick Start 2-Object Oriented Programming

1. Three inherited permissions

  • public: Can be accessed inside and outside the class.
  • protected: Access within the class and subclasses, but not outside the class.
  • private: Only access within the class is allowed, and neither subclass nor outside of the class can be accessed.

2. Adjust inheritance permissions

Using the using class name:: variable name or method, you can control the inheritance authority of a single member or method. The usage method is as follows. After Son inherits Father, the key permission in the Father class is changed to public, and the outside world can also access it; if it is placed under the private of the Son class, then the key becomes private to Son and can no longer be used by Son. Inherited by subclasses.

class Father
{
	protected key;
};

class Son :public Father 
{
public:
	using Father::key;
}

3. Inheritance type

There are 9 combinations of inheritance types. The principle is the principle of least privilege, such as private + protected, then the inherited privilege is private, whichever is smaller.

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-IkbpsDgh-1611509116567)(C:\Users\85114\AppData\Roaming\Typora\typora-user-images\ image-20210121222038995.png)]

4. Multiple inheritance

A class can inherit from multiple base classes at the same time, such as SofaBed inherits Sofa and Bed at the same time.

class SofaBed : public Sofa, public Bed
{

}

5. Virtual inheritance

In the case of multiple inheritance, multiple base classes that may be inherited have the same members or methods, and ambiguity will appear at this time. Virtual inheritance can solve the problem of ambiguity, and also solve the problem of memory waste (only one copy of the same member or method is retained). Virtual inheritance will add an extra 4-byte offset pointer, which can be used by Baidu to inherit the memory distribution.

class SofaBed : virtual public Sofa, virtual public Bed
{

}

The call sequence of virtual inheritance constructor and destructor:

  1. The constructor of the virtual base class is executed first, and the constructors of multiple virtual base classes are constructed in the order of being inherited;
  2. Execute the constructor of the base class, and the constructors of multiple base classes are constructed in the order of inheritance;
  3. The constructor of the member object is executed, and the constructors of multiple member objects are constructed in the stated order;
  4. Execute the constructor of the derived class;
  5. Destruction is performed in the reverse order of construction;

6. The subclass passes parameters to the base class constructor

Subclasses can directly pass parameters to the base class constructor in the constructor, in the form of base class (input parameters) and member class names (input parameters). The calling order of these constructors has nothing to do with the order of writing, but according to the first Calls are made in the order described in section 5.

class SofaBed : virtual public Sofa, virtual public Bed
{
private:
	Data data;	/* Data类 */
public:
   	/* SofaBed构造函数 */
    SofaBed(char *str1, char*str2, char *str3, char* str4) : Sofa(str1), Bed(str2), data(str3)
    {
        
    }
}

7, the realization of polymorphism

Polymorphism needs to rely on virtual functions to achieve.

#include <iostream>
using namespace std;

class Human {
private:
    int a;
public:
	virtual void eat(void) {cout<<"use hand to eat"<<endl;}
};

class English:public Human{
public:
	void eat(void){cout<<"use knife to eat"<<endl;}
};

class Chinese:public Human{
public:
	void eat(void){cout<<"use chopsticks to eat"<<endl;}
};

void eatTest(Human &human)
{
	human.eat();
}

int main(int argc, char **argv)
{
	Human h;
	English e;
	Chinese c;
	
	eatTest(h);
	eatTest(e);
	eatTest(c);
}

The execution results are as follows:

use hand to eat
use knife to eat
use chopsticks to eat

If you remove the virtual attribute of the eat method in the Human class, or change the void eatTest(Human &human) function to void eatTest(Human human), the result of the call is as follows, so that polymorphism cannot be achieved. Polymorphism can only be achieved through pointers or references, and it is not possible to pass values ​​directly.

use hand to eat
use hand to eat
use hand to eat

8. The principle of polymorphism

The program is divided into static binding and dynamic binding. For static binding, the functions that will be called are determined at compile time. The case of non-virtual functions is statically bound; for dynamic binding, the function to be called is dynamically determined when the program is running, such as virtual functions. Case. Static binding is highly efficient, and dynamic binding supports polymorphism.

For the case of a virtual function in a class, there will be a pointer in the class that points to a virtual function table, and the corresponding function in the virtual function table will be found and called through the pointer when calling, as shown in the following figure.

Insert picture description here

For the example above, sizeof(h) = sizeof(e) = sizeof(c) = 16. Because the virtual function table not only contains virtual function pointers, but also some of the class and inheritance information .

9. Limitations of polymorphism

  • Only members of a class can be declared as virtual functions.
  • Static member functions cannot be virtual functions.
  • Inline functions cannot be virtual functions.
  • The constructor cannot be a virtual function.
  • Destructors are generally declared as virtual functions. If the destructor is not declared as a virtual function, then when the subclass is destructed, it will only call the destructor of the parent class, not the destructor of this class.
  • Overloaded functions cannot be virtual functions.
  • When the function parameters are the same and the return value is a pointer or reference of this class, it is allowed to declare it as a virtual function; if the return value is other types, it is not allowed to be set as a virtual function.

Guess you like

Origin blog.csdn.net/qq_27575841/article/details/113101405