I'm learning C++ and I am trying to understand when to assign a pointer to a base class to an object created using the new
keyword as opposed to creating an object normally and setting a pointer equal to the address of the object?
Effectively is there any difference in the methods implemented below.
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() { cout<<" In Base \n"; }
};
class Derived: public Base
{
public:
void show() { cout<<"In Derived \n"; }
};
int main(void)
{
Base *bp = new Derived();
bp->show();
Derived d1;
Base *bp = &d1;
bp->show();
return 0;
}