#include using namespace std; class A{ public: //virtual ~A()=default; virtual ~A() { cout << "In base class destructor" << endl; } void Method1(void) const { cout << "Method 1: In the base class A" << endl; } virtual void Method2(void) const { cout << "Method 2: In the base class A" << endl; } virtual void Method3(void) const { cout << "Method 3: In the base class A" << endl; } void Method4(void) const { cout << "Method 4: In the base class A" << endl; } }; class B :public A{ public: ~B() { cout << "In Derived class destructor" << endl; } void Method1() const { cout << "Method 1: In the derived class B" << endl; } void Method2(void) const override { cout << "Method 2: In the derived class B" << endl; } /* void Method3(int x) const override { cout << "Method 3: In the derived class B" << endl; }; */ }; class C : public B { public: void Method2(void) const override { } }; int main() { A a; B b; A * ptr1 = new A; A * ptr2 = new B; cout << "Calling from an instance of A" << endl; a.Method1(); a.Method2(); cout << endl; cout << "Calling from an instance of B" << endl; b.Method1(); b.Method2(); // b.Method3(); b.Method4(); cout << endl; cout << "Calling from a ptr to A, stored as an A*" << endl; ptr1->Method1(); ptr1->Method2(); cout << endl; cout << "Calling from a ptr to B, stored as an A*" << endl; ptr2->Method1(); ptr2->Method2(); ptr2->Method3(); ptr2->Method4(); cout << endl; cout << "Deleting Ptr1 A*->A * " << endl; delete ptr1; cout << "Deleting Ptr2 A*->B * " << endl; delete ptr2; cout << endl << endl; cout << "Exiting the program " << endl; return 0; }