This segment focuses on the concepts of Upcasting and Downcasting in the C++ programming language. When converting one data type to another, it involves a process known as typecasting. However, Upcasting and Downcasting specifically refer to object typecasting. For instance, in the scenario where there are objects of the Parent and Child classes, namely parentobj and childobj, these can be transformed into Parent to Child and Child to Parent respectively through the application of Upcasting and Downcasting in C++ programming.
Upcasting
It involves generating a pointer or reference to the derived class from the base class's pointer or reference, known as Upcasting. Upcasting refers to converting the reference or pointer of the derived class to a base class. Upcasting is considered a secure casting method when compared to downcasting. It permits public inheritance, enabling the implicit casting of references from one class to another without requiring an explicit typecast. By default, upcasting establishes an "is-a" relationship between the base and derived classes.
Base *ptr = &derived_obj;
The subclass is able to acquire all attributes from the superclass, encompassing both data fields and methods, allowing for the execution of functions using instances of the subclass, similar to how it is done with instances of the superclass.
Program to demonstrate the Upcasting in C++
Let's explore an illustration of converting a pointer of the derived class to a pointer of the base class 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 process of Downcasting serves as the reverse of upcasting, transforming the pointer or reference of the base class into the pointer or reference of the derived class. It involves manually converting the object of the base class to the object of the derived class, necessitating the explicit specification of the typecast. Unlike upcasting, downcasting typically does not adhere to the is-a relationship and is considered less secure. Additionally, the derived class has the capability to introduce new features like additional data members and class methods that utilize these members. However, these new functionalities are not applicable to the base class.
Derived *d_ptr = &b_obj;
Program to demonstrate the downcasting in C++
Let's generate an illustration demonstrating the downcasting of an object from the base class 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 explore an illustration demonstrating the application of downcasting and upcasting in C++ to convert an object of the base class to the derived class and vice versa.
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