C++

Dynamic_Cast C++

The dynamic cast in the C++ programming language will be discussed in this article. To begin, we must understand the notion of casting and many types of casting. The process of altering a variable’s data type is known as casting. Casting is divided into two categories in the C++ programming language: implicit casting and explicit casting.

Data type conversion in C++:

Type casting is the procedure of altering the data type to some other data type. There are two types of casting or type conversion in the C++ programming language: implicit and explicit casting. Automated type conversion is another name for implicit typecasting. It is performed by the compiler during real-time compilation and does not need any user input or action. When there are two sorts of data types in an expression, this form of casting occurs. For example:

Graphical user interface, text, application Description automatically generated

In the given code, we can see that an integer variable and a character variable are inserted in the last line expression, and the value of the integer variable “i” is changed. The ASCII number equivalent of “a” will be converted to a character value and added to the integer value of the variable “i” in this statement. If the value of the variable “i” is printed, the result will be a total of both of these values. The compiler automatically transforms the character variable’s data type to an integer data type by converting it to the ASCII standard value of the alphabet on the right, which is a great example of implicit or automated type conversion on the run time.

Now, when it comes to explicit type casting or type conversion, it is not an automated process; the user must manually convert a data type of a variable to another type of variable in the code. The data types are usually arranged in a hierarchy based on their memory space and the amount of information they can hold. So, when a lower order data type is used to store any information but has to be transformed into a higher-order data type so that information loss can be minimized and more and more information can be stored, explicit type casting or type conversion is commonly done. For example, because an integer type variable cannot store values after the decimal point, we may lose some information if we continue to use integer variables. To avoid this loss, we convert the integer variable to a float variable, saving values after decimal points and preventing information loss. The explicit type conversion in the C++ programming language may be accomplished in one of two ways: via assignment or by utilizing the cast operator. The assignment conversion is done in a code expression, and the syntax for this expression is provided below.

# “(data type) expression”

In the above code, we must put a valid data type within the bracket, and after the bracket, we must write the variable or expression that we wish to modify into the data type written inside the bracket.

Now we will look into the type of conversion done by the cast operators in the C++ programming language. Cast operators are also called unary operators that force a variable to change its data type from one existing to another. There are four types of casting cast operators: Static cast, Dynamic cast, Const cast, and Re-interpret cast.

Dynamic casting in C++:

Dynamic casting in the C++ programming language is based on a concept called RTTI(Run Time Type Identification). RTTI is a function present in several programming languages like C/C++, Ada, and Object Pascal. Run-Time Type Identification or Information is a function that identifies and extracts the information regarding the details of an object’s data type on the run time of a program. This function provides a safe path for type casting methods like the “typeid” function or dynamic type casting. It detects the data type information on the run time and assists the conversion of data type when the operators are in play.

Dynamic casting is mostly utilized in C++ for run-time safe down casting. To work with the dynamic cast, the base class must have 1 virtual function. Dynamic cast only works with polymorphic base classes because it utilizes this information to determine safe down casting. A dynamic cast operator does dynamic casting. Now that we know about the concepts related to dynamic casting, we can go towards the implementation part. Let us first look at the syntax for using dynamic casting in the C++ programming language, which is written below:

# “dynamic_cast <new_type>(Expression)”

In the above expression, the first part describes the name of the operator; in the angle brackets, we write the name of the data type that we need to convert our expression into, and in the round brackets, we write the variable or object’s name that we want to convert.

Now that we know how to use the dynamic cast operator and fill in the parameters for converting data types of variables, we can use it to convert data types of variables.

Using Dynamic cast method in Ubuntu 20.04:

To implement this concept, we must use several classes to work with for converting objects of the class by inheritance. So, to do that first, we must first know that the C++ program file in the Ubuntu environment is stored with the extension “.cpp,” so to create this file on our desktop, open a terminal and type “cd Desktop” on the command line, then press enter and type “touch filename .cpp” to create a file with the “.cpp” extension. Now we will write a code in that file to create a base class and 2 derived classes, and in the driver code, we will use the dynamic cast operator.

Graphical user interface, text Description automatically generated

Exit the file after clicking the save button. Return to the terminal and build the file using the command “g++” with your file name and the “.cpp” extension. A file with the extension “.out” will be created using this command. You may now run that file by entering “./” followed by your “.out” extension.
Text Description automatically generated

In this program, the base class pointer stores derived class 1 objects (d1). The dynamic casting base class, the pointer retained the Derived1 object and allocated it to derived class 1, which provided valid dynamic casting.

Conclusion:

This article taught us the type casting method used in the C++ programming language. The type of casting was also discussed in detail, and we focused on the need for why we use such methods in C++ programming. We discussed the assist function that helps the conversion of variable’s data type by fetching information and verifying for correct conversion called RTTI. We also implemented the concept of Dynamic casting by using the dynamic cast operator in an expression in the C++ programming language in the Ubuntu 20.04 environment.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.