Type Conversion

Type  conversion generally means a process of converting one type to another type when two different types are assigned. Or we can say that when two variables of different datatypes are used together or are assigned to each other we need to convert type of one variable to match with the type of another variable. When we talk about

type in programming language it generally means basic datatypes such as:

Int

char

float

long int

double and so on.

Type conversion: The process of converting one pre defined type to another type is called type conversion.

TYPE CONVERSION IS OF TWO TYPE

1.IMPLICIT.

2.EXPLICIT.

IMPLICIT CONVERSION :Implicit conversion is when compiler automatically converts one predefined data type to another data type.

for example:

1.Implicit conversion takes place from float data type to integer data type.


#include<iostream>
using namespace std;

main()

{

                int x;

                int y=55.7;

                x=y; // type conversion takes place

                cout<<y;

                return 0;

}

RESULT

55.


Here compiler automatically converts float to integer .where the disadvantage of this process is during this conversion data gets lost. Value 55.7 becomes 55.

2.Implicit conversion takes place from int data type to float data type.

#include<iostream>
using namespace std;

main()
{
    int a=35;
    float b;
    b=a;//type conversion from float to int
    cout<<"the value of b is "<<b;
    return 0;
}

In this method compiler automatically converts integer data type to float.


RESULT

the value of b is : 35.0


CONCLUSION

1.When we performs implicit type conversion , compiler automatically converts the data type at the right of the assignment to the datatype at the left side of assignment without giving ant error.

2.As seen in first example when we assigned float data type to integer data type,type conversion will take place ,but data is lost in such cases. to overcome this disadvantage we will use Explicit type conversion.

 

EXPLICIT CONVERSION :

 

Explicit type conversion is a type conversion which is done explicitly(i.e) by the user instead of leaving it up to compiler to perform automatically.

This is also called as Type casting. Where we specifically converts one operand of different data type to another operand of different type.

SYNTAX OF TYPE CASTING (EXPLICIT TYPE CONVERSION )

(data_type)expression;

Rules  to follow when type casting is done.

1.All integers data types can be converted to float.

2.All float data types can be converted to Double.

3.All character data types can be converted to integer .

TYPE CONVERSION USING C++ CONCEPTS OR TYPE CONVERSION IN C++.

In C++ Type conversion can be done in 3 types.

1.Conversion of  Basic Type to Class type.

2.Conversion of   Class type to Basic Type.

1.Conversion of   Class type to Class Type.

1.Conversion of  Basic Type(Primitive Type) to Class type(User Defined Type).

Conversion of Basic Data type i.e(int,char float..) to Class type can be done using CONSTRUCTORS. It can be easily carried out using Parameterized constructor.

Process:

For carrying Conversion of  Basic Type(Primitive Type) to Class type(User Defined Type) ,object of class which is  a(user defined data type) should be assigned to variable.To carry on conversion value of Right side operand is assigned to operand present at left side. .for ex


main()

{

Class_name obj(10);//(value assigned to object by passing parameter)//parameterized constructor

int a=20;

obj=a;     value of variable (basic type is converted to class type)

}


Lets discuss a program of parameterized constructor.

#include<iostream>
using namespace std;
class complex
{    int a,b;
    public:
        complex()
        {        }
        complex (int k)
        {        a = k;
                 b = 0;        }
        void setdata(int x, int y)
        {    a = x;
            b = y;         }
        void showdata()
        {
            cout<<"a:"<<endl<<a<<endl<<"b:"<<b;
        }                
};
int main(){
    complex c1;
    int d = 5;//Basic data type
    c1 = d; //parametized constructer called(value of Basic type is assigned to class type
    c1.showdata();    
}

 

Leave a Comment