02C + + review Data abstraction / data encapsulation / interface (abstract class)

Posted by KeeganWolf on Thu, 10 Mar 2022 11:31:04 +0100

1, C + + data abstraction

1. Definition

Data abstraction means that it only provides key information to the outside world and hides the implementation details of its background, that is, it only shows the necessary information without presenting details. Data abstraction is a programming (Design) technology that depends on the separation of interface and implementation.

C + + classes make data abstraction possible. They provide a large number of public methods for manipulating object data to the outside world, that is, the outside world does not actually know the internal implementation of the class.

2. Benefits of data abstraction

Data abstraction has two important advantages:
(1) the interior of the class is protected from damage to the object state due to unintentional user level errors.
(2) class implementations may change over time to respond to changing requirements, or to error reports that require no change in user level code.

If data members are defined only in the private part of the class, the author who wrote the class can change the data at will. If the implementation changes, you just need to check the code of the class to see what impact the change will have. If the data is public, any function that directly accesses data members of the old representation may be affected.

3. Instances of data abstraction

In C + + programs, any class with public and private members can be used as an instance of data abstraction. See the following example:

using namespace std;

class Adder{
   public:
      // Constructor
      Adder(int i = 0)
      {
        total = i;
      }
      // External interface
      void addNum(int number)
      {
          total += number;
      }
      // External interface
      int getTotal()
      {
          return total;
      };
   private:
      // Hidden data
      int total;
};
int main( )
{
   Adder a;

   a.addNum(10);
   a.addNum(20);
   a.addNum(30);

   cout << "Total " << a.getTotal() <<endl;
   return 0;
}

The above class adds numbers and returns the sum. Public members addNum and getTotal are external interfaces. Users need to know them in order to use the class. The private member total is something that the user does not need to know, but it is necessary for the class to work normally.

2, C + + data encapsulation

All C + + programs have the following two basic elements:
Program statements (code): These are the parts of the program that perform actions. They are called functions.
Program data: data is program information, which will be affected by program functions.

Encapsulation is a concept in object-oriented programming that binds data and functions that manipulate data together, which can avoid external interference and misuse, so as to ensure safety. Data encapsulation leads to another important OOP concept, namely data hiding.

Difference between data encapsulation and data abstraction:
Data encapsulation is a mechanism that binds data and functions that manipulate data together. Data abstraction is a mechanism that only exposes the interface to users and hides the specific implementation details.

C + + supports data encapsulation and data hiding (public, protected, private) by creating classes. We already know that classes contain private, protected, and public members. By default, all items defined in a class are private. For example:

{
   public:
      double getVolume(void)
      {
         return length * breadth * height;
      }
   private:
      double length;      // length
      double breadth;     // width
      double height;      // height
};

The variables length, breadth, and height are all private. This means that they can only be accessed by other members of the Box class, not by other parts of the program. This is a way to implement encapsulation.

In order to make the members in the class public (that is, other parts of the program can also be accessed), they must be declared with the public keyword before these members. All variables or functions defined after the public identifier can be accessed by all other functions in the program.

Defining a class as a friend class of another class will expose the implementation details and reduce the encapsulation. The ideal approach is to hide the implementation details of each class as much as possible.

2, C + + interface (abstract class)

The interface describes the behavior and function of the class without completing the specific implementation of the class.

C + + interface is implemented by using abstract classes. Abstract classes and data abstraction are not confused with each other. Data abstraction is a concept that separates implementation details from relevant data.

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

{
   public:
      // Pure virtual function
      virtual double getVolume() = 0;
   private:
      double length;      // length
      double breadth;     // width
      double height;      // height
};

Abstract classes (commonly known as ABC) are designed to provide other classes with an appropriate base class that can be inherited. Abstract classes cannot be used to instantiate objects. They can only be used as interfaces. If you try to instantiate an object of an abstract class, it will lead to 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 ABC to declare interfaces. If a pure virtual function is not overloaded in a derived class, trying to instantiate the object of the class will lead to a compilation error.

Classes that can be used to instantiate objects are called concrete classes. Take a look at the following example. The base class Shape provides an interface getArea(), and implements getArea() in two derived classes, Rectangle and Triangle respectively:

 
using namespace std;
 
// Base class
class Shape 
{
public:
   // Pure virtual function providing interface framework
   virtual int getArea() = 0;
   void setWidth(int w)
   {
      width = w;
   }
   void setHeight(int h)
   {
      height = h;
   }
protected:
   int width;
   int height;
};
 
// Derived class
class Rectangle: public Shape
{
public:
   int getArea()
   { 
      return (width * height); 
   }
};
class Triangle: public Shape
{
public:
   int getArea()
   { 
      return (width * height)/2; 
   }
};
 
int main(void)
{
   Rectangle Rect;
   Triangle  Tri;
 
   Rect.setWidth(5);
   Rect.setHeight(7);
   // Area of output object
   cout << "Total Rectangle area: " << Rect.getArea() << endl;
 
   Tri.setWidth(5);
   Tri.setHeight(7);
   // Area of output object
   cout << "Total Triangle area: " << Tri.getArea() << endl; 
 
   return 0;
}

Transfer from https://blog.csdn.net/sinat_33924041/article/details/83617469

Topics: C++