-2

Example code:

#include <string>

namespace vehicle
{
    class Vehicle
    {
    public:
        Vehicle(int a);
        virtual ~Vehicle();   <------ not method?

    protected:
        int a;
    };
}

Also, I don't fully get the concept of a pure virtual method where you declare a method as:

virtual method() = 0;

Why do we need this?

honk
  • 9,137
  • 11
  • 75
  • 83
lukieleetronic
  • 623
  • 7
  • 10
  • 23
  • 1
    That is not the `not` operator, you need to read a [book](http://stackoverflow.com/q/388242/241631). – Praetorian Mar 12 '16 at 01:39
  • Haha True that, I'm just being too lazy here. It's right on the first page of the class chapter. Never saw this while coding in java. But having said this, I like to say that there ain't no stupid question. – lukieleetronic Mar 12 '16 at 01:44
  • You can use a pure virtual in a base class. It's used when the base class cannot possibly "know" what to do or return, but the method is required. Derived classes will implement their own version. If they don't, objects of that class cannot be instantiated. e.g. Base class Vehicle has a method Move. Derived Plane flies and Car drives. – BryanT Mar 12 '16 at 01:45

2 Answers2

1

virtual ~Vehicle();

The tilde ~ denotes a destructor. It's a method, a special method for destroy the object.

Virtual destructor is used in abstract base class.

virtual method() = 0;

That's pure virtual function. It indicates that you have to provide implementation method for the implementation class implementing this abstract base class.

artm
  • 17,291
  • 6
  • 38
  • 54
1

Let's suppose there are two classes: Base and Derived

struct Base
{
    Base() {}
    virtual void foo() = 0;
    virtual ~Base()
    {
        std::cout << "Base descructor" << std::endl;
    }
};

struct Derived : Base
{
    int *i;
    Derived(int i_): Base(), i(new int[i_]) {}
    virtual void foo() override
    {
        std::cout << "foo" << std::endl;
    }
    virtual ~Derived()
    {
        std::cout << "Derived descructor" << std::endl;
        delete [] i;
    }
};

Pure virtual functions

If Derived doesn't override the foo function you can create an instance of the Derived class. When you try, your code will not compile, because foo is a pure virtual function.

error: invalid new-expression of abstract class type 'Derived'

note: because the following virtual functions are pure within 'Derived':

struct Derived : Base

Pure virtual functions are used to describe the interface.

Now about virtual destructors:

The ~(tilda) sign is used to denote the class destructor. It is a special method that is called when the object is destroyed.

A client creates an instance of Derived:

Base *instance = new Derived;

then this variable is used somehow and when you don't need the variable you need to free the memory:

delete instance;

Let's trace the calls:

Derived descructor

Base descructor

So you can see that both base and derived destructors are called and no memory leak is possible. But if you remove this virtual keyword from the destructors

Base descructor

As you can see the descturtor of Derived is not called, so a memory leak exists (the array of Derived class don't get released).

Hence virtual constructors are useful when you work with objects through pointers.

neshkeev
  • 6,280
  • 3
  • 26
  • 47