What is an abstract class in C++?

What is an abstract class in C++?

Author: gmontekrissto, 2010-10-21

2 answers

An abstract class in C++ is a class in which at least one purely virtual function is declared.


Description

An abstract class is used when you need to create a family of classes (there are many varieties of monsters in the game ), and it would be better to put the general implementation and behavior of in a separate class. With this tactic, you will only have to redefine / add methods specific to each class (each monster has its own animation of impact/movement) and / or extend the functionality of the class.

But there is an opportunity to create an abstract class, which is contrary to the architecture: how can a "dedicated common part" be a full-fledged class? Answer: no, it is necessary to prohibit creating 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 inheriting classes to define an implementation for the given the method. It is clear that such a "restriction" causes dependency and affects flexibility, so in 90% of cases, this method is made destructor, since who is who, and a destructor in classes where there is inheritance and virtual methods is always needed. But do not forget that when calling the destructor, the class calls all the destructors of its parents, and therefore we are bound to write an implementation of the destructor, in the case of defining it as purely virtual in the abstract class:

virtual ~IUnit() = 0 
{
}

A class or its descendant 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:

  1. Every interface is an abstract class, not every abstract class is an interface (note #1).
  2. The interface contains only public the abstract class has no restrictions.
  3. The interface contains only pure virtual methods, an abstract class can contain both fields and regular methods in addition.
  4. The interface is implemented, the abstract class is inherited (for C++, approx. #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 his.


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 way to use an abstract class in conjunction with this pattern is Template Method Design Pattern.

 23
Author: rikimaru2013, 2019-04-07 08:52:41

An abstract class in C++ is a class in which at least one purely virtual function is declared.

 5
Author: Nicolas Chabanovsky, 2010-10-22 06:43:14