Article Directory
1 Abstract class
1.1 Why are there abstract classes
When performing object-oriented analysis, there will be some abstract concepts. As shown in the figure below, the rectangular class and the circular class are subclasses of the shape class, and they all have a function to find the area. However, it only makes sense to find the area of a specific shape, and it is meaningless to find the area of the conceptual "graph"!
Graphics is just a conceptual type, there is no specific object. However, there is still a need for graphics.
The graphics class here should be designed as an abstract class, used to represent abstract concepts in the real world. There are no specific objects, and the related functions are not fully implemented, so they can only be inherited and rewritten.
1.2 Abstract classes and pure virtual functions
We learned virtual functions in the past, and they are implemented through the virtual keyword. Without giving the realization of the function body, directly "= 0" is a pure virtual function
The syntax rules are as follows:
"= 0" tells the compiler that it is currently declaring a pure virtual function, without defining the function body
Abstract class
- Classes with pure virtual functions are abstract
- Pure virtual functions are member functions that only define prototypes, no implementation is given
- A pure virtual function becomes a virtual function after being implemented
- If the subclass does not implement all pure virtual functions, the subclass is still an abstract class
// 37-1.cpp
#include<iostream>
using namespace std;
class Shape
{
public:
virtual double area() = 0;
};
class Rect : public Shape
{
public:
Rect(int a, int b) : ma(a), mb(b) {}
double area()
{
return ma * mb;
}
private:
int ma;
int mb;
};
class Circle : public Shape
{
public:
Circle(int r) : mr(r) {}
double area()
{
return 3.14 * mr * mr;
}
private:
int mr;
};
void area(Shape* p)
{
double s = p->area();
cout << "area = " << s << endl;
}
int main()
{
Rect rect(1, 2);
Circle circle(10);
area(&rect);
area(&circle);
return 0;
}
Shape is an abstract class and cannot generate objects. This avoids generating Shape objects without knowing them, because Shape objects are meaningless. After a pure virtual function is implemented, it becomes a virtual function, which implements polymorphism. An object of a pointer subclass of an abstract class can dynamically call a member function of a subclass according to the object type to find the area of a specific shape.
$ g++ 37-1.cpp -o 37-1
$ ./37-1
area = 2
area = 314
2 Interface
C ++ classes that meet the following conditions become interfaces
- No variables are defined in the class
- All member functions are pure virtual functions and are public
We implement an interface as follows:
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;
};
There are no member variables, only public pure virtual functions.
3 Summary
1. Abstract classes are used to describe abstract concepts in the real world
2. Abstract classes are implemented by pure virtual functions
3. Abstract classes can only be inherited and cannot create objects
4. Classes where only pure virtual functions exist are interfaces