C + + object oriented: C + + interface (abstract class)

Time:2021-3-16

Interfaces describe the behavior and function of a class without the need to complete a specific implementation of the class.

 

The C + + interface usesabstract classData abstraction is a concept that separates implementation details from related 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:

Class box {public: // virtual double getvolume() = 0; private: double length; // length double width; // width double height; // height};

 

Designabstract classThe purpose of ABC is to provide an appropriate base class for other classes to inherit. Abstract classes cannot be used to instantiate objects, they can only be used asInterfaceuse. An attempt to instantiate an object of an abstract class will result in 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 declaration interface. If you do not override a pure virtual function in a derived class, trying to instantiate an object of that class will result in a compilation error.

The classes that can be used to instantiate objects are calledconcrete class

 

Instances of abstract classes

See the example below. The base class shape provides an interfacegetArea(), which are implemented in two derived classes, rectangle and triangle respectivelygetArea()

#include 
 
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);
   //The area of the output object
   cout << "Total Rectangle area: " << Rect.getArea() << endl;
 
   Tri.setWidth(5);
   Tri.setHeight(7);
   //The area of the output object
   cout << "Total Triangle area: " << Tri.getArea() << endl; 
 
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Total Rectangle area: 35Total Triangle area: 17

From the above example, we can see how an abstract class defines an interface getarea (), and how two derived classes implement the same function through different algorithms for calculating area.

 

design strategy

An object-oriented system may use an abstract base class to provide an appropriate, universal and standardized interface for all external applications. Then, the derived class inherits all similar operations by inheriting the abstract base class.

Functions provided by external applications (i.e. public functions) exist in the form of pure virtual functions in abstract base classes. These pure virtual functions are implemented in the corresponding derived classes.

This architecture also makes it easy for new applications to be added to the system, even after the system is defined.

 

If you encounter difficulties in self-study, you want to find oneC + + learning environment, you can join our C / C + + technology exchange group, click me to join ~ will save a lot of time, with the help of professional Tauren Daniu, to overcome many problems encountered in learning.

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]