How to implement "object-oriented programming" __ Programming in C language

Source: Internet
Author: User
Tags print object

Some people think object-oriented is c++/java this kind of high-level language patent, actually is not such, object-oriented as a design method, is unrestricted language. It can only be said that it is easier and more natural to implement object-oriented C++/java with this syntax.

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

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

② provides a reference design for C programmers. It is not allowed to be programmed in C + +, only in the presence of certain situations. At this point, you can refer to the code in this article, using C syntax to simulate C + + classes to achieve object-oriented programming.

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

Statement of Rights : the author owns all the rights of this book. The author authorizes anyone to reproduce the content published on this website, but must comply with the following restrictions when reprinted: ① reproduced must be reproduced in full text, must not have any changes, should include "rights statement" and "official website address" ② is limited to the network reproduced, that the final results posted on the network. Those who do not comply with the above two reprint act as a tort. No person shall use the content of this website for any other purpose unless I allow it.
website address: http://www.afanihao.cn/message please go to http://www.afanihao.cn/kbase/

1.1 1th Method

This method only works with objects of a single instance. As an example of a printer print object, there is only one printer in the system, and the interface is provided in the following ways.

First, give the Printer.h, 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 realization of function interface is given. Because there is no way to manipulate a real printer, the code is so complex that the author is not easy to understand. So the concept of "virtual printer" is used here, the 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;

To define a unique instance

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);

}

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

Call this in the MAIN.C.

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

#include "Printer.h"

void Main ()

{

Pr_open (); Open it

Pr_print ("aaabbbccc\n"); Output text

Pr_close (); Shut down

}

Can be summed up the characteristics of this formulation:

(1) Although object oriented, but only one object, the object is not visible outside;

(2) The outside world can only interface the function of the object through function. As you can see, there are no pointers to incoming objects 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 an object in the provided interface function that has an object pointer, where the ~open function is used to create an object, ~close 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 has been more complete, it has 3 elements:

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

Destruction of ② objects: using Pr_close to destroy objects

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

1.3 3rd Method

This approach is the same as the upgrade of Method 2, concept and Method 2. It is in the form and C + + class is basically consistent, it may be said that C + + class is from this method evolved.

。。。 This section is not open, more content please buy paper materials, thank you for your support ....

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.