Back to notes -- Keyboard shortcut: 'u'              Slide program -- Keyboard shortcut: 't'    A variant were vf is a pure virtual function in class A.Lecture 4 - slide 8 : 24
Program 1
// Illustration of pure virtual functions in continuation of 
// the already shown example of virtual functions. Does not compile as shown.
// If red parts are eliminated, the program compiles.

#include <iostream>
#include <string>

using namespace std;

class A {                              // An abstract class
private:
   double a;
public:
  virtual void vf(double d) = 0;                 // A pure virtual function

  void f(double d){                              // Statically bound function f in A
    cout << "f in A" << endl;
  }
};

class B : public A {                              
private:
  double b;
public:
  void vf(double d){                             // Definition of the virtual function 
    cout << "virtual vf in B" << endl;
  }

  void f(double d){                              // Statically bound function f in B.
    cout << "f in B" << endl;
  }
};

int f1(A a){       // error: cannot declare parameter  a  to be of abstract type  A.
    a.vf(1.0);       
    a.f(2.0);        
    cout << endl;
}

int f2(A *ap){
  ap->vf(3.0);     // virtual vf in B
  ap->f(4.0);      // f in A
  cout << endl;
}

int f3(A &ar){
  ar.vf(5.0);      // virtual vf in B
  ar.f(6.0);       // f in A
  cout << endl;
}

int main(){
  A a1;            // error: cannot declare variable  a1  to be of abstract type  A.

  B  b1,
    *b2 = new B();

  f1(b1);          // error: cannot allocate an object of abstract type 'A'
  f2(b2);
  f3(b1);
}