How to implement "Object-oriented programming" in C language

Source: Internet
Author: User
Tags print object

Some people think that object-oriented is the C++/java of this high-level language patent, is not the case, object-oriented as a design method, is not restricted language. It can only be said that the use of C++/java this syntax to achieve object-oriented is easier and more natural.

In this section, it's hard work to show how to implement object-oriented programming in C. There are two purposes for writing these:

① a better grasp of the concept of class in C + +. Learn this chapter, know C Programmer's non-resistance, know why to invent a class concept, why should have member function and so on.

② provides a reference design for C programmers. By the existence of certain occasions, only the C language is allowed to program, not allowed in C + + programming. At this point, you can refer to this code, the C syntax to emulate C + + classes to implement object-oriented programming.

This article provides several object-oriented methods based on C-struct in order from easy to difficult. In the presentation of this section, unify the name file with the. c suffix, that is, write the code in C language syntax. C and C + + 's struct syntax is slightly different, please refer to the Appendix "C + + and the difference".

Rights Statement : The author has all the rights in this book. The author authorizes anyone to freely reprint the content posted on this website, but must abide by the following restrictions when reproduced: ① reprint must be reproduced in full, no modification, must include "rights statement" and "official address" ② Limited to the network reprint, that is, the final results published on the network. Any non-compliance with the above two of the reprint act as a tort. Except as permitted by me, no person shall use the content of this website for any other purpose.
website Address: http://www.afanihao.cn/message please go to http://www.afanihao.cn/kbase/

1.1 1th Method

This method is only suitable for single instance objects. As an example of a printer print object, there is only one printer in the system, and the interface is provided as follows.

First, give the Printer.h, which shows the external interface,

Printer.h begin///////////////

#ifndef _printer_h

#define _printer_h

int Pr_open (); Turn on the printer

void Pr_close (); Turn off the printer

void Pr_print (const char* text); Print text

#endif

/////////////////////////////////////////////////////

Secondly, the printer.c is given and the implementation of the function interface is given. Since there is no way to manipulate a real printer, the code is too complex and the author is not easy to understand. So the concept of "virtual printer" is used here, so-called "print", just write the file to the specified file C:\printer.txt,

PRINTER.C///////////////

#include <stdio.h>

#include "Printer.h"

Defined

typedef struct

{

file* outfile;

}printer_t;

Defining unique instances

Static printer_t PR = {NULL};

Turn on the printer

int Pr_open ()

{

Pr.outfile = fopen ("C:/printer.txt", "AB");

if (Pr.outfile = = NULL)

return-1;

return 0;

}

Turn off the printer

void Pr_close ()

{

if (pr.outfile)

{

Fclose (Pr.outfile);

Pr.outfile = NULL;

}

}

Print text

void Pr_print (const char* text)

{

fprintf (pr.outfile, text);

}

/////////////////// ///////////////

This is called in MAIN.C.

MAIN.C///////////////

#include "Printer.h"

void Main ()

{

Pr_open (); Open it

Pr_print ("aaabbbccc\n"); Output text

Pr_close (); Shut down

}

This can be summed up in the characteristics of the wording:

(1) Although object-oriented, there is only one object that is not visible outside the object;

(2) The function of the object can only be accessed by the external function. You can see that there is no pointer to the object passed in the function.

1.2 2nd method

This method is used when multiple objects can be created. Still in front of the printer for example, in fact this printer is just a "virtual" printer, the ultimate goal is to output to a local file. Then, you can allow the creation of multiple printer objects.

The header file Printer.h defines the object in which the provided interface function has an object pointer, where the ~open function is used to create an object, ~close is used to destroy the object.

Printer.h///////////////

#ifndef _printer_h

#define _printer_h

#include <stdio.h>

typedef struct

{

file* outfile;

}printer_t;

printer_t* pr_open(const char* filename); Turn on the printer

void pr_close(printer_t* PR); Turn off the printer

void pr_print(printer_t* PR, const char* text); Print text

#endif

Here's how it's implemented,

PRINTER.C///////////////

#include <stdio.h>

#include <stdlib.h>

#include "Printer.h"

Turn on the printer

printer_t* pr_open (const char* filename)

{

printer_t* PR = (printer_t*) malloc (sizeof (printer_t));

Pr->outfile = fopen (filename, "AB");

if (!pr->outfile)

{

Free (PR);

return NULL;

}

return PR;

}

Turn off the printer

void Pr_close (printer_t* PR)

{

Fclose (Pr->outfile);

Free (PR);

}

Print text

void Pr_print (printer_t* PR, const char* text)

{

fprintf (pr->outfile, text);

}

Call this object in the MAIN.C,

MAIN.C///////////////

#include "Printer.h"

void Main ()

{

printer_t* PR = pr_open ("C:/1.txt");

Pr_print (PR, "aaabbbccc\n");

Pr_close (PR);

}

At this point, the object-oriented is relatively complete, it has 3 elements:

① Object creation: Use the Pr_open function to create an object that allows multiple objects to be created;

② Object destruction: Use Pr_close to destroy objects

③ Object Function Interface: Pr_print is its function interface, note that the first parameter of this function is to point to an object

1.3 3rd Method

This approach is an upgrade to Method 2, as is the idea and Method 2. It is basically consistent with the C + + class, and perhaps it can be said that the C + + class has evolved from this approach.

..... This section is not public, more content please buy paper textbooks, thank you for your support! .....

How to implement "Object-oriented programming" in C language

Related Article

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.