[go: up one dir, main page]

0% found this document useful (0 votes)
38 views8 pages

Type Casting in C++

Type casting in C++ is the conversion of one data type to another, which can be done implicitly by the compiler or explicitly by the programmer. Implicit type casting occurs automatically when compatible types are involved, while explicit type casting requires the use of a cast operator. C++ provides four types of cast operators: static_cast, dynamic_cast, const_cast, and reinterpret_cast, each serving different purposes in type conversion.

Uploaded by

8c45969zp6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views8 pages

Type Casting in C++

Type casting in C++ is the conversion of one data type to another, which can be done implicitly by the compiler or explicitly by the programmer. Implicit type casting occurs automatically when compatible types are involved, while explicit type casting requires the use of a cast operator. C++ provides four types of cast operators: static_cast, dynamic_cast, const_cast, and reinterpret_cast, each serving different purposes in type conversion.

Uploaded by

8c45969zp6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Type Casting in C++

Type casting refers to the conversion of one data type to another in a program.
Typecasting can be done in two ways: automatically by the compiler and manually by
the programmer or user. Type Casting is also known as Type Conversion.

For example, suppose the given data is an integer type, and we want to convert it into
float type. So, we need to manually cast int data to the float type, and this type of
casting is called the Type Casting in C++.

int num = 5;
float x;
x = float(num);
x = 5.0

2nd example:

float num = 5.25;


int x;
x = int(num);
Output: 5

Type Casting is divided into two types: Implicit conversion or Implicit Type Casting and
Explicit Type Conversion or Explicit Type Casting. 10s

Implicit Type Casting or Implicit Type Conversion

o It is known as the automatic type casting.


o It automatically converted from one data type to another without any external
intervention such as programmer or user. It means the compiler automatically
converts one data type to another.
o All data type is automatically upgraded to the largest type without losing any
information.
o It can only apply in a program if both variables are compatible with each other.

1. char - sort int -> int -> unsigned int -> long int -> float -> double -
> long double, etc.
Note: Implicit Type Casting should be done from low to higher data types. Otherwise, it
affects the fundamental data type, which may lose precision or data, and the compiler
might flash a warning to this effect.

Explicit Type Casting or Explicit Type Conversion

o It is also known as the manual type casting in a program.


o It is manually cast by the programmer or user to change from one data type to
another type in a program. It means a user can easily cast one data to another
according to the requirement in a program.
o It does not require checking the compatibility of the variables.
o In this casting, we can upgrade or downgrade the data type of one variable to
another in a program.
o It uses the cast () operator to change the type of a variable.

Syntax of the explicit type casting

1. (type) expression;

type: It represents the user-defined data that converts the given expression.

expression: It represents the constant value, variable, or an expression whose data


type is converted.

For example, we have a floating pointing number is 4.534, and to convert an integer
value, the statement as:

int num;
num = (int) 4.534; // cast into int data type
cout << num;

When the above statements are executed, the floating-point value will be cast into an
integer data type using the cast () operator. And the float value is assigned to an
integer num that truncates the decimal portion and displays only 4 as the integer value.

Program to demonstrate the use of the explicit type casting in C++

Let's create a simple program to cast one type variable into another type using the
explicit type casting in the C++ programming language.

#include <iostream>
using namespace std;
int main ()
{
// declaration of the variables
int a, b;
float res;
a = 21;
b = 5;
cout << " Implicit Type Casting: " << endl;
cout << " Result: " << a / b << endl; // it loses some information

cout << " \n Explicit Type Casting: " << endl;


// use cast () operator to convert int data to float
res = (float) 21 / 5;
cout << " The value of float variable (res): " << res << endl;

return 0;
}

Output:

Implicit Type Casting:


Result: 4

Explicit Type Casting:


The value of float variable (res): 4.2

In the above program, we take two integer variables, a and b, whose values are 21 and
2. And then, divide a by b (21/2) that returns a 4 int type value.

In the second expression, we declare a float type variable res that stores the results of
a and b without losing any data using the cast operator in the explicit type cast
method.
Not in Syllabus

Some different types of the Type Casting

In type cast, there is a cast operator that forces one data type to be converted into
another data type according to the program's needs. C++ has four different types of
the cast operator:

1. Static_cast
2. dynamic_cast
3. const_cast
4. reinterpret_cast

Static Cast:

The static_cast is a simple compile-time cast that converts or cast one data type to
another. It means it does not check the data type at runtime whether the cast
performed is valid or not. Thus the programmer or user has the responsibility to ensure
that the conversion was safe and valid.

The static_cast is capable enough that can perform all the conversions carried out by
the implicit cast. And it also performs the conversions between pointers of classes
related to each other (upcast - > from derived to base class or downcast - > from base
to derived class).

Syntax of the Static Cast

static_cast < new_data_type> (expression);

Program to demonstrate the use of the Static Cast

Let's create a simple example to use the static cast of the type casting in C++
programming.

#include <iostream>
using namespace std;
int main ()
{
// declare a variable
double l;
l = 2.5 * 3.5 * 4.5;
int tot;
cout << " Before using the static cast:" << endl;
cout << " The value of l = " << l << endl;

// use the static_cast to convert the data type


tot = static_cast < int > (l);
cout << " After using the static cast: " << endl;
cout << " The value of tot = " << tot << endl;

return 0;
}

Output:

Before using the static cast:


The value of l = 39.375
After using the static cast:
The value of tot = 39

Dynamic Cast

The dynamic_cast is a runtime cast operator used to perform conversion of one type
variable to another only on class pointers and references. It means it checks the valid
casting of the variables at the run time, and if the casting fails, it returns a NULL value.
Dynamic casting is based on RTTI (Runtime Type Identification) mechanism.

Program to demonstrate the use of the Dynamic Cast in C++

Let's create a simple program to perform the dynamic_cast in the C++ programming
language.

#include <iostream>
using namespace std;

class parent
{
public: virtual void print()
{

}
};
class derived: public parent
{

};

int main ()
{
// create an object ptr
parent *ptr = new derived;
// use the dynamic cast to convert class data
derived* d = dynamic_cast <derived*> (ptr);

// check whether the dynamic cast is performed or not


if ( d != NULL)
{
cout << " Dynamic casting is done successfully";
}
else
{
cout << " Dynamic casting is not done successfully";
}
}

Output:

Dynamic casting is done successfully.

Reinterpret Cast Type

The reinterpret_cast type casting is used to cast a pointer to any other type of pointer
whether the given pointer belongs to each other or not. It means it does not check
whether the pointer or the data pointed to by the pointer is the same or not. And it
also cast a pointer to an integer type or vice versa.

Syntax of the reinterpret_cast type

1. reinterpret_cast <type> expression;

Program to use the Reinterpret Cast in C++


Let's write a program to demonstrate the conversion of a pointer using the reinterpret
in C++ language.

#include <iostream>
using namespace std;

int main ()
{
// declaration of the pointer variables
int *pt = new int (65);

// use reinterpre_cast operator to type cast the pointer variables


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

cout << " The value of pt: " << pt << endl;
cout << " The value of ch: " << ch << endl;

// get value of the defined variable using pointer


cout << " The value of *ptr: " << *pt << endl;
cout << " The value of *ch: " << *ch << endl;
return 0;

Output:

The value of pt: 0x5cfed0


The value of ch: A
The value of *ptr: 65
The value of *ch: A

Const Cast

The const_cast is used to change or manipulate the const behavior of the source
pointer. It means we can perform the const in two ways: setting a const pointer to a
non-const pointer or deleting or removing the const from a const pointer.

Syntax of the Const Cast type

1. const_cast <type> exp;

Program to use the Const Cast in C++


Let's write a program to cast a source pointer to a non-cast pointer using the
const_cast in C++.

#include <iostream>
using namespace std;

// define a function
int disp(int *pt)
{
return (*pt * 10);
}

int main ()
{
// declare a const variable
const int num = 50;
const int *pt = # // get the address of num

// use const_cast to chnage the constness of the source pointer


int *ptr = const_cast <int *> (pt);
cout << " The value of ptr cast: " << disp(ptr);
return 0;

Output:

The value of ptr cast: 500

You might also like