What is an abstract class in C ++?
2 answers
An abstract class in C ++ is a class in which at least one pure virtual function is declared.
Description
An abstract class is used when it is necessary to create a family of classes ( many varieties of monsters in the game ), while it would be better to bring the overall implementation and behavior into a separate class. With this tactic, it is only necessary to redefine / add to each class-specific methods ( each monster has its own percussion / movement animation ) and / or expand the class functionality.
But it is possible to create an abstract class, which is contrary to architecture: how can the “dedicated common part” be a full-fledged class? Answer: no way, it is necessary to prohibit the creation of such a class . To do this, specify one of the methods as pure virtual ( pure virtual method ), an example for C ++:
virtual void f() = 0;
thereby forcing the heirs classes to determine the implementation for this method. It is clear that such a "restriction" causes dependence and beats on flexibility, therefore in 90% of cases this method is done by a destructor , because someone who, and the destructor in classes where there is inheritance and virtual methods is always needed. But do not forget that when the destructor is called, the class calls all the destructors of its parents, and therefore we are obliged to write the implementation of the destructor , if it is defined as purely virtual in the abstract class:
virtual ~IUnit() = 0 { }
A class or its successor ceases to be abstract for the compiler and an instance of such a class can be created as soon as the implementation for each pure virtual method is defined.
Difference from the interface
Since the concepts of "interface" and "abstract class" are confused, I will give their differences:
- Each interface is an abstract class, not every abstract class is an interface (note # 1).
- The interface contains only the public section, the abstract class has no restrictions.
- The interface contains only pure virtual methods, the abstract class can contain both fields and ordinary methods in addition.
- The interface is implemented, the abstract class is inherited (for C ++ note # 1)
Note # 1: since in C ++ there is no concept of an interface at the language level , programmers simulate its behavior through an abstract class, inheriting it.
Example
class AbstractUnit { private: int m_hp; double m_speed; protected: virtual void animateMoveToPoint( const Point2D& moveToPoint ) = 0; public: virtual ~AbstractUnit() = 0 {} void setHP(const int hp); int getHP() const; void setSpeed( const double speed ); double getSpeed() const; void runTo( const Point2D& moveToPoint ); }; class Archer : public AbstractUnit { public: Archer() { setHP( 155 ); setSpeed( 400 ); } ~Archer() { } void animateMoveToPoint( const Point2D& moveToPoint ) override { // play particle // play sound // run skeleton animation } };
An interesting, as for me, way to use an abstract class in conjunction with this Pattern Method Pattern Pattern .
- A little bit on the topic: stackoverflow.com/q/235352/10105 - VladD
надо запретить создавать подобный класс. Для этого указывают один из методов как чисто виртуальным
надо запретить создавать подобный класс. Для этого указывают один из методов как чисто виртуальным
- I will add: if purely virtual methods are not provided, you can make all the designers protected. - ߊߚߤߘ- " it is necessary to prohibit the creation of a similar class. To do this, indicate one of the methods as purely virtual " - this statement is simply wrong. Yes, in this way you can prohibit the creation, but the purpose of purely virtual methods is not at all that. Their meaning is primarily in the definition of a common interface hierarchy. And it is possible to simply prohibit the creation of a protected constructor, for example, and for this, you definitely should not artificially introduce some purely virtual functions. - freim
An abstract class in C ++ is a class in which at least one pure virtual function is declared.