The C language implements C++ inheritance and polymorphic code sharing

  • 2020-05-27 06:39:37
  • OfStack

This question mainly examines the difference between C and C++, as well as the concepts of inheritance and polymorphism in C++.

The difference between C and C++

C is a process-oriented language, while C++ is an object-oriented process.

What are object orientation and process orientation?

Process-oriented is to analyze the steps to solve the problem, and then use the function to implement these steps step by step, in the use of 11 calls on the line, the focus is on the analysis of the process. Object-oriented is to divide the problem into various objects, the purpose of building objects is not just to complete the steps, but to describe the behavior of each problem in the process of solving.

What is the difference between object-oriented and procedural?

The process-oriented design method USES functions to describe the operation of data, but separates the function from the operation data.

Object-oriented design method is to encapsulate the object and data into a whole.

Process-oriented design steps for the process, late maintenance is difficult.

Object-oriented design is data-centric, and data is more stable and easier to maintain than function.

Three characteristics of object orientation


For encapsulation, the data and the concrete operation implementation code are placed inside an object, so that the specific details of the code are not discovered by the outside world, leaving only some interfaces for external use, but not any form of object internal implementation. Encapsulation can hide implementation details, making the code easier to maintain and ensuring system security.


Inheritance is the most important means for object-oriented programming to reuse code. It allows programmers to add functionality to a class by extending it to maintain its original class properties. The resulting class is called a derived class, and inheritance can represent the hierarchy of object-oriented mechanisms.


Polymorphic simply means "one interface, multiple implementations", which means multiple forms of the same thing. Polymorphism in object-oriented languages refers to the many different implementations of an interface. That is, multiplexing the same interface for different operations.

C++ polymorphism supports two polymorphisms, compile-time polymorphism and run-time polymorphism. Compile-time polymorphism is realized by overloaded functions and run-time polymorphism by virtual functions. Static polymorphism: the compiler completes at compile time. Based on the type of function arguments (which may be implicitly cast), the compiler can infer which function to call. If there is a corresponding function, it can call the corresponding function; otherwise, it will report a compilation error. Dynamic polymorphism: determines the actual type of the referenced object during function execution (not at compile time) and calls the corresponding method based on the actual type. The virtual keyword is used to modify the member function of the class to indicate that the function is virtual, that the derived class needs to be re-implemented, and that the compiler will implement dynamic binding.

Conditions for dynamic polymorphism: 1 > , usage scenario: the parent class pointer or reference to the parent class or subclass object (determined by the assignment compatibility rule); 2 > , the realization of polymorphic two conditions: virtual function rewrite; The parent class's pointer or reference calls the overridden virtual function. 3 > , if the parent class of the member function plus virtual keyword, the subclass overrides the function default virtual, can not be specified, but 1 plus; Override: subclass overrides the virtual function of the parent class, requires the function name, function parameters, and return value exactly 1 (except covariant);

In summary: encapsulation can hide implementation details including including private members, which increases the security index for code modules; Inheritance can extend existing modules in order to increase the reusability of code. Polymorphism is to ensure that the class instances are properly called when the class is inherited and derived, thus realizing the reuse of the interface

C simulation implements polymorphism

Polymorphism in C++

What we know is that a table of virtual functions is maintained in C++, and according to the assignment compatibility rule, we know that a pointer or reference to a parent class can point to a subclass object. If a parent class pointer or reference call virtual functions of the parent is the parent class pointer will find themselves in their own virtual function table function address, if the superclass object pointer or reference point is a subclass of object, and the subclass has rewritten the parent virtual functions, the pointer will have rewritten the virtual function call subclasses.
//c++ polymorphism

class A
  virtual void fun()// Virtual function implementation 
    cout << "Base A::fun() " << endl;

class B:public A
  virtual void fun()// Virtual function implementation , subclasses virtual Keywords can be missing 
    cout << "Derived B::fun() " << endl;

void Test1()
  A a;// A base class object 
  B b;// Derived class object 

  A* pa = &a;// The parent class pointer points to the parent class object 
  pa->fun();// Call the superclass's function 

  pa = &b; // Superclass pointer to subclass object, polymorphic implementation 
  pa->fun();// Calls a function of the same name of a derived class 

The C language implements polymorphism

We know that there is no such thing as an class class in the C language, but there is an struct structure, and we can consider using struct to simulate it; However, there are no member functions inside the structure of C language. How to implement the functions Shared by the parent structure and the substructure? We can consider using function Pointers to simulate. However, there is a disadvantage in this approach: instead of pointing to the virtual function table maintained in C++, the parent and son function Pointers point to a block of physical memory, which will be difficult to maintain if there are too many simulated functions.

//C Dynamic, using function Pointers 

typedef void(*FUN)();// redefine 1 Function pointer types 

// The parent class 
struct Base 
  FUN _f;

// A subclass 
struct Derived
  Base _b;// Defined in a subclass 1 The object of the base class can realize the inheritance of the parent class 

void FunB()
  printf("%s\n", "Base::fun()");
void FunD()
  printf("%s\n", "Derived::fun()");

void Test2()
  Base b;// Superclass object 
  Derived d;// A subclass object 

  b._f = FunB;// The superclass object calls the superclass function of the same name 
  d._b._f = FunD;// The subclass calls the subclass's function of the same name 

  Base *pb = &b;// The parent class pointer points to the parent class object 

  pb = (Base *)&d;// Have the parent class pointer to a subclass object , Strong rotation is performed because the type does not match 


Related articles: