| override-final/prog1.cc - Illustration of how to prevent overriding a virtual function. | Lecture 5 - slide 13 : 40 Program 1 |
// Illustration of how to prevent overriding of virtual functions in a class A, B, C hierarchy.
// Virtual B::vf is final. Prevents overriding of vf in class C.
#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 : public A {
private:
double b;
public:
B(double b): A(b-1), b(b){};
void vf() override final {
cout << "Virtual vf from B" << endl;
}
};
class C : public B {
private:
double c;
public:
C(double b): B(b-1), c(b){};
void vf() override{ // error: overriding final function 'virtual void B::vf()'
cout << "Virtual vf from C" << endl;
}
};
void f(){
A *a1 = new C(5.0);
a1->vf();
delete a1;
}
int main(){
f();
}