Three types of _c languages for realizing information hiding in C + + program development

Source: Internet
Author: User

Whether it is modular design, object-oriented design or layered design, it is the key internal requirement to realize the external hiding of information in subsystem. With my simple experience, the information hiding according to the extent of the different divided into (1) not available (2) visible unavailable (3) visible is available.

1 not visible and unavailable

This means that the variables, structs, and class definitions inside the module are completely hidden from the outside, and the outside is ignorant. The common way to implement this is to use an opaque pointer, see my blog C language Development function library, using opaque pointers to hide the details of the structure.

This approach is also applicable to C + + language, a possible implementation of interface-oriented programming.

Header file IMyClass.h

Class IMyClass
{public
:
virtual ~imyclass ();
Public:
virtual void Public_function1 ();
virtual void Public_function2 ();
imyclass* Createmyclassobject ();

Implementing File MyClass.cpp

#include "IMyClass.h"
class myclass:imyclass.h
{
private:
int x;
int y;
int Z;
Public:
virtual void Public_function1 ();
virtual void Public_function2 ();
imyclass* Createmyclassobject ()
{return
new MyClass ();
}

This implementation can be applied both at the source level and at the library level.

In theory, complete realization is the most perfect design. However, this is very demanding for program design, and will bring more code and design logic levels as well as some limitations (such as the inability to implement inheritance of existing types).

Therefore, many C + + libraries are designed to use the following invisible way, such as MFC.

2 visible is not available

This method refers specifically to C + +, referring to a non-public type member in a C + + class. For example, in the header file Myclass.h:

Class MyClass
{
private:
int x;
int y;
int Z;
Protected:
float F;
Public:
int M;
void Member_method1 ();

For call callers, when you include a secondary header file, you can see member variables such as X,Y,Z, but you can't use them. As long as clients need to use new to generate instances or inherit classes, they must know the full definition of the class.

This is not the case for C, because any variable in the structure is public.

3 Visible Available

That is, there is no hidden, the design must be avoided.

The above mentioned is aimed at the implementation of the three types of information hiding in C + + program development, hope to help all above!

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.