[C++ Deep Analysis Tutorial 30] Abstract Classes and Interfaces in C++

Add qq1126137994 WeChat: liu1126137994

1. Abstract concepts in object-oriented;
write picture description here

In reality, you need to know the specific type of figure to know how to find the area, but for an abstract 'figure', we cannot find its area, and it is meaningless to find its area:

class shape
{
public:
    double area()
    {
        return 0;
    }
};

shape is just a conceptual type, no objects! ! !
2. So what is an abstract class?

Abstract classes in object orientation:

  • Can be used to represent abstract concepts in the real world
  • is a type that can only define types but cannot produce objects
  • Can only be inherited and overridden related functions
  • The direct feature is that the related functions are not fully implemented

The shpe mentioned above is an abstract graphic concept in the real world:
therefore:

  • The program must reflect abstract graphic concepts
  • The concept of graphics represented by abstract classes in the program
  • Abstract classes cannot be used to create objects, they can only be used for inheritance

3. How to implement abstract classes in C++:

  1. C++ implements abstract classes through pure virtual functions
  2. A pure virtual function refers to a member function that only defines the function prototype
  3. A C++ class with pure virtual functions constitutes an abstract class

    4. Syntax rules for pure virtual functions:
    write picture description here

A first look at abstract classes:

#include <iostream>
#include <string>

using namespace std;

class Shape
{
public:
    virtual double area() = 0;
};

class Rect : public Shape  //定义矩形
{
    int ma;
    int mb;
public:
    Rect(int a, int b)  
    {
        ma = a;
        mb = b;
    }
    double area()
    {
        return ma * mb;
    }
};

class Circle : public Shape
{
    int mr;
public:
    Circle(int r)
    {
        mr = r;
    }
    double area()
    {
        return 3.14 * mr * mr;
    }
};

void area(Shape* p)
{
    double r = p->area();

    cout << "r = " << r << endl;
}

int main()
{
    Rect rect(1, 2);
    Circle circle(10);

    area(&rect);
    area(&circle);

    return 0;
}

summary:

  • Abstract classes can only be inherited as parent classes
  • Subclasses must implement the specific functions of pure virtual functions
  • After a pure virtual function is implemented, it becomes a virtual function
  • If the subclass does not implement a pure virtual function, then the subclass becomes an abstract class

5. What is an interface?

A C++ class that satisfies the following conditions becomes an interface:

  • There are no member variables defined in the class
  • All member functions are public
  • All member functions are pure virtual functions
  • An interface is a special kind of abstract class

A preliminary exploration of the interface:

#include <iostream>
#include <string>

using namespace std;

class Channel
{
public:
    virtual bool open() = 0;
    virtual void close() = 0;
    virtual bool send(char* buf, int len) = 0;
    virtual int receive(char* buf, int len) = 0;
};

int main()
{
    return 0;
}

6. Summary:

  1. Abstract classes are used to describe abstract concepts in the real world
  2. Abstract classes can only be inherited and cannot create objects
  3. There is no concept of abstract class in C++
  4. Implementing abstract classes through pure virtual functions in C++
  5. A class with only pure virtual functions is called an interface
  6. An interface is a special kind of abstract class

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324841289&siteId=291194637