Type conversion or type casting in C++

Type conversion means to convert a data from one type to another type. As C++ allows various data types, sometimes we need to convert a data from one type to another. Then this is known as type conversion. In this article we will discuss about different types of type conversion in C++.

There are two main types of type conversion in C++. These are as follows:

  • Implicit type conversion
  • Explicit type conversion or type casting

C++ implicit type conversion

Another name of implicit type conversion is automatic type conversion. Because they are converted automatically by the compiler and we need not do anything for this kind of type conversion. See the following C++ program to understand about implicit type conversion.

Here we have assigned a double data to a integer variable and the double data is converted automatically to integer data.

// implicit type conversion

#include <iostream>
using namespace std;

int main(){
    float a = 23.65;
    int b;

    b = a;    // b in integer where a is float

    cout << "Value of a is = " << a << endl;
    cout << "Value of b is = " << b << endl;

    return 0;
}

Output of type conversion program

type-conversion-in-c-type-casting-c-type-conversion

You many have noticed that some data have lost during the type conversion. This is happened when we convert a larger type of data to comparatively smaller type of data. This type of conversion is called narrowing conversion.

As here we have converted float data to integer type of data, we have lost the fraction part from the number. But when we convert a data to lower type to upper type then we will never loss data. See the example program bellow where we have used type conversion from smaller type to larger type.

#include <iostream>
using namespace std;

int main(){
    int x = 55;
    double y;

    y = x;    // convert from integer to double

    cout << "x = " << x << endl;
    cout << "y = " << y << endl;

    return 0;
}

Output

x = 55
y = 55

C++ explicit type conversion (Type casting)

When the compiler can not convert one type of data to another then we have to convert it manually. This is called explicit type conversion. Explicit type conversion is also known as type casting.

We can convert one type of data to another manually using following three types of type casting.

  • Using cast notation
  • Using function notation
  • Using conversion operators

Type casting using cast notation

Cast notation is normally used in C language and this is also called the C style type casting. However it is valid to use cast notation for type casting in C++. The syntax of cast notation when converting from int to double is given bellow.

// convert from int to double using cast notation

double_variable_name = (double)int_variable_name;

See the program bellow where we have implement this type conversion using cast notation.

#include <iostream>
using namespace std;

int main(){

    int p = 10;

    double q;

    q = (double)p;    // type casting using cast notation

    cout << "Value of double variable is = " << q << endl;
    cout << "Value of integer variable is = " << p << endl;

    return 0;
}

Output of type casting program

Value of double variable is = 10
Value of integer variable is = 10

Type casting using function notation

We can also use function notation for type conversion in C++. See the syntax of function notation when we convert from integer to double type of data.

// convert from integer to double using function notation

double_var_name = double(int_var_name);

Following program will help you to learn type casting using function notation in C++.

#include <iostream>
using namespace std;

int main(){

    int x = 15;

    double y;

    y = double(x);    // type conversion using function notation

    cout << "Value of x is = " << x << endl;
    cout << "Value of y is = " << y << endl;

    return 0;
}

Output of type casting program

Value of x is = 15
Value of y is = 15

Type conversion using conversion operators

We can also use some operators for type conversion. These operators force a data type to convert into another type of data. These are actually unary operators and in C++ we can use four operators for type conversion. These are as follows;

  • static_cast operator
  • dynamic_cast operator
  • const_cast operator
  • reinterpret_cast operator

Example program of static cast operator

#include <iostream>
using namespace std;

int main(){

    double d = 5.193353;

    int x = static_cast<int>(d);

    cout << "x = " << x << endl;

    return 0;
}

Output of program:

x = 5

Example program of dynamic cast operator

// type conversion using dynamic cast operator

#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 << "value is null" << endl;
	}else{
		cout << "value is not null" << endl;
    }
    
	return 0;
}

Program output:

value is not null

We will learn about C++ object and class later. So, don’t worry if you do not understand this program.

Example program of const cast operator

// type conversion using const cast operator

#include <iostream>
using namespace std;

int my_funct(int* pt){
    return (*pt + 25);
}

int main(void){
    const int num = 20;
    const int *first_ptr = &num;

    int *second_ptr = const_cast <int *>(first_ptr);

    cout << my_funct(second_ptr) << endl;
    return 0;
}

Output of this type conversion program:

45

Example program of reinterpret cast operator

// type conversion using reinterpret cast operator

#include <iostream>
using namespace std;

int main(){
    int* pt = new int(67);

    char* my_ch = reinterpret_cast<char*>(pt);

    cout << *my_ch << endl;

    return 0;
}

Output of this program:

C

Previous pageBooleans in C++

Next pageIf else in C++