This section will discuss Upcasting and Downcasting with an example in the C++ programming language. When we convert one data type into another type, the process is called typecasting. But the, Upcasting and downcasting are the types of object typecasting. Suppose the Parent and Child class has two types of object, parentobj and childobj, which can be cast into the Parent to Child and Child to Parent using the Upcasting and Downcasting in C++ programming.
Upcasting
It is the process to create the derived class's pointer or reference from the base class's pointer or reference, and the process is called Upcasting. It means the upcasting used to convert the reference or pointer of the derived class to a base class. Upcasting is safe casting as compare to downcasting. It allows the public inheritance that implicitly cast the reference from one class to another without an explicit typecast. By default, the upcasting create is-a relationship between the base and derived classes.
Base *ptr = &derived_obj;
The derived class can inherit all the base class properties that include data members and the member function to execute the function using the derived class object, as we do with a base object.
Program to demonstrate the Upcasting in C++
Let's consider an example to convert the derived class's pointer to the base class's pointer in the C++ programming language.
Program1.cpp
#include <iostream>
using namespace std;
class Base
{
public:
void disp()
{
cout << " It is the Super function of the Base class ";
}
};
class derive : public Base
{
public:
void disp()
{
cout << "\n It is the derive class function ";
}
};
int main ()
{
// create base class pointer
Base *ptr;
derive obj; // create object of derive class
ptr = &obj; // assign the obj address to ptr variable
// create base class's reference
Base &ref = obj;
// Or
// get disp() function using pointer variable
ptr->disp();
return 0;
}
Output
It is the Super function of the Base class
Downcasting
The Downcasting is an opposite process to the upcasting, which converts the base class's pointer or reference to the derived class's pointer or reference. It manually cast the base class's object to the derived class's object, so we must specify the explicit typecast. The downcasting does not follow the is- a relation in most of the cases. It is not safe as upcasting. Furthermore, the derived class can add new functionality such as; new data members and class member's functions that use these data members. Still, these functionalities could not apply to the base class.
Derived *d_ptr = &b_obj;
Program to demonstrate the downcasting in C++
Let's create an example to downcast the base class's object to the derived class in the C++ programming language.
Program2.cpp
#include <iostream>
using namespace std;
class Parent
{
public:
void base()
{
cout << " It is the function of the Parent class "<< endl;
}
};
class Child : public Parent
{
public:
void derive()
{
cout << " it is the function of the Child class " <<endl;
}
};
int main ()
{
Parent pobj; // create Parent's object
Child *cobj; // create Child's object
// explicit type cast is required in downcasting
cobj = (Child *) &pobj;
cobj -> derive();
return 0;
}
Output
It is the function of the Child class
Program to demonstrate the upcasting and the downcasting in C++
Let's consider an example to use the downcasting and the upcasting in C++ to convert the base class to derive and the derived class's object to the base class.
Program3.cpp
#include <iostream>
using namespace std;
class Parent {
private:
int id;
public:
void showid ()
{
cout << " I am in the Parent class " << endl;
}
};
class Myson : public Parent {
public:
void disp ()
{
cout << " I am in the Myson class " << endl;
}
};
int main ( int argc, char * argv[])
{
// create object of the Parent class
Parent par_obj;
// create object of the Myson class
Myson my_obj;
// upcast - here upcasting can be done implicitly
Parent *ptr1 = &my_obj; // base class's reference the derive class's object
// downcast - here typecasting is done explicitly
Myson *ptr2 = (Myson *) &par_obj;
// Upcasting is safe:
ptr1->showid();
ptr2->showid();
// downcasting is unsafe:
ptr2->disp();
getchar();
return 0;
}
Output
I am in the Parent class
I am in the Parent class
I am in the Myson class