C + + interface Implementation and separation (reprint)

Source: Internet
Author: User

Original address: http://www.360doc.com/content/13/0218/10/8363527_266294787.shtml

Good design should only expose the interface to the user, all the implementation details for the user should be hidden, that is to say, the user just give the interface to pass the corresponding parameters on the line, do not need the tube inside is how to achieve, such as we use Fopen,fseek,createwindow functions will find very useful, Without the need to control how the internal code of the Fopen,fseek,createwindow function is implemented, how the data structure is organized, that is, absolutely no details can be exposed to the user, including data organization.

I'm going to use C and C + + for an example of how C + + is implemented separately, and then see which is better.

Let's look at the encapsulation implemented by the C + + class:

---------------------------Interface1.h---------------------------

#ifndef Interface1_h
#define Interface1_h

Class DATA
{
Private
int _i;
Short _j;

Public
DATA ();
~data ();

void set (int i, short j);
void Get (int* i, short* j);
};

#endif

---------------------------Interface1.cpp---------------------------

#include "Interface1.h"

DATA::D ata ()
{
_i = _j = 0;
}

Data::~data ()
{
_i = _j = 0;
}


void Data::set (int i, short j)
{
_i = i;
_j = j;
}

void Data::get (int* i, short* j)
{
*i = _i;
*j = _j;
}

---------------------------Test.cpp---------------------------

#include <stdio.h>
#include "Interface1.h"

int main ()
{
Data data;
int i;
Short J;

Data.set (2, 3);
Data.get (&i, &j);

printf ("I =%d, j =%d\n", I, j);

return 0;
}


Let's look at C how to cleverly package and hide implementation Details:

---------------------------Interface.h---------------------------

#ifndef Interface_h
#define Interface_h

void* data_create ();
void Data_set (void* dummy, int i, short j);
void Data_get (void* dummy, int* I, short * j);
void Data_destroy (void* dummy);

#endif

---------------------------interface.c---------------------------

#include <stdlib.h>

struct DATA
{
int i;
Short J;
};

void* Data_create ()
{
return malloc (sizeof (struct DATA));
}

void Data_set (void* dummy, int i, short j)
{
struct data* DATA = dummy;

Data->i = i;
Data->j = j;
}

void Data_get (void* dummy, int* I, short * j)
{
struct data* DATA = dummy;

*i = data->i;
*j = data->j;
}

void Data_destroy (void* dummy)
{
Free (dummy);
}

---------------------------test.c---------------------------

#include <stdio.h>
#include "interface.h"

int main ()
{
int i;
Short J;

void* data = Data_create ();

Data_set (data, 2, 3);

Data_get (data, &i, &j);
printf ("I =%d, j =%d\n", I, j);

Data_destroy (data);

return 0;
}


It can be seen that the implementation of C only exposes the interface to the user, the internal implementation of the details are hidden, but the C + + class implementation instead in the header file exposed implementation details.

Of course, the use of C + + can only expose the interface to the user, but the implementation will be more complex, and need to consume more memory (using the virtual function).

--------------------------------------Parent.h--------------------------------------

#ifndef Parent_h
#define Parent_h

Class PARENT
{
Public
virtual void Set (int i, short j) = 0;
virtual void get (int* i, short* j) = 0;
};

parent* Get_child ();

#endif

--------------------------------------Parent.cpp--------------------------------------

#include "Parent.h"
#include "Child.h"

parent* Get_child ()
{
return new child;
}

--------------------------------------Child.h--------------------------------------

#ifndef Child_h
#define Child_h

#include "Parent.h"

Class Child:public PARENT
{
Private
int _i;
Short _j;

Public
Child ();
~child ();

void set (int i, short j);
void Get (int* i, short* j);
};

#endif

--------------------------------------Child.cpp--------------------------------------

#include "Child.h"

Child::child ()
{
_i = _j = 0;
}

Child::~child ()
{
_i = _j = 0;
}


void Child::set (int i, short j)
{
_i = i;
_j = j;
}

void Child::get (int* i, short* j)
{
*i = _i;
*j = _j;
}

--------------------------------------Test.cpp--------------------------------------

#include <stdio.h>
#include "Parent.h"

int main ()
{
int i;
Short J;

parent* parent = Get_child ();

Parent->set (2, 3);
Parent->get (&i, &j);

printf ("I =%d, j =%d\n", I, j);

return 0;
}

C + + interface Implementation and separation (reprint)

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.