Upcasting and Downcasting in C++17 Mar 2025 | 4 min read 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, parent_obj and child_obj, which can be cast into the Parent to Child and Child to Parent using the Upcasting and Downcasting in C++ programming. ![]() UpcastingIt 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. 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 Output It is the Super function of the Base class DowncastingThe 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. 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 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 Output I am in the Parent class I am in the Parent class I am in the Myson class |
Shell sort in C++
In computer science, sorting algorithms are frequently used to arrange data in a specific order. There are many kinds of sorting algorithms, and each has advantages and disadvantages of its own. The Shell sort is one of the most widely used sorting algorithms, sometimes referred to...
4 min read
std::is_trivially_assignable in C++
C++ is a strong and adaptable programming language that offers developers a lot of capabilities. Support for low-level programming and performance optimization is one of the main features of C++. One essential part of C++ is the Standard Template Library (STL), which provides a set of...
4 min read
Convert Scientific Notation to Decimal form in C++
In this article, we will discuss how to convert Scientific Notation to Decimal Form in C++. The scientific notation is in the form of a*(10) ^b. Where a is the coefficient, b is the exponent. The product of the coefficient and the exponent gives the decimal form...
3 min read
Clone a Linked List with next and Random Pointer in C++
Clone a Linked List with and Random Pointer in C++ In this article, you will learn about how to clone a linked list with and Random Pointer in C++. Each node in a linked list of size N has two connections: one points to the node...
3 min read
Algorithmic Trading with C++
In this article, you will learn about the algorithm trading with C++ with its examples, advantages, and disadvantages. Introduction: Algorithmic trading has become increasingly popular in financial markets, with traders leveraging computer algorithms to execute strategies with speed and precision. This guide outlines the process of implementing algorithmic...
15 min read
DFA-based division in C++
In this article, you will learn the with its example. Using a Deterministic Finite Automaton (DFA) to Check Divisibility Division using deterministic finite automata (DFA) is a technique that can efficiently implement integer division in hardware. The basic idea is constructing a DFA that recognizes strings representing...
4 min read
Maximize the Cost of Repeated Removal of String P or its Reverse from the String S
We are given random variables, namely a and b couple of strings, namely X and Y, holding the cost. We are asked to perform the task of the minimum total price, which will be obtained after successfully removing the string X and by reversing the string...
3 min read
Detect and Remove Loop in a Linked List in C++
In this article, we will discuss how to detect and remove Loop in a Linked List in C++ with different methods. Create a function called detectAndRemoveLoop() that verifies whether a given Linked List has a loop. After that, it removes the Loop and returns true if it...
7 min read
iswblank() function in C/C++
In this article, you will learn about the with its example. iswblank() function: The C Standard Library includes the iswblank() function, which can be found in the <wctype.h> header file. Iswblank() is intended to support wide characters (wchar_t) in C, unlike isblank() in the standard <ctype.h> library....
4 min read
BigInt (Big Integers) in C++ with Examples
In C or C++, we have different types of data types like integers, long, float, characters etc. Each data type occupies some amount of memory. There is a range of numbers that can be occupied by that data type. For example, an integer occupies 4 bytes of...
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
