Monday 26 April 2021

Referencing Subclass objects with Subclass and Superclass reference in C++ Qus - 1

  class A


{


public:

  

  void show()


  {


      cout<<"Hello"<<endl;


  }


};

class B: public A           //Inheritance Visibility Mode must be public for this qus


{


    public:


     void show()


  {


      cout<<"India"<<endl;


  }


   };


int main() {


    B *b=new B;


    b->show();


    A *b1=new B;


     b1->show();


   //  B *b1=new A;

 //Error -  Derived class pointer object cannot point to a base class object, Derived class pointer cannot point to base class.


}




Output:


India


Hello


Note:

//Super class or base class pointer can point subclass object

//Subclass dynamic or pointer object can not point to the base class object. Subclass pointer can not point to base class


No comments:

Post a Comment