If a base class pointer contains the address of a derived class object, then the pointer is called as Upcasted pointer & this process is called as Upcasting.

Let us look at a program to understand the concept of upcasting.

C++ Program For Upcasting

using namespace std;
class base{ 
 public: void display1(){ 
   cout<<"base - display1 n";
} void display2(){ 
   cout<<"base - display2 n";
} }; class der : public base{ 
 public: void display1(){ 
   cout<<"der - display1 n";
} void display2(){ 
   cout<<"der - display2 n";
} }; int main(){ 
base b, *bptr; 
bptr = &b;
bptr->display1(); bptr->display2();
der d; bptr = &d; //upcasting 
return 0; }

Output:-base – display1
base – display2
base – display1
base – display2

Explanation of the program

We have assigned the address of a derived class object to the base class pointer. Is this not an error, since we are assigning of one type to a pointer of another?

No, the rule is that derived object can be treated as a base object as well since derived object contains the base part.

So, in this case, the compiler relaxes the type checking. Note that we have used the following statement.

This article is written by our awesome writer
Comments to: Upcasting in C++ with Simple Example

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories


Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page


Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
    Strength indicator
    Log In | Lost Password