Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'                override-final/prog2.cc - Illustration of how to prevent overriding all virtual functions.Lecture 5 - slide 13 : 40
Program 2

// Illustration of how to prevent overriding of all virtual functions.
// Class B is final. Makes every virtual function in class B final.  

#include <iostream>

using namespace std;

class A {
private:
   double a;
public:
  A(double a): a(a){};

  void virtual vf(){
    cout << "Virtual vf from A" << endl;
  }
};

class B final : public A {
private:
  double b;
public:
  B(double b): A(b-1), b(b){};

  void  vf() override{
    cout << "Virtual vf from B" << endl;
  }
};

class C: public B {            // error: cannot derive from 'final' base 'B' in derived type 'C'
private:
  double c;
public:
  C(double b): B(b-1), c(b){};

  void  vf() override{                       
    cout << "Virtual vf from C" << endl;
  }
};

void f(){
  A *a1 = new C(5.0);
  
  a1->vf();

  delete a1;
}  

int main(){
  f();
}