-6

We all know a basic example of an Animal and then Cat and Dog classes. But I don't get one thing, for example let's say that Animal is a pure abstract class that has only one method:

    virtual void sayHello() = 0;

Then we make classes Cat and Dog and we still need to overwrite the method in their classes, so what's basically the purpose of it, becasue for now I see only additional code. I had one example with BankAccount class, we made 2 pure virtual classes only with methods: one to freeze an account and 2nd to put out the message about deposit. Then we made SavingsAccount class, which inherited all previously mentioned class and still I had to overwrite these pure virtual methods. Even If I did another i.e. account in foreign currency CurencyAccount, I still would have to overwrite the pure virtual method. So what's the purpose of making these pure virtual classes and methods?

NGC 6543
  • 67
  • 1
  • 1
  • 6
  • 4
    why not google "What is polymorphism for"? – Andrey Chernukha Nov 08 '18 at 12:34
  • 3
    @AndreyChernukha Sometimes ppl dont know how a certain "thing" is called. I hope the answer wont be needed now, then. – Croolman Nov 08 '18 at 12:35
  • Abstract classes could also be called *interface* classes. If you ever used Java then an abstract class is similar to Java interfaces. In short, they could be usd by child-classes to get a common interface. – Some programmer dude Nov 08 '18 at 12:36
  • 1
    @Croolman ok, I agree, yet it would be helpful to google "What is c++ abstract classes for". This is definitely not a SO question – Andrey Chernukha Nov 08 '18 at 12:37
  • Just a bit about terminology: "pure abstract class", as in the title, mixes two technical terms. A member function can be **pure virtual** (its declaration ends with `= 0`). A class that has one or more pure virtual functions is an **abstract class**. – Pete Becker Nov 08 '18 at 14:28

2 Answers2

1

One purpose is so you can make polymorphic code.

You can now write a function that does not need to know what kind of animal it is, each animal might have a very different implementation of its sayHello() code, which it does not make sense to share between sub-classes of Animal

void doSomething(Animal &animal)
{
    animal.sayHello();
}

If every animal has the same implementation of sayHello() though, it wouldn't make much sense to make the method pure virtual..

nos
  • 223,662
  • 58
  • 417
  • 506
  • omg I got it, you do one doSth() for whole animals and then we can call by it all animals! Thank you – NGC 6543 Nov 08 '18 at 13:16
0

It let you use inherited class' objects independently of their dynamic type.

struct Base
{
    virtual void f() const = 0;
}
struct Derived1 : Base
{
    void f() const override { /* impl */ }
}
struct Derived2 : Base
{
    void f() const override { /* impl */ }
}

void g(Base const& b)
{
    b.f();
}

As you can see, g() doesn't care if the object bound to b is actually a Derived1 or a Derived2. It calls its member function f() nonetheless. And the virtual mechanism calls the appropriate f() function.

There is a ton of actual applications, pitfalls, etc. You should really get a good C++ book. ;)

YSC
  • 38,212
  • 9
  • 96
  • 149