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

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows