Object-oriented in C

Source: Internet
Author: User

In April, Wang C returned from the computer language ranking list and went back to the throne, killing Java. In front.

C ++ is object-oriented and can meet the original intention of C ++: A Better C! C ++ has to be compatible with C, so we can still see the functions and variables written outside the class. C # is different. Combined with the respective advantages of Java and C ++, completely object-oriented. What I want to talk about today is to use C to imitate the object-oriented in C #. Note that it is only to imitate ~~~

1) C # has class, so it should be replaced by the struct in C;

2) If the C # class contains a member function, use a function pointer in C;

The following defines a class and struct to represent a form. When you click a form, the system prompts the Form ID and the Form title:

Class in C:

Class in C #

1 Class Window
2 {
3 Private   String Wndtext;
4 Private   String Wndid;
5 Public Window ( String Text, String ID)
6 {
7 Wndtext = Text;
8 Wndid = ID;
9 }
10 Public   Void Onclick ()
11 {
12 Console. Write ( " Form " + Wndid + " The title is: " + Wndtext );
13 }
14 }

 

ClientProgram:

1 Window win = New ( " Instance form " , " Zero X 12345678 " );
2 Win. onclick (); // Display Information

 

 

Struct in C

1 Struct window
2 {
3 Char   * Wndtext;
4 Char   * Wndid;
5 Void ( * Onclick) (char * Text, char * ID ); // Function pointer 6 };

 

Client Program:

Code

Void Fun ( Char   * Text, Char   * ID)
{
Printf (
" Form % s title: % s " , ID, text );
}
Window win;
Win. wndtext
= " Form in C " ;
Win. wndid
= " Zero X 87654321 " ;< P>

win. onclick = fun; // function pointer

win. onclick (win. wndtext, win. wndid);
// call its member functions like a class "

if you love programming, love C. If you love C, so please love pointers; if you love pointers, please love pointers.

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.