Home c++ What is an abstract class in C++

What is an abstract class in C++

Author

Date

Category

What is an abstract class in C++?


Answer 1, authority 100%

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 it is necessary to create a family of classes (many kinds of monsters in the game ), and it would be better to move the overall implementation and behavior into a separate class. With this tactic, you will only have to override / add methods specific to each class (each monster has its own hit / move animation ) and / or extend the functionality of the class.

But there is an opportunity to create an abstract class, which contradicts the architecture: how can a “dedicated common part” be a full-fledged class? Answer: no way, it is necessary to forbid the creation of such a class . To do this, indicate one of the methods as pure virtual (pure virtual method ), example for C++:

virtual void f () = 0;

thereby forcing the derived classes to define the implementation for the given method. It is clear that such a “constraint” causes dependency and beats flexibility, therefore in 90% of cases a destructor is made using this method, since someone else, but a destructor in classes where there is inheritance and virtual methods is needed always. 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 destructor implementation , if it is defined as purely virtual in an abstract class:

virtual ~ IUnit () = 0
{
}

A class or its inheritor 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 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 a public section, 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 inherits (for C++ note # 1)

Note # 1: since in C++ there is no concept of a language-level interface , 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 & amp; 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 & amp; moveToPoint);
};
class Archer: public AbstractUnit
{
public:
  Archer ()
  {
    setHP (155);
    setSpeed ​​(400);
  }
  ~ Archer ()
  {
  }
void animateMoveToPoint (const Point2D & amp; moveToPoint) override
  {
    // play particle
    // play sound
    // run skeleton animation
  }
};

An interesting way for me to use an abstract class in conjunction with this pattern is Template Method Design Pattern .


Answer 2, authority 22%

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions