Dynamic _Cast in C++
C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic cast in C++. Before that, we will learn about typecasting in C++.
What is Type Casting?
Casting is a technique in which we can use one data type in another data type. The operator used in the casting technique is known as the cast operator. The cast operator is a unary operator. The conversion of one data type to another data type is the main aim of the casting.
The syntax for casting one operator into another operator is as follows.
Syntax:
(Cast type) expression;
or
Cast type (expression)
Example 1:
#include <iostream>
using namespace std;
int main()
{
int p, q;
float r;
p = 13;
q = 24;
r = (float)p * q;
cout << "The Result Is: " << r;
return 0;
}
Output:
The Result Is: 312
Explanation
In the above example, first, the variable p is converted to float then multiplied by q, now the result is also floating float then the result is assigned to the variable r. Now, the value of r is 312.
Example 2:
#include <iostream>
using namespace std;
int main()
{
int p = 11, q = 3;
float r;
r = p / q;
cout << "The Result Is:" << r;
return 0;
}
Output:
The Result Is: 312
Explanation
In the above case, the variable r is 3, not 3.6, because variables p and q both are integer types therefore p/q is also an integer type. After calculating p/q that is int type is assigned to the variable r which is float type. But p/q is int type i.e., 11/3 is 3, not 3.6. Therefore, the value of variable r is 3.
Example 3:
#include <iostream>
using namespace std;
int main()
{
int p = 11, q = 3;
float r;
r = (float)p / q;
cout << "The Result Is :" << r;
return 0;
}
Output:
The Result Is: 3.66667
Explanation
Now, the variable r is 3.6, because in the above expression first p is converted into float therefore p/q is also float type. 11.0/3 is 3.6. Then that is assigned to the variable r.
C++ supports four types of the casting. These are as follows:
- Static cast
- Dynamic cast
- Const cast
- Reinterpret cast
Static Cast:
It is the simple type of cast used in the C++ programming language for casting. It is a compile-time cast. The conversion of int to float and void to pointer is done by static casting. It is also known as the explicit conversion function.
Dynamic Cast:
It is a type of operator that is used to convert data from one type to another type. It can be used for down casting at run time. We need one virtual function at the base class of programming for working on dynamic type casting. It works on a polymorphic base of the programming language.
Syntax:
dynamic_cast <new_type>(Expression)
- Downcasting: The casting of a base class reference for the building of class reference is known as downcasting.
- Upcasting: The casting of the derived class reference to a base class reference is known as upcasting.
Example 4:
#include <iostream>
using namespace std;
class Base {
virtual void print()
{
cout << "Base" << endl;
}
};
class Derived1 : public Base {
void print()
{
cout << "Derived1" << endl;
}
};
class Derived2 : public Base {
void print()
{
cout << "Derived2" << endl;
}
};
int main()
{
Derived1 d1;
Base* bp = dynamic_cast<Base*>(&d1);
Derived1* dp2 = dynamic_cast<Derived1*>(bp);
if (dp2 == nullptr)
cout << "null" << endl;
else
cout << "not null" << endl;
return 0;
}
Output:
not null
Explanation
In this program, there is one base class and two derived classes (Derived1, Derived2), here the base class pointer hold derived class 1 object (d1). At the time of dynamic_casting base class, the pointer held the Derived1 object and assigning it to derived class 1, assigned valid dynamic_casting.