Sunday, February 25, 2018

C++ : Polymorphism - Virtual Destructors illustration through program

#include <iostream>
using namespace std;
class B {
public:
    B() { cout << "98 "; }
virtual ~B() { cout << "56 "; } // Don't Edit or Modify the "cout"

}; // End of class B

class D : public B {
    int n;
public:
    D(int p):n(p) { cout << n << " "; }
    ~D() { cout << n*2 << " "; }
}; // End of class D

int main() {
    int n ; cin >> n ;
    B *basePtr = new D(n);
    delete basePtr;
    return 0;
}

No comments:

Post a Comment

Sacred Thought

28 April 2024 Today I am going to explain verse 31 - 38 chapter two for you all. There is no opportunity better than a righteous war (सत्य औ...