Type Conversion in C++

Type Conversion in C++

There is a concept of type conversion in C++ to convert any class with another class whether it is primitive type or class type.
 
C++ allows you to covert one data type to another data type and hence it is called type conversion.

int, char, float, double are primitive types whereas class type is any class that you defined.


Why type conversion is required?

There is an automatic type conversion for converting one primitive data type to another primitive data type. for example: if you want to convert int type to float, you can easily convert it.

int x = 5;
float y;

y = x;   //automatic type conversion

float a = 5.63;
int z;

z = a;   //automatic type conversion

However, if you want to convert one class that you defined to another class, there is no automatic type conversion. You need to do type conversion yourself. That is the reason behind type conversion.


Types of type conversion in C++

1. Implicit Conversion.
2. Explicit Conversion (or Type Casting).


Implicit conversion

1. The type conversion which is done automatically by C++ compiler is called implicit type conversion or automatic conversion. 

2. You should know that there is some data loss during automatic conversion or implicit conversion.

3. If you convert lower data type to higher data type, there is no data loss (e.g., int to long conversion, no data loss).

4. If you convert higher data type to lower data type, there is some data loss (long to int conversion, data loss).

For Example:- Conversion from int to double
#include<iostream>
using namespace std;
int main()
{
 int intNum = 9;
 
 double doubleNum;
 
 // implicit conversion
 // assigning int value to a double variable
 
 doubleNum = intNum;
 
 cout << "integer Number = " << intNum << endl;
 cout << "double Number = " << doubleNum << endl;
 
 return 0;
}

Explicit conversion

When the programmer want to change the data type he created with another data type, he need to do it manually. This is called explicit conversion or type casting.

There are four possible type conversion in C++. They are:

1. Primitive type to Primitive type.
2. Primitive type to Class type.
3. Class type to Primitive type.
4. One Class type to Another Class type.


Primitive type to Primitive type Conversion

There is auto conversion of primitive type to primitive type.

For Example:- Conversion from double to int
#include<iostream>
using namespace std;

int main()
{
 double doubleNum = 9.91;
 
 int intNum;
 
 // implicit conversion
 // assigning double value to an int variable
 
 intNum = doubleNum;
 
 cout << "integer Number = " << intNum << endl;
 cout << "double Number = " << doubleNum << endl;
 
 return 0;
}


Primitive type to Class type Conversion

Primitive type can be converted to class type through constructor.

For Example:- primitive type to class type
#include<iostream>
using namespace std;

class Complex {
 int real, imaginary;
 
 public:
  Complex(int num) {
   real = num;
   imaginary = 0;
   }
   
   void setData(int num1, int num2) {
    real = num1;
    imaginary = num2;
   }
   
   void showData() {
    cout << "Real no: " << real << " Imaginary no: " << imaginary << endl;
   }
  };
  
  int main() {
   Complex C1;
   int num1 = 11;
   C1 = num1;    //C1.complex(num1);
   C1.showData();
  }

In the above example, num1 is an integer which is primitive data type and it is converted to C1 which is the class that had been created above.


Class type to Primitive type Conversion

1. Class type can be converted into primitive type with the help of casting operator.

2. Casting operator is a special type of operator which is helpful in converting one data type to another one.

3. Syntax of Casting operator is given below:

operator type()
{

//...

//...

//...

return (type_data);
}


For Example:- class type to primitive type
#include<iostream>
using namespace std;

class Complex  {
  int real, imaginary;
  
  public:
    void setData(int num1, int num2)  {
      real = num1;
      imaginary = num2;
    }
    
    void showData()  {
      cout << "Real no: " << real << " Imaginary no: " << imaginary << endl;
    }
    
    //casting operator for returning real or integer part of complex number.
    operator int()  {
      return real;
    }
  };
  
  int main()  {
    Complex C1;
    C1.setData(11, 21);
    
    int num1;
    num1 = C1;    //num1 = C1.operator int();
    
    cout << num1;
  }


One Class type to Another Class type Conversion

One class type can be converted to another class type in two ways:
1. using constructor.
2. using casting operator.


For Example:- one class type to other class type
#include<iostream>
using namespace std;

class Product  {
  private:
  int num1, num2;
  
  public:
    void setData(int x, int y)  {
      num1 = x;
      num2 = y;
    }
    
    int getNum1()  {
      return num1;
    }
    
    int getNum2()  {
      return num2;
    }
  };
  
  class Item  {
    int num3, num4;
    
    public:
      void showData()  {
        cout << "A = " << num3 << " B = " << num4;
      }
      
      Item()     //default constructor  {
      }
      
      Item(Product P)  {
        num3 = P.getNum1();
        num4 = P.getNum2();
      }
    };
    
    int main()  {
      Item I1;
      Product P1;
      
      P1.setData(11, 101);
      I1 = P1;
      
      I1.showData();
    }



<< Previous                                                                                      Next >>
Previous
Next Post »