Upcasting and Downcasting in C++





Upcasting :

An upcast is a cast from a derived type to one of its base classes. This cast is safe and does not require an explicit cast notation.

Upcasting is using the Super class's reference or pointer to refer to a Sub class's object. Or we can say that, the act of converting a Sub class's reference or pointer into its Super class's reference or pointer is called Upcasting.

#include<iostream>
using namespace std;
class Parent
{ int x;
  public:
 void show() 
 {
  cout<< "Super function";
 }
};

class Child : public Parent
{ 
 int y;
};

int main()
{
// Base class pointer
 Parent* ptr;    
 Child obj;
 ptr = &obj;
 
// Base class's reference
 Parent &ref = obj;
   //OR
 Parent *p = new Child();
}                

Upcasting can cause object slicing when a derived class object is passed by value as a base class object.


Downcasting :

Downcasting is an opposite process for upcasting.

  • It converts base class pointer to derived class pointer.

  • Downcasting must be done manually. It means that you have to specify explicit type cast.
    The reason for this restriction is that the is-a relationship is not, in most of the cases, symmetric. A derived class could add new data members, and the class member functions that used these data members wouldn't apply to the base class.

  • Downcasting is not safe as upcasting.

  • A downcast is safe only if the object that's addressed at runtime is actually addressing a derived class object.

In the above example, we derived Child class from a Parent class, adding a member function, gotoSchool(). It wouldn't make sense to apply the gotoSchool() method to a Parent object. However, if implicit downcasting were allowed, we could accidentally assign the address of a Parent object to a pointer-to-Child


Child *ch =  &p; 
// actually this won't compile
// error: cannot convert from 'Parent *' to 'Child *'                        
                    

Because a Parent isn't a Child (a Parent need not have a gotoSchool() method), the downcasting in the above line can lead to an unsafe operation.

So, here you have to perform explicit conversion like this:


Child *ch =  (Child *)p;                        
                    

prowessapps.in Note:
If you do upcasting then you can only call those properties those are inherited or override from base class, derived class's property con not be accessible.


Example:Program to understand the concept
#include<iostream>
using namespace std;
class Parent
{ int x;
  public:
 void show() 
 {
  cout<< "Parent Function";
 }
};

class Child : public Parent
{ 
 int y;
 public :
 void display()
  {
   cout<<"Child's Function";
  }
};

int main()
{
//Upcasting(implicit)
  Parent *p = new Child() ;
  p->show();
 // p->display();//ERROR
  
//Donwcasting (Explicit)
  Child *ch = (Child *)p;
  ch->show();
  ch->display();
return 0;
}                        
                    

Next topic is Virtual Destructor





 





Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved