Michael
Michael

Reputation: 1498

Why does an interface needs to be redeclared?

I have an abstract base class and want to implement a function in the derived class. Why do I have to declare the function in the derived class again?

class base {
public:
    virtual int foo(int) const = 0;
};

class derived : public base {
public:
    int foo(int) const; // Why is this required?
};

int derived::foo(int val) const { return 2*val; }

Upvotes: 6

Views: 457

Answers (6)

Jorge Rodriguez
Jorge Rodriguez

Reputation: 613

While you can't instantiate a class with pure virtual functions, you can still create a class like this:

class base {
public:
    virtual int foo(int) const = 0;
};

class derived : public base {
public:
};

class very_derived : public derived {
public:
    virtual int foo(int) const { return 2; }
};

The derived class is still an abstract class, it can't be instantiated since it doesn't override foo. You need to declare a non-pure virtual version of foo before you can instantiate the class, even if you don't define foo right away.

Upvotes: 1

Matthieu M.
Matthieu M.

Reputation: 299930

Because the hierarchy could have more layers.

struct Base {
    virtual void foo() const = 0;
    virtual void bar() const = 0;
};

struct SuperBase: Base {
     virtual void bar() const override;
};

struct Concrete: SuperBase {
     virtual void foo() const override;
};

Here, SuperBase does not provide an implementation for foo, this needs be indicated somehow.

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272537

Consider that the derived-class definition might be in a header, whereas its implementation may be in a source file. The header typically gets included in multiple locations ("translation units"), each of which will be compiled independently. If you didn't declare the override, then the compiler wouldn't know about it in any of those other translation units.

Upvotes: 7

Alok Save
Alok Save

Reputation: 206546

The intention of making a function pure virtual in Base class is that the derived class must override it and provide its own implementation.
Note that presence of an pure virtual function in the class makes that class an Abstract class. In simple terms the class acts as an interface for creating more concrete classes.One cannot create objects of an Abstract class.

If you do not override the pure virtual function in derived class then the derived class contains the inherited Base class pure virtual function only and it itself acts as an Abstract class too.Once your derived class is abstract it cannot be instantiated.
So in order that your derived class be instantiated it needs to override and hence declare the pure virtual function.

Upvotes: 4

Michael Anderson
Michael Anderson

Reputation: 73490

You might think the compiler can deduce that you're going to have to provide an implementation of derived::foo(), but derived could also be an abstract class (and in fact that's what you'll get if you dont declare foo() in derived)

Upvotes: 3

Ozair Kafray
Ozair Kafray

Reputation: 13539

It is to override the abstraction of the base class.

If you do not re-declare it, then your derived class is also an abstract class. If you do then you now have a non-abstract type of the base.

Upvotes: 1

Related Questions