I'm newbie in C++. I'm learning c++ oops concepts. Is it allowed to using Derived class(D) allocate the memory of Base class(B) pointer?
B *a = new D();
My code:
#include <iostream>
using namespace std;
class B
{
public:
B()
{
cout<<"B constructor"<<endl;
}
~B()
{
cout<<"B Destroctur"<<endl;
}
};
class D : public B
{
public:
D()
{
cout<<"D constructor"<<endl;
}
~D()
{
cout<<"D Destroctur"<<endl;
}
};
int main()
{
B *a = new D();
delete a; // Is valid?
}
Also, Is it valid to free the memory of base class pointer?
delete a;