1

I recently came to know that in C++ pure virtual functions can optionally have a body.

I know that the body of the virtual function exist because I want to call her from the derived class, but can I do this?

class Base{
    int x;
  public:
    virtual void print()=0;
};

void Base::print(){
  cout << x;
}

class Derived : public Base{
      int y;
  public:
      void print(){
          Base::print();
          cout << y;
      }
};

And the result will be: the value of x and then the value of y?

What I really mean that the function Base::print() will know to get the value of x from function in the Derived class????

hetepeperfan
  • 4,292
  • 1
  • 29
  • 47
  • @kol : your compiler. – rems4e Aug 24 '13 at 10:54
  • @kol: yes, they can have a body which can even be invoked. :) ""Effective C++" Meyers mentions a reason for a pure virtual function to have a body: Derived classes that implement this pure virtual function may call this implementation smwhere in their code. If part of the code of two different derived classes is similar then it makes sense to move it up in the hierarchy, even if the function should be pure virtual." – László Papp Aug 24 '13 at 10:54
  • The result of doing *what* exactly? Also `Base::print()` will not be getting any values from functions in `Derived` because `Derived` has no functions that return any values - you might want to rephrase that part. – millimoose Aug 24 '13 at 10:58
  • 1
    Related: http://stackoverflow.com/questions/4174694/pure-virtual-functions-may-not-have-an-inline-definition-why – Armen Tsirunyan Aug 24 '13 at 11:05

2 Answers2

3

Output will be "12" for the following code, so yes, the pure virtual function with the body will be called, and then the derived print. So, yes, the result will be: the value of x and then the value of y just as you guessed. This is valid C++, and yes it will know how to get 'x' in that context.

Effective C++" Scott Meyers mentions a reason for a pure virtual function to have a body: Derived classes that implement this pure virtual function may call this implementation smwhere in their code. If part of the code of two different derived classes is similar then it makes sense to move it up in the hierarchy, even if the function should be pure virtual.

#include <iostream>

using namespace std;

class Base{
    int x;
  public:
    Base(): x(1) {}
    virtual void print()=0;
};

void Base::print(){
  cout << x;
}

class Derived : public Base{
      int y;
  public:
      Derived(): Base(), y(2) {}
      void print(){
          Base::print();
          cout << y;
      }
};

int main()
{
    Derived d;
    d.print();
    return 0;
}

Output: 12

László Papp
  • 51,870
  • 39
  • 111
  • 135
2

Abstract functions certainly can have an implementation and in case of an abstract destructor it is even required that the function is still implemented (making a destructor virtual doesn't mean that only the derived destructors get called, after all). ... and the implementation of an abstract function is still just a normal function having access to the members of its class.

The x is a member of Base in your example. What is surprising about Base::print() having access to a private member of Base?

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380