Upcast&Downcast
Upcast&Downcast
C++ allows that a derived class pointer (or reference) to be treated as base
class pointer. This is upcasting.
As you can see, Manager and Clerk are both Employee. They are both Person
too. What does it mean? It means that Manager and Clerk classes inherit
properties of Employee class, which inherits properties of Person class.
For example, we don’t need to specify that both Manager and Clerk are
identified by First and Last name, have salary; you can show information
about them and add a bonus to their salaries. We have to specify these
properties only once in the Employee class:
In the same time, Manager and Clerk classes are different. Manager takes a
commission fee for every contract, and Clerk has information about his
Manager:
Try It
#include <iostream>
class Person
//content of Person
};
public:
FirstName = fName;
LastName = lName;
salary = sal;
string FirstName;
string LastName;
double salary;
void show()
cout << "First Name: " << FirstName << " Last Name:
" << LastName << " Salary: " << salary<< endl;
salary += bonus;
};
public:
Commision = comm;
double Commision;
double getComm()
return Commision;
}
};
public:
manager = man;
Manager* manager;
Manager* getManager()
return manager;
};
emp->addBonus(200);
emp->show();
};
int main()
Employee* emp;
//implicit upcasting
emp = &m1;
//It's ok
cout<<emp->FirstName<<endl;
cout<<emp->salary<<endl;
//cout<<emp->getComm();
congratulate(&c1);
congratulate(&m1);
cout<<"Manager of "<<c1.FirstName<<" is "<<c1.getManager()-
>FirstName;
Both upcasting and downcasting do not change object by itself. When you use
upcasting or downcasting you just “label” an object in different ways.
UPCASTING
Upcasting is a process of treating a pointer or a reference of derived class
object as a base class pointer. You do not need to upcast manually. You just
need to assign derived class pointer (or reference) to base class pointer:
Employee* emp;
//implicit upcasting
emp = &m1;
When you use upcasting, the object is not changing. Nevertheless, when you
upcast an object, you will be able to access only member functions and data
members that are defined in the base class:
//It's ok
emp->FirstName;
emp->salary;
emp->getComm();
emp->show();
emp->addBonus(200);
};
This function will work with all the classes that are derived from the Employee
class. When you call it with objects of type Manager and Person, they will be
automatically upcasted to Employee class:
//automatic upcasting
congratulate(&c1);
congratulate(&m1);
Try to run this program:
Happy Birthday!!!
First Name: Kevin Last Name: Jones
Happy Birthday!!!
First Name: Steve Last: Name Kent
An example about how to use upcasting with virtual functions is described in
“C++ Polymorphism” topic.
Memory layout
As you know, derived class extends properties of the base class. It means
that derived class has properties (data members and member functions) of
the base class and defines new data members and member functions.
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.
Downcasting is not safe as upcasting. You know that a derived class object
can be always treated as base class object. However, the opposite is not
right. For example, a Manager is always a Person; But a Person is not always
a Manager. It could be a Clerk too.
Employee* emp;
//implicit upcasting
emp = &m1;
Manager* m2 = (Manager*)(emp);
This code compiles and runs without any problem, because emp points to an
object of Manager class.
What will happen, if we try to downcast a base class pointer that is pointing
to an object of base class and not to an object of derived class? Try to
compile and run this code:
Manager* m3 = (Manager*)(&e1);
You can use a safe cast that can help you to know, if one type can be
converted correctly to another type. For this purpose, use dynamic cast.
Dynamic Cast
dynamic_cast is an operator that converts safely one type to another type. In
the case, the conversation is possible and safe, it returns the address of the
object that is converted. Otherwise, it returns nullptr.
dynamic_cast<new_type> (object)
If you want to use dynamic cast for downcasting, base class should be
polymorphic – it must have at least one virtual function. Modify base class
Person by adding a virtual function:
Manager* m3 = dynamic_cast<Manager*>(&e1);
if (m3)
else