0% found this document useful (0 votes)
35 views

Type Conversions IN C++: by Prof - Manikandan QMC College, Medavakkam

The document discusses type conversions in C++. It notes that the compiler does not support automatic type conversions for user-defined data types. The static_cast operator can be used to explicitly convert between types. An example program demonstrates converting an integer to a float using static_cast, which properly handles the division and returns a float value of 10.3333 instead of an integer value of 10 without the cast.

Uploaded by

Prof.Manikandan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Type Conversions IN C++: by Prof - Manikandan QMC College, Medavakkam

The document discusses type conversions in C++. It notes that the compiler does not support automatic type conversions for user-defined data types. The static_cast operator can be used to explicitly convert between types. An example program demonstrates converting an integer to a float using static_cast, which properly handles the division and returns a float value of 10.3333 instead of an integer value of 10 without the cast.

Uploaded by

Prof.Manikandan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

TYPE CONVERSIONS

IN C++

By

Prof.Manikandan

QMC College, Medavakkam.

TYPE CONVERSIONS
The compiler
does not support automatic type
conversions for the user defined data types.
We can use casting operators functions to do this.

Data type varname = Static_cast <data type>


Syn:
int main()
{
int a; int b;
Float y=Static_cast <float> (a)/b;
..
}

Static_cast operator is used to convert a given


expression to the specified type.
Example:

int a=31;
int b=3;
float y=staitc_cast <float>(a)/b;

Out put:
10.3333

EXAMPLE PROGRAM
#include<iostream.h>
int main()
{
int a=31;
int b=3;
float x=a/b;
float y=static_cast<float> (a)/b;
cout<<"output without static_cast ="<<x;
cout<<"output with static_cast ="<<y;
return 0;
}
Output:
output without static_cast =10
output with static_cast =10.3333

1.
2.
3.

Using the static_cast to an integer as float


returns a float value.
The casting operator function should satisfy the
following conditions.
It must be a class member.
It must not specify a return value.
It must not have any arguments.

You might also like