Type Casting in C++

Type Casting in C++
Techiio-author
Written by Sagar RabidasFebruary 2, 2022
10 min read
C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Type Casting in C++

Type Casting in C++

As the name itself recommends, the typecasting method is the conversion of one variable kind to another. In different words, making an integer type variable act like every other together with character for an unmarried set of operations whilst required. So essentially there are two varieties of conversion in c++. One is known as implicit kind conversion whereas the alternative one is specific type conversion. Implicit conversion is also called automated kind conversion due to the fact it is finished through the compiler on its very own with no personal intervention. If more than one statistics type is gift implicit will paintings.

Let’s have a look at the syntax for defining typecasting in C++:

Syntax:

int num1;
float num2;
num2 = (float) num1;

In the above syntax, the value of num1 has promoted from int to float directly which is also known as standard conversion.

There is one more type of typecasting in C++ which is known as conversion using the cast operator which is like a unary operator that also converts from one to another data type. There are 4 sub-types of casting in cast operators.

  • Static Cast: It is used to cast a pointer of base class into derived class.
  • Dynamic Cast: It is used in runtime casting.
  • Constant Cast: It is used explicitly overriding constant in a cast.
  • Reinterpret Cast: It is used to change a pointer to any other type of pointer.

Examples of Type Casting in C++

Now we will see how exactly type conversion works in C++ in both implicit and explicit conversion ways through C++ programs with an explanation in detail.

Example #1

here is a c++ program to demonstrate the working of implicit & explicit type conversion:

Code:

#include <iostream>
using namespace std ;
int main()
{
int a = 15 ;
char b = 'c' ;
a = a + b ;  // implicitly conversion of a.
float z = a + 3.0 ; // implicitly conversion of z
cout << " The Value of a is = " << a << endl
<< " The Value of b is = " << b << endl
<< " The Value of z is = " << z << endl ;
return 0 ;
}

Output:

blogpost

As you can see inside the above code inside the fundamental we declared an integer with a fee same as 15 after which a man or woman b whose fee is equal to c. After that, we are changing the value of a to a plus b which is an instance of implicit kind conversion, and inside the second component wherein we're including a glide price 3.Zero to the declared integer a which is also an instance of implicit kind conversion in c programming. Eventually, we are printing the converted values of a, b, and z at the consumer display screen.

Example #2

Here is a c++ program to demonstrate the working of explicit type casting:

Code:

#include <iostream>
using namespace std ;
int main()
{
double a = 52.20 ;
// Explicit conversion from double to int
int total = (int)a + 10 ;
// Explicit conversion from double to float
float total1 = (float)a + 2.0 ;
cout << " The total value of a after conversion is = " << total  ;
cout << " \n The total value of a after conversion is = " << total1 ;
return 0 ;
}

Output:

blogpost

As you could see within the above code in the major we declared a double “ a ” with a value same as fifty-two. 20 after which an integer variable named “ total ” will help within the explicit conversion of double value to an integer price. After that one glide is asserted with the name “ total1 ” for you to additionally help in kind conversion however from double to float. After that, we are converting the price of general to a plus 10 that's an example of a specific kind of conversion, and in the 2d element wherein we are including a flow fee of 2.0 to total1. Subsequently, we're printing the transformed values of an on the user screen.

Example #3

Here is a c++ program to demonstrate the working of explicit typecasting:

Code:

#include <iostream>
using namespace std ;
class school
{
private :
int id_number ;
public :
school( int r ) : id_number( r )
{
}
void function() const
{
( const_cast <school*> (this) )->id_number = 92 ;
}
int getid_number()
{
return id_number ;
}
};
int main(void)
{
school sc( 2 ) ;
cout << " The previous id_number number is : " << sc.getid_number() << endl ;
sc.function() ;
cout << " The current id_number number is : " << sc.getid_number() << endl ;
return 0 ;
}
Output:
blogpost

As you can see within the above code within the principal we declared a personal integer variable named “ id_number ” to assist inside the explicit conversion of value all through the feature name. After that, we have declared a public constructor with the equal magnificence name “ faculty ” wherein we are passing an argument named “ r ” of the integer data kind. After that, we are asserting a regular function with the name “ feature () ” for changing the price of id_number with the help of const_cast. Then we are returning the casted value of id_number via the int getid_number () function. Sooner or later, we're printing the transformed values of id_number at the user display through int fundamental via developing an object “ sc ” of sophistication faculty. We are printing each of the id_number values wherein the first is the oldest price and the second one is the contemporary id_number price which is shown after typecasting the use of the court feature.

C
C++
Type Casting in C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance