// Uppgift: Experiment med virtual i kombination med C++11 "delete" // Nyckelord: Arv, polymorfi, virtual, delete, abstrakt klass, #include using namespace std; class A { public: virtual ~A() {} // all subclasses of A will inherit this function void fn1() { cout << "A::fn1" << endl; } // all non abstract (an instantiatable class) subclasses // of A will inherit and must define/implement void fn2() virtual void fn2() = 0; // will not be possible to create fn3 in any subclass virtual void fn3() = delete; }; class B : public A { public: virtual ~B() {} // remove the inherited implementation of fn1 // it will not exist in class B after this void fn1() = delete; // not possible: fn2 has to be implemented (or set to 0) // void fn2() = delete; // required implementation of fn2 void fn2() { cout << "B::fn2" << endl; } }; class C : public B { public: virtual ~C() {} // new implementation of removed fn1 void fn1() { cout << "C::fn1" << endl; } // new implementation of virtual fn2 void fn2() { cout << "C::fn2" << endl; } // not possible: virtual function fn3 may not exist // void fn3() { cout << "C::fn3" << endl; } }; int main() { // A* a1 = new A(); // not possible: A is abstract A* a2 = new B(); A* a3 = new C(); B* b1 = new B(); B* b2 = new C(); C* c1 = new C(); cout << "\n--- a2 say: " << endl; a2->fn1(); a2->fn2(); cout << "\n--- a3 say: " << endl; a3->fn1(); a3->fn2(); cout << "\n--- b1 say: " << endl; // b1->fn1(); // not possible: fn1 is deleted b1->fn2(); cout << "\n--- b2 say: " << endl; // b2->fn1(); // not possible: fn1 is deleted b2->fn2(); cout << "\n--- c1 say: " << endl; c1->fn1(); c1->fn2(); // have to delete everything that was new'ed delete a2; delete a3; delete b1; delete b2; delete c1; return 0; }