Three features of C + + encapsulation

Source: Internet
Author: User

Original address: https://qunxinghu.github.io/2016/09/12/C++%20%E4%B8%89%E5%A4%A7%E7%89%B9%E6%80%A7%E4%B9%8B%E5%B0%81%E8%A3%85/

What is encapsulation?

Packaging
: Encapsulation can hide implementation details and make code modular
Encapsulation is the process and data is surrounded, access to data only through the defined interface.
In object-oriented programming, it can be understood as: encapsulate objective things into abstract classes, and classes can put their own data and methods only trusted class or object operation, to the untrusted information hiding.

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.

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.

Data encapsulation

All C + + programs have the following two basic elements:

    • Program statements (Code): This is the part of the program that executes the action, which is called a function.
    • Program Data: The data is the information of the program, it is affected by the program function.

Encapsulation is the concept of binding data and manipulating data in object-oriented programming, so as to avoid being disturbed and misused by the outside world, thus ensuring security.

Data Encapsulation is a mechanism by which data and operational data are bundled together, and data abstraction is a mechanism that exposes only the interface to the user and hides the specific implementation details.

C + + supports encapsulation and data hiding (public, protected, private) by creating classes. As we already know, the class contains private members, protected members (protected), and members of public members. by default, all items defined in a class are private.

C + + interface (abstract class)

Interface
: An interface describes the behavior and functionality of a class without having to complete a specific implementation of the class.
C + + interfaces are implemented using abstract classes, abstract classes are not confused with data abstraction, and data abstraction is a concept that separates implementation details from related data.

If at least one function in a class is declared as pure virtual , then this class is an abstract class . A pure virtual function is specified by using "= 0" in the declaration, as follows:

class Box{   public:      // 纯虚函数      virtual double getVolume() = 0; private: double length; // 长度 double breadth; // 宽度 double height; // 高度};

The purpose of designing abstract classes (often called ABC) is to provide other classes with an appropriate base class that can be inherited. An abstract class cannot be used to instantiate an object, it can only be used as an interface. If you attempt to instantiate an object of an abstract class, you will cause a compilation error. Therefore, if a subclass of ABC needs to be instantiated, each virtual function must be implemented, which also means that C + + supports the use of the ABC declaration interface. If you do not overload a pure virtual function in a derived class, attempting to instantiate an object of that class causes a compilation error.

C + + encapsulation of three major features

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.