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

What is a data structures

  Data structure is a tool in the hands of a programmer for  storing a large number of data items  in the main memory (RAM) of a computer wh...