Type Conversion in C++

Type Conversion in C++

Type conersion in C++ is a conversion from one type to another type. In Type conversion takes place when in an expression has more than one data type is present and in this condition type cast takes place to avoid lose of data.

There are two different types of type conversion they are follows

    • Implicit type conversion
    • Explicit type conversion

1. Implicit type conversion

The implicit type conversion is the type of conversion done automatically by the compiler without any human effort that is an implicit conversion automatically converts one data type into another type based on predefined rules of the C++ compiler. So it is also called as automatic type conversion.

Order of the type conversion in implicit conversion

probool -> char -> short int -> int -> unsigned int -> long int -> unsigned long int -> long  long int -> float -> double -> long double

Program of implicit conversion in C++

// program for Implicit type conversion
#include <iostream>
using namespace std;

int main()
{
  int m = 100; // integer
  char n = 'x'; // character 

  m = m + n;

  float a = m + 3.0;

  cout << "m = " << m << endl
    << "n = " << n << endl
    << "a = " << a << endl;

  return 0;
}

Output

m = 100
n = x
a = 103

2. Explicit Type Conversion

Explicit tyee conversion is user defined and it also called type casting. In this conversion the user can typecast the result to make it of a certain data type.

Explicit conversion is defined as when the user manually changes data from one type to another type of data.

Explicit type conversion can be done by two ways in C++ they are follows

    • Converting by assignment operator
    • Conversion using Cast operator

A. Conversion using the assignment operator

In this type of conversion it required the type is explicitly specified bedore paranthesis. Explicit type conversion causes loss the data. It’s also considered as “forced casting.”

Syntax of conversion using assignment operator

(type) expression

Program of Conversion using the assignment operator

// C++ program of explicit type conversion

#include <iostream>
using namespace std;

int main()
{
  double m = 3.0;

  // Explicit conversion from double to int
  int sum = (int)m + 2;

  cout << "Sum = " << sum;

  return 0;
}

Output

sum = 5

B. Conversion using Cast operator

A Cast operator is an unary operator which converted one data type into another data type.

Program of conversion using cast operator

#include <iostream>
using namespace std;
int main()
{
    float f = 3.5;
  
    // using cast operator
    int b = static_cast<int>(f);
  
    cout << b;
}

Output

3

There are four types of casts in C++ language they are follows

    • Static cast
    • Dynamic cast
    • Const cast
    • Reinterpret cast

1. Static cast

Static cast is the most basic form of cast . This cast can do upcasts and downcasts.And  in this cast takes a long time to compile. In all conversion process, no checks are made to make sure the object converting is a finish object of the target type.

Program of static cast in C++

#include <iostream>
using namespace std;
int main()
{
  float m = 7.5;

  // cast operator
  int n = static_cast<int>(m);

  cout << n;
}

Output

7

2. Dynamic cast

In dynamic cast it guaranty that the result of type conversion is a complete, valid object of target the pointer type.This cast handled with the polymorphism.It is inly used when typecasting to derived class in inheritance

 

There are two types of dynamic cast

   

 1. Pointer dynamic cast

The cast returns Null when pointer is cast and it get fails.

<type> *xsubclass = dynamic_cast<<type> *>( xobject );

   

 2. Reference dynamic cast

In this cast there is not possible to return a NULL pointer to signify failure when reference casting

<type> subclass = dynamic_cast<<type> &>( ref_obj );

3. Const cast

Const cast is used to cast away the immutability of variables. Inside the const member function, non-const class members can be modified with the cast.

4.Reinterpret cast

Reinterpret cast is used to pointer of one type converted into another of any type. It does not check if the pointer type and the data that the pointer points to are the same it onle changes the type of the pointer.

 

Read more, Features of C++ Language

 

Share this post

Leave a Reply

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