More

    Type Conversion in C++.

    Required Changes Conversion
    Source Program Destination Class
    Basic To Class Not Application Constructor
    Class To Basic Casting Operator Not Application
    Class To Class Casting Operator Constructor

    This process can also be used to convert type to derived data type. Based on this, there are the following situations of type conversions –

    1. Basic to basic
    2. Basic to class
    3. Class to basic
    4. Class to class coverage

    When different types of Constructor or Variable are used in an expression. Then the basic operators in C and C++ are automatically converted, similarly in the assignment operator, when one data type variable is initialized with another data type variable, then their type is automatically converted there too.

    Now we will understand all these one by one with example.

    Basic to Basic Conversion

    In this, the basic data type (int, char, float) is converted to any other basic data type. This conversion happens automatically. For example-

    • Conversion from int to float
    • Conversion from Char to Int

    Example

    #include <iostream.h>
    #include <conio.h>
    
    void main( )
    {
    clrscr();
    char a ;
    a = ‘a’ ;
    int r;
    r = a;
    cout << “r=” << r;
    }
    

    Basic to Class Conversion

    When a data type is converted into a class type, it is called basic to class conversions. Constructor is used for basic to class conversions. like –

    Test (int 5)
    {
    a = 5;
    }
    

    This constructor is creating a test object from the basic data type int 5. For this we understand with the following example –

    #include <iostream.h>
    #include <conio.h>
    class test
    {
    int a;
    Public:
    test()
    { }       // Empty Constrictor
    test (int s)  // Parameterized Constrictor
    {
    a = 5;
    }
    void show()
    {
    cout<<"n value is ="<<a;
    }
    };
    
    Void main()
    {
    clrscr();
    int basic;               // basic data-type
    basic = 30;
    test object ;        // Class data type
    object = basic;           // basic to class conversion
    object.show();
    }
    

    Output

    Value is = 80

    Class to basic conversion

    In this, a class object is converted into a basic data-type. Overload Casting Operator is used for it. Overload Casting Operator is a Member Function, whose Syntax is as follows –

    operator type name ( )
    ,
    Function body
    ,

    #include <iostream.h>
    #include <conio.h>
    Class test
    {
    int m;
    Public:
    test (int x)
    {
    m = x;
    }
    
    void show()
    {
    cout << “n value is =” << m;
    }
    
    Operator int()  //casting operator
    {
    int y; y = m; return y; 
    }
    };
    
    void main ( )
    {
    clrscr();
    test t (150);
    t.show();
    int a;
    a = t
    cout << “a =” << a;
    }
    

    Class to Class Conversion

    Sometimes we need to change from one class type to another. Eg :- Object x = Object y

    Object xx is object of class and object y is object of class y. class y is converted to class x. Since this change has been done from class y to class x, y is called source class ax as destination class.

    These types of transformations are done with the help of Constructor or Conversion function. For example, in class to basic conversion, it is done by the costing operator function, in which the operator type name ( ) changes the object of the class to the type name of which it is a member.

    In case of change between objects, the type name is assumed to be the destination class. Therefore, when there is a need to change the class, then the Costing Operator function is used.

    That is, in class to class conversions, the costing operator is used in the source class and the constructor is used in the destination class.

    #include <iostream.h>
    #include <conio.h>
    class test           // source class
    {
    int a;
    Public:
    test 1()
    {
    a = 200;
    }
    int geta()
    {
    return a ;
    }
    void show()
    {
    cout << “n test 1 class value : =” << a; }
    };
    class test2         // destination class
    {
    Private :
    int b:
    Public :
    test 2()
    {              // Empty Constrictor
    }
    void display()
    {
    cout << “/n test 2 value : =” <<b;
    }
    
    test2(test 1)     // conversion constructor
    {
    b = r.geta();
    }
    };
    
    void main()
    {
    clrscr();
    test t1;
    t1.show();
    test2 t2 ;
    t2 = t1
    t2.display();
    }

    Output

    Test 1 value = 200
    Test 2 value = 200

    Recent Articles

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Stay on op - Ge the daily news in your inbox