Upcasting and DownCasting in C++

upcasting and downcasting

What is Upcasting and Downcasting in C++

In C++ we can create a pointer references between the parent and child classes and form a “is a” relation between them. On this page, we will study about the two types of casting which are used in C++.

Upcasting

In simple terms using upcasting allows us to treat the child class object as if it were the parent class object. There are two ways of creating upcasting relationship –

Way 1

Creating parent class pointer and assigning it to the base classes reference.

Example –

Parent* parentObj; // Parent class pointer 
Child childObj; // Creating child class object
parentObj = &childObj; // assigning to address reference of base class object

Way 2

Creating Parent Classes referenced object and assigning it to the child class object

Example –

Parent &parentObj; // Parent class reference 
Child childObj; // Creating child class object
parentObj = childObj; // direct assignment

Facts

  • The object is not changing
  • However, even with the child class objects we will only be able to access the data and function members of the parent class
Run
#include <iostream>
using namespace std;

// This is Parent class
class Parent
{
  public:
    void print()
    {
      cout << "Parent Class printing" << endl;
    }
};

// This is Child class
class Child : public Parent
{
  public:

  // as we see that it is already declared in the parent function
  void print()
  {
    cout << "Child Class printing" << endl; } }; int main() { Parent *parent_object; Child child_object; parent_object = &child_object; // catch of the program is here // also as we are dealing with pointers instead of . we need to use ->
  parent_object->print();

  return 0;
}

Output –

Parent Class printing

Now, as you see that even though the object address finally is of the child class, the function accessed is of the parent class this is because of upcasting in C++.

Downcasting

Downcasting is vice versa as of Upcasting. In upcasting we created parent class pointer reference to child class address reference.

We can not do this implicitly thus, we follow explicit form, which is –

// doing this will give error
Child *child = &parent;

// We have to use explicit casting as follows
Child *child = (Child) &parent; 
Let see a program that helps us understand this –

Run

#include  <iostream>
using namespace std;

// This is Parent class
class Parent
{
public:
void print()
{
cout << "Parent Class printing" << endl;
}
};

// This is Child class
class Child : public Parent
{
public:

// as we see that it is already declared in the parent function
void print()
{
cout << "Child Class printing" << endl; } }; int main() { Parent *parent_object; Child *child_object = (Child* ) &parent_object; // catch of the program is here // also as we are dealing with pointers instead of . we need to use ->
child_object->print();

return 0;
}

Output

Child Class Printing
  • In this case even though the final address is of the parent class
  • The function called is of the child class
  • We have to use explicit casting here

Sometimes, we also use dynamic casting but this is not necessary to learn in the current scope.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription