Embedded Linux C + + language (iii)--Encapsulation (encapsulation)

Source: Internet
Author: User
Tags access properties

embedded linux C + + Language (iii)--Encapsulation ( Encapsulation ) First, Package introduction

In process-oriented programming, such as C language , data and related operation functions of data are separate individuals; in object-oriented programming such as C + +, data and data related operations are designed to be objects, including attributes (data) and operations (functions), which together form an object entity (that is, a class entity). Object-oriented programming makes the program more modular, easier to read and write, and improves code reuse to a higher level.

In object-oriented programming, data and data operations are encapsulated for objects. Encapsulation can hide implementation details, allowing code to be modularized, to surround processes and data, and to access data only through defined interfaces.

Encapsulation is a model for providing services externally, and the encapsulation model is an individual abstraction of everything in the world.

The encapsulated access properties are as follows:

Accessing an Attribute Property object inside an object
public has access to accessible
Protected protection can be accessed not accessible
Private access is not accessible

in the C language, all behaviors and properties in a struct are public ( default ) , you can use both the interface anddirect access to its internal data, no internal data is masked. C + + in the class You can specify how behaviors and properties are accessed ,Open Data internally, shield data externally, and provide interfaces externally.

ii. Encapsulation of the C language

Stack 's C language implementation:

#include  <stdio.h> #include  <string.h> typedef struct stack{     char space[1024];    int top;} Stack; void stack_init (stack *st) {    st->top = 0;     memset (&st->space, 0, sizeof (St->space));}  int is_empty (stack *st) {    return st->top == 0;} Int is_full (stack *st) {    return st->top == 1024;}  char pop (stack *st) {    return st->space[--st->top];}  void push (stack *st, char c) {    st->space[st->top++]  = c;}  int main (void) {    stack st;    stack_init (&st);     unsigned int i;    for (i =  ' a ';  i <  (' z '  + 1);  i++)     {         if (!is_full (&st))             push (& St, i);     }    for (i =  ' a '; i <  (' z ')  + 1) ( i++)     {        printf ("%c\ N ",  pop (&st));     }    return 0;}

The data members of the encapsulated struct in the C language can be modified by external calls.

Third,C + + class encapsulation

C + + class implementation stack:

Stack.h Source :

#ifndef stack_h#define Stack_h class Stack{public:stack ();    BOOL Is_empty ();    BOOL Is_full ();    void push (char c);    Char pop ();p Rivate:char space[1024]; unsigned int top;}; #endif//Stack_h

Stack.cpp Source:

#include "stack.h" #include <string.h> stack::stack () {top = 0; memset (space, 0, sizeof (space));} BOOL Stack::is_empty () {return top = = 0;} bool Stack::is_full () {return top = = 1024x768;} void Stack::p ush (char c) {s pace[top++] = C;} Char stack::p op () {return space[--top];}

Main.cpp Source:

 #include  <iostream> #include   "Stack.h" #include  < String.h>using namespace std; int main () {    stack st;     unsigned int i;    for (i =  ' a '; i <   ' z '  + 1; i++)     {        if ( !st.is_full ())             st.push (i);     }    for (i =  ' a '; i <  ' z '  + 1; i++)     {        cout<<st.pop () <<endl;     }    return 0;} 

The data members in the class stack encapsulated in the C + + language are private , and for external accessibility, by setting access to data members and Operation methods, you can open external, Block data members and how to do it.


This article from "Endless life, Struggle not only" blog, reprint please contact the author!

Embedded Linux C + + language (iii)--Encapsulation (encapsulation)

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.