This has been provided in my official notes but I spotted an error. Before I take it up to my instructor, I just thought of confirming it here with all my blood brothers- you guys.
#include <iostream.h>
class Base {
public:
virtual void display() { cout << "Base class" << endl; }
};
class Derived: public Base {
// Nothing here
};
void main()
{
Base * ptr;
// Calls Base::display ( )
ptr = new Base ;
ptr ->display();
delete ptr ;
// Calls Base::display ( ) again
ptr = new Derived ;
ptr ->display();
delete ptr ;
}
The output is:
Base class
Base class
I think the problem is in the very last line of the main function. I mean, since the destructor is not virtual, I don't think you can delete a dynamically allocated instance of the derived class type using the pointer of the base class type. What do you think?