OO in C (1): class simulation and Polymorphism in C language, inheritance

Source: Internet
Author: User
Class simulation in C Language (C ++ programming ideology) introduces the concept of class in object-oriented language. This is an evolution of programming ideas. Class: a collection of specific operations on specific data. Therefore, a class contains two categories: Data and operations. In C, struct is only a collection of data. (Liyuming1978@163.com) 1. example: The following figure shows 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 inherits a c_class B * B _this; // void (* Foo) (c_class B * BThis) is very important in order ); // 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 * athis) {printf ("it is. A = % d/N ", Athis-> A); // or here // Exi T (1); // printf ("pure virtual cannot be executed/N"); // or here} 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); // instantiate B _creat (& B); MB = & B; MA = &; MA = (c_class A *) MB; // introduce the polymorphism pointer prin TF ("% d/N", ma-> A); // unfortunately, the function variable does not have private ma-> Foo (MA); // polymorphism. foo (& A); // It Is Not A polymorphism of B _f2 (& B); // The member function, because of the efficiency issue, the function pointer return 0;} is not used. The output result is 11it is B. C = 13it is. A = 1It is B _fun 2. class simulation explanation: I have seen an article on the Internet about similar ideas (it is said that there are more detailed explanations on C ++ programming ideas, but unfortunately I have no time to read this, if anyone knows this, let's talk about it ). But as the father of C ++ said, "C ++ and C are two languages ". Therefore, do not use it in a similar way in syntax, which may lead to unexpected events. In fact, I agree that the purpose of this article is not to use C to simulate C ++. it is meaningless to use one language to simulate another language. My goal is to solve the problem that the overall framework structure in C language is too scattered and data and functions are out of touch. A major problem with the C language is that the structure is loose. Although a good large program is basically designed according to a function file, but it cannot be more granular-the reason is that its data and functions are out of touch. The biggest difference between a class and a common function set is here. Class can be instantiated, so that the same function can correspond to different variables of the instantiated class. A feature of natural language is generalization: such as tables. It can be said that watches, watches, stopwatches, and so on. Such descriptions can be abstract (Inheritance and polymorphism) in an object-oriented language ). However, we should note that even if it corresponds to the type of watch, there are still detailed attributes such as the length of the table chain, the color of the dial, and so on. If such subtle attributes are still abstracted, class expansion cannot be avoided. So the class uses member variables to describe such attributes. In this way, the instance and initialize different classes will describe the objects with different attributes. However, in C language, this is impossible (at least the language itself does not provide this function ). In C, if each function needs to share a variable, the global variable must be used (within a file ). However, global variables cannot be instantiated again. Therefore, the common method is to define an array. In the past, the common method for C to deal with such problems is this. For example, socket numbers and Handel are actually the subscript of arrays. (Different connections correspond to different numbers and different windows correspond to different Handel. In fact, this is different from the member variables of different classes.) I personally think: there is no essential difference between the two forms (arrays and simulation classes) (if you do not consider the application of virtual functions), the only difference between them is: the array method puts the space application in the "module", and the class simulation method leaves the space application to the outside. It can be said that, class simulation is more flexible. 3. Other words: My ideas are still immature. My goal is to enable C programmers to enjoy more fun in object-oriented programming. We only face the vast "black box". Our job is to pile up code, and if we want to change the code function, we just need to change it to a black box. The bigger purpose is to promote the emergence of such a black box. Maybe one day, a language with good efficiency and good structure will appear. At that time, will programming be as easy as talking?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.