Skip to content

Commit

Permalink
english
Browse files Browse the repository at this point in the history
  • Loading branch information
xliu79 committed Jul 18, 2020
1 parent f04d7db commit 9f6088a
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
46 changes: 46 additions & 0 deletions english/basic_content/c_poly/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Object oriented features of C + + implemented by C


## About Author:


![](../img/wechat.jpg)

## 1.C++

Polymorphism in C ++ :In C++, we usually maintain a virtual function table. According to the assignment compatibility rule, we know that the pointer or reference of the parent class can point to the child class object



If the pointer or reference of a parent class calls the virtual function of the parent class, the pointer of the parent class will look up its function address in its own virtual function table.If the pointer or reference of the parent object points to an object of a subclass, and the subclass has overridden the virtual function of the parent class, the pointer will call the overridden virtual function of the subclass.

Code Example:[c++_examp.cpp](./c++_examp.cpp)



## 2.C Implement

- Encapsulation

There is no concept of class class in C language. But with struct structure, we can use struct to simulate;

Encapsulating properties and methods into structures using function pointers.



- Inherit

Structure nesting


- Polymorphic

Class and subclass methods have different function pointers

There is no member function in the structure of C language. How to realize the common function of parent structure and child structure? We can consider using function pointers to simulate. But there is a flaw in this approach:The function pointer between the parent and child does not point to the virtual function table maintained in C ++, but a piece of physical memory. If there are too many simulated functions, it will not be easy to maintain.

In order to simulate polymorphism, function pointer variables must be aligned(They are completely consistent in content and alignment of variables).Otherwise, the parent class pointer points to the child class object, and the operation crashes!



Code example :[c_examp.c](./c_examp.c)
Binary file added english/basic_content/c_poly/c++_examp
Binary file not shown.
43 changes: 43 additions & 0 deletions english/basic_content/c_poly/c++_examp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @file c++_examp.cpp
* @brief c++中的多态
* @author 光城
* @version v1
* @date 2019-08-06
*/

#include <iostream>

using namespace std;
class A
{
public:
virtual void f()//Implement a virtual function
{
cout << "Base A::f() " << endl;
}
};

class B:public A // 必须为共有继承,否则后面调用不到,class默认为私有继承!
{
public:
virtual void f()//Virtual function implementation, virtual keyword in subclass can not be appearence
{
cout << "Derived B::f() " << endl;
}
};


int main()
{
A a;//Base class object
B b;//an object of derived type

A* pa = &a;//The parent class pointer points to the parent object
pa->f();//Call the function of the parent class
pa = &b; //The parent class pointer points to the subclass object, which is implemented in polymorphism


pa->f();//Call the function with the same name of the derived class
return 0;
}
Binary file added english/basic_content/c_poly/c_examp
Binary file not shown.
55 changes: 55 additions & 0 deletions english/basic_content/c_poly/c_examp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @file c_examp.c
* @brief C实现多态
* @author 光城
* @version v1
* @date 2019-08-06
*/

#include <stdio.h>

/// Define a pointer of function
typedef void (*pf) ();

/**
* @brief parent class
*/
typedef struct _A
{
pf _f;
}A;


/**
* @brief child class
*/
typedef struct _B
{
A _b; ///< The inheritance of the parent class can be realized by defining an object of the base class in the subclass
}B;

void FunA()
{
printf("%s\n","Base A::fun()");
}

void FunB()
{
printf("%s\n","Derived B::fun()");
}


int main()
{
A a;
B b;

a._f = FunA;
b._b._f = FunB;

A *pa = &a;
pa->_f();
pa = (A *)&b; /// The parent class pointer points to the object of the subclass. Because of the type mismatch, it needs to be forced
pa->_f();
return 0;
}

0 comments on commit 9f6088a

Please sign in to comment.