OO in c: Class simulation and polymorphism in C language, inheritance

zhaozj2021-02-16  86

OO IN C (1): Class simulation and polymorphism in C language, inherit liyuming1978 [original]

Class C language simulation (C programming ideology)

Inside the object-oriented language, the concept of classes appears. This is an evolution of programming ideas. So-called class: is a collection of specific operations for specific data. Therefore, the class contains two categories: data and operations. The struct in the C language is just a collection of data. (Liyuming1978@163.com)

1. Example: Next, first look from a small example

#ifndef c_class

#define c_class struct

#ENDIF

C_class a {

C_class a * a_this;

Void (* foo) (c_class a * a_this);

Int a;

INT B;

}

C_class b {// b inherited A

C_class b * b_this; // is very important

Void (* foo) (c_class b * bthis); // virtual function

Int a;

INT B;

INT C;

}

Void B_f2 (c_class b * bthis)

{

Printf ("IT IS B_FUN / N");

}

Void A_foo (c_class a * atthis)

{

Printf ("IT IS A.A =% D / N", ATHIS-> A); //

// EXIT (1);

// Printf ("Inevaver is not allowed / N"); //

}

Void B_foo (c_class b * bthis)

{

Printf ("IT IS B.C =% D / N", BTHIS-> C);

}

Void a_creat (struct a * p)

{

P-> foo = a_foo;

P-> a = 1;

P-> b = 2;

P-> a_this = p;

}

Void B_Creat (Struct B * P)

{

P-> foo = b_foo;

P-> a = 11;

P-> b = 12;

P-> c = 13;

P-> B_THIS = P;

}

Int main (int Argc, char * argv [])

{

C_Class A * MA, A;

C_Class B * MB, B;

A_CREAT (& A); // Instantiation

B_CREAT (& B);

MB = & B;

MA = & a;

MA = (c_class a *) MB; // introduced polymorphic pointer

Printf ("% d / n", ma-> a); // Unfortunately, the function variable is not private

MA-> foo (mA); // polymorphism

A.foo (& a); // is not a polymorphic

B_F2 (& B); // member function, because the efficiency problem does not use function pointer

Return 0;

}

Output results:

11

IT is b.c = 13

IT is a.a = 1

IT IS B_FUN

2. Class simulation commentary:

I saw an article online to tell a similar idea (it is said to have more detailed explanation of C programming ideas, but unfortunately I didn't have time to see this, if you know what people say it). But just like the father of C : "C and C are two languages." So don't be confused by them in syntax, it is possible to cause some unpredictable things.

In fact, I agree with this point of view. The purpose of this article is not to use C to simulate C , and use a language to simulate another language is completely meaningless. My purpose is to solve the C language, the overall frame structure is too dispersed, and the problem of data and function. A big problem in C language is loose structure. Although the current large programs are basically in accordance with a functional design, it is not possible to do smaller particleization - the reason is that its data and function of the function. The biggest difference in class and ordinary functions is here. The class can be instantiated, so the same function can correspond to variables of different instantiated classes.

One feature of the natural language is summarizing: For example, a table. It can be said a watch, a watch, a stopwatch, and the like, such a description can be abstract (inheritance and polymorphism) with object-oriented languages. But we must notice that even if the type of the watch is corresponding to the length of the watch, there is a detailed property of the chain length, the color of the dial, and if it is also abstract, it is unable to avoid the problem of expansion. Therefore, this kind of attribute is described by means of a member variable. In this way, the different classes are initialized, describing the objects of different attributes.

But in the C language, this is impossible (at least the language itself does not provide such a function). In the C language, if each function is to share a variable, a global variable must be used (one file). But global variables cannot be instantiated again. So the usual way is to define an array. In the past C language, the usual way is that this is the case, such as the number of Socket, Handel, etc., is actually the subscript of the array. (Different connections correspond to different numbers, different windows correspond to different Handel, in fact, this and different classes have different member variables)

Personally, two forms (arrays and simulated classes) have no essential differences (if they do not consider the application of virtual functions), their only difference is: The number of arrays will put the space application in the "module", and the class The simulation method left the space application to the outside, it can be said to this, the class simulation is more flexible.

3. Other words:

My thoughts are still very immature. My purpose is to allow C language programmers to enjoy more fun to object-oriented programming. We only face the vast "black box", our job is a stacking code, and if you want to change the code function, you can only change a black box.

The greater purpose is to promote the production of such a blackbox. Perhaps one day, an efficiency is very good, the language is very good, and the language will appear. At that time, it was as easy as it would be like speaking.

转载请注明原文地址:https://www.9cbs.com/read-11900.html

New Post(0)