25. C + + data abstraction

Source: Internet
Author: User

C++Data Abstraction

Data abstraction refers to providing only critical information to the outside world and hiding the implementation details of its background, that is, presenting only the necessary information without presenting the details.

Data abstraction is a programming (design) technology that relies on interfaces and implements separation.

Let's give a real-life example, such as a TV set, where you can turn on and off, switch channels, adjust the volume, add external components (such as speakers, VCRs, DVD players), but you don't know its internal implementation details, which means that you don't know how it is receiving signals from a cable, How to convert the signal and finally display it on the screen.

Therefore, we can say that television has its internal implementation and external interface separated, you do not need to know its internal implementation principle, directly through its external interface (such as power button, remote control, sound volume controller) can control the TV.

Now, let's get down to it, for C + + programming, C + + classes provide the possibility for data abstraction . They provide the outside world with a lot of public methods for manipulating object data, which means that the internal implementation of the class is not really clear to the outside world.

For example, your program can call the sort () function without needing to know the algorithm used to sort the data in the function. In fact, the underlying implementation of a function sort differs depending on the version of the library, and as long as the interface is not changed, the function call can work as usual.

In C + +, we use classes to define our own abstract data types (ADT). You can use the cout object of Class Ostream to output data to standard output, as follows:

1 # include <iostream> 2 3 using namespace std; 4  5 int main () 6 {7    cout << "Hello C + +" <<Endl; 8    return 0; 9 }

Here, you don't need to understand how cout is displaying text on the user's screen. You only need to know the public interface, cout's underlying implementation can be freely changed.

Access Tags Force abstraction

In C + +, we use Access tags to define the abstract interface of a class. A class can contain 0 or more access tags:

    • Members that are defined with a public tag can access all parts of the program. The abstract view of a type of data is defined by its public members.
    • A member defined with a private label cannot access code that uses a class. The private section hides implementation details for code that uses types.

There is no limit to how often access tags appear. Each access tag specifies the access level that is immediately followed by the member definition. The specified access level is valid until the next access tag is encountered or the closing parenthesis of the class principal is encountered.

The benefits of data abstraction

There are two important advantages to data abstraction:

    • The inside of the class is protected from unintentional user-level errors that cause the object state to be compromised.
    • Class implementations may change over time to respond to changing requirements or to address error reports that require no change in user-level code.

If you define a data member only in the private part of the class, the author of the class can change the data arbitrarily. If the implementation changes, you only need to check the code of the class to see what impact this change will have. If the data is public, any functions that directly access the data members of the old representation may be affected.

Examples of data abstraction

In a C + + program, any class with public and private members can be an instance of the data abstraction. Take a look at the following example:

1#include <iostream>2 using namespacestd;3 4 classadder{5     Public:6       //constructor Function7Adder (inti =0)8       {9Total =i;Ten       } One       //interface to external A       voidAddnum (intNumber ) -       { -Total + =Number ; the       } -       //interface to external -       intgettotal () -       { +           returnTotal ; -       }; +    Private: A       //data hidden from the outside at       intTotal ; - }; - intMain () - { - Adder A; -  inA.addnum (Ten); -A.addnum ( -); toA.addnum ( -); +  -cout <<" Total"<< a.gettotal () <<Endl; the    return 0; *}

Execution Result:

60

The above class adds the numbers and returns the sum. Public members Addnum and gettotal are external interfaces that users need to know to use classes. The private member Total is what the user does not need to know, but it is necessary for the class to work properly.

Design Strategy

Abstraction separates code into interfaces and implementations. So when designing a component, you must keep the interface separate from the implementation, so that if you change the underlying implementation, the interface will remain intact.

In this case, no matter how the interface is used by any program, the interface will not be affected, just recompile the latest implementation.

25. C + + data abstraction

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.