Explanation of creating objects of derived class by a base class (Virtual destructor concept)

I have two classes:

class A{
private:
    int a;
public:
    void display()
    {
        cout<<"A: "<<a;
    }
    A()
    {
        a=10;
    }
};
class B: public  A
{
private:
    int b;
    public:
    void display1()
    {
        cout<<"B: "<<b;
    }
    B()
    {
        b=15;
    }

};

What is the difference between A *a= new B and B *b = new B ?
Object b can access the members of both classes A &B whereas object a can access members of class A only.
However in the virtual destructor example given in: No Virtual constructors but virtual destructor or any other example for that matter, it is always shown base class creates an object of derived class. Why would this be useful? When obj a can access members of class A only, what is the need to create an object of class B?

I have not been able to think of a practical example for this.


Source: oop

Leave a Reply