Apply class child to parent class

this is just a sample code

 class parent{ //abstact class
        //pure virtual function
        virtual fun=0;
    }
    class child : parent{
        fun;
    }
    main()
    {
           //what should i do here,so i can add parent in vector
        attach(child);
    }
    void attach(parent* p){
        vector.push_back(p); //want to add reference of parent into vecotr
    }

      

and I want to throw a child at a parent, but I can't please help me?

+2


source to share


2 answers


The child instance has a parent element type (and a child element). If you have a child instance, there is no extra parent instance. You can use a child instance wherever a parent instance is required. No need to quit.



+5


source


Class cast excetion :
 Occurs when u try to cast a parent class into child class.
 Reason: the parent class has not everything that a child class has, on the other hand a       child has everything that a parent has so you can cast a child into parent.
         In other words, the instance that you want to downcast  must be an instance of the class that to which you are downcasting.

      



0


source







All Articles