Type Conversion
Type Conversion
Content
• Type conversion
• Basic to Basic type
• Basic to Class type
• Class to Basic type
• Class to Class type
Let’s understand
• Have you bought any Pulse or Rice?
• It may possible that you will find some pebbles
inside it?
• That pebbles are also counted in weight.
• When someone is giving speech in English if he/she
may use some words of other language?
• The other language words used are also considered in
English speech.
Type Conversion
• Similarly, when constants and variables of different
types are mixed up in an expression
• Result:
• 2.6 not=> 2
• Explicit Type Conversion:
• Forces an expression to be of specific
type.
• It is also called type casting.
• Syntax of type casting/explicit conversion:
• type(expression);
• E.g.
• • int(4*6.22);
• Result:
• • 24 not=> 24.88
• Explicit Type Conversion:
• There is a possibility of loss of data in case
of type casting.
• E.g.
• • cout<<int(4*6.22);
• Result:
• • 24 not=> 24.88
return(marks+100);
}
};
Class to Class type
• In this type of conversion both the type that is source type
and the destination type are of class type.
• In other words, one class data type is converted into the
another class type.
• For example we have two classes one for “computer” and
another for “mobile”. Suppose if we wish to
assign “price” of computer to mobile then it can be
achieved by the statement below which is the example of
the conversion from one class to another class type.
• mob = comp ;