12438_13 Type Conversion

download 12438_13 Type Conversion

of 13

Transcript of 12438_13 Type Conversion

  • 7/29/2019 12438_13 Type Conversion

    1/13

    Type Conversion

  • 7/29/2019 12438_13 Type Conversion

    2/13

    Introduction

    It is the process of converting incompatible

    values from one type to another type.

    Forbuilt-in typesit happens automatically

    (implicitly), but foruser - defined types, wehave to define them.

    Types :

    Conversion from basic type to class type Conversion from class type to basic type

    Conversion from one class type to another class

    type

  • 7/29/2019 12438_13 Type Conversion

    3/13

    Example:

    int m;

    float x = 3.145;m = x;

    convert x to an integer before its value isassigned to m.

  • 7/29/2019 12438_13 Type Conversion

    4/13

    Conversion from Basic Type to

    Class Type

    It takes place automatically using one

    argument constructor of the destination class.

    Example:

    string :: string (char *a)

    { length = strlen (a);

    P = new char [length + 1];

    strcpy (P, a); }

    This constructor builds a string type object from achar* type variable a. The variables length and Pare the data members of the class string.

  • 7/29/2019 12438_13 Type Conversion

    5/13

    string s1, s2;

    char* name1 = IBM PC;

    char* name2 = Apple Computers;s1 = string (name1);

    s2 = name2;

    The statement s1 = string (name1); firstconverts name1 from char* type to string type

    & then assigns the string type values to the

    object s1. The statement s2 = name2; alsodoes the same job by invoking the constructor

    implicitly.

  • 7/29/2019 12438_13 Type Conversion

    6/13

    Note: -

    The constructor used for type conversion

    from basic type to class type takes a single

    argument.

    The left hand operand of assignment

    operator = is always a class object.

    Therefore, this type of conversion can be

    performed by overloading assignmentoperator.

  • 7/29/2019 12438_13 Type Conversion

    7/13

    Conversion from Class Type to

    Basic Type

    It can be performed by defining conversion

    function in the source class.

    Casting Operator or conversion function.

    Syntax:

    operator typename ()

    { :::::::::

    ::::::::: //function statement

    }

  • 7/29/2019 12438_13 Type Conversion

    8/13

    Example:

    vector :: operator double ()

    { double sum = 0;

    for (int i = 0; i < size; i++)

    sum = sum + v [i] * v [i];

    return sqrt (sum);

    }

    The operator double () converts a class object to typedouble, the operator int () converts a class type objectto type int, so on

  • 7/29/2019 12438_13 Type Conversion

    9/13

    Conversion from Class Type to

    Another Class Type

    It can be performed by defining conversion

    function in the source class or through one

    argument constructor, that takes an object of

    the source class, in the destination class.

    It depends upon where you want to

    conversion facility to be located.

  • 7/29/2019 12438_13 Type Conversion

    10/13

    If you want to place the conversion facility in

    the source class, then use conversion function

    with name of the destination class as

    typename.

    class x; //forward declaration

    class y //source class{ ::::::::

    operator x() //conversion function

    { ::::::: }

    ::::::::::}

    class x //destination class

    { :::::::::: }

  • 7/29/2019 12438_13 Type Conversion

    11/13

    If you want to place the conversion facility in

    the destination class, then use constructor that

    takes object of source class as its only

    argument.

    class y; //forward declaration

    class x //destination class{ ::::::::

    x ( y obj ) //constructor for conversion

    { ::::::: }

    ::::::::::

    }

    class y //source class

    { :::::::::: }

  • 7/29/2019 12438_13 Type Conversion

    12/13

    Summary of Type Conversion

    Types Source Class Destination Class

    Basic Type to Class Type Not applicable Constructor

    Class Type to Basic Type Conversion Function Not applicable

    Class Type to Class Type Conversion Function Constructor

    Casting Operator function : - Conditions

    It must be a class member.

    It must not specify a return value.

    It must not have any arguments.

  • 7/29/2019 12438_13 Type Conversion

    13/13

    Thank You!!!