First, 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 (abstract classes) and implements separation. C + + classes provide the possibility for 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.
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.
Ii.. Data encapsulation
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 introduces another important OOP concept, namely data hiding .
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.
Third, interface (abstract class)
Interfaces describe 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:
1 classBox2 {3 Public:4 //Pure virtual function5 Virtual DoubleGetvolume () =0;6 Private:7 DoubleLength//length8 Doublebreadth;//width9 DoubleHeight//HeightTen};
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.
A class that can be used to instantiate an object is called a concrete class .
Take a look at the following example, the base class Shape provides an interface Getarea ()that implements Getarea ()in two derived classes Rectangle and Triangle respectively:
1#include <iostream>2 3 using namespacestd;4 5 //base class6 classShape7 {8 Public:9 //provides a pure virtual function of the interface frameworkTen Virtual intGetarea () =0; One voidSetWidth (intW) A { -width =W; - } the voidSetHeight (inth) - { -Height =h; - } + protected: - intwidth; + intheight; A }; at - //Derived Classes - classRectangle: PublicShape - { - Public: - intGetarea () in { - return(Width *height); to } + }; - classTriangle: PublicShape the { * Public: $ intGetarea ()Panax Notoginseng { - return(Width * height)/2; the } + }; A the intMainvoid) + { - Rectangle Rect; $ Triangle Tri; $ -Rect.setwidth (5); -Rect.setheight (7); the //the area of the output object -cout <<"Total Rectangle Area:"<< Rect.getarea () <<Endl;Wuyi theTri.setwidth (5); -Tri.setheight (7); Wu //the area of the output object -cout <<"Total Triangle Area:"<< Tri.getarea () <<Endl; About $ return 0; -}
When the above code is compiled and executed, it produces the following results:
1 * 2 -
C + + data abstraction, data encapsulation, interfaces (abstract class)