Object Oriented Programming: Chapter 1: Principles of Object Oriented Programming

Type Conversion

C++ Programming

The type conversion is a process in which variable in one data type can be converted to another data type.

Type Conversion

•  Definition: The type conversion is a process in which variable in one data type can be converted to another data type.

Some data types can be automatically converted by the compiler. This automatic conversion is called as implicit conversion.

For example :

#include<iostream>

using namespace std;

void main()

{

       int x;

       float y=99.99;

       x=y;<‒‒‒‒‒‒‒‒‒‒here value of x becomes 99 and .99 will be truncated.

}

Similarly, we can make use of type cast to explicitly convert one type to another basic type. This type of conversion is called explicit conversion.

In C++ the type casting is done in two ways ‒

* C style type casting

* C++ style type casting

• The syntax for C style type casting is as follows ‒

(data type) expression

For example: (double) 2/3;

• The syntax for the C++ style type casting is as follows ‒

 Data type(expression)

The C++ program which illustrates the type casting is –

/*********************************************************************

Program for demonstrating the type conversion

*********************************************************************/

#include<iostream>

 using namespace std;

 void fun1()

{

       int a;

       double b,ans;

        a=2;b=11.10;

       ans (double)a*b;

       cout<<ans<<endl;

}

void fun2()

{

       int a;

       double b,ans;

       a=2; b=11.10;

       ans double(a)*b;

       cout<<ans<<endl;

}

void main()

{

       cout<<"The C style type casting"<<endl;

       fun1();

       cout<<"The C++ style type casting"<<endl;

       fun2();

}

Output

The C style type casting

22.2

The C++ style type casting

22.2

Program Explanation:

In above program the variable a is of type integer and b is of type double. When the multiplication of the two needs to be done then first we type cast the variable a to double type. The above program shows both C and C++ style type casting.

 

Object Oriented Programming: Chapter 1: Principles of Object Oriented Programming : Tag: Oops, Computer Programming : C++ Programming - Type Conversion


Object Oriented Programming: Chapter 1: Principles of Object Oriented Programming



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation