Download presentation
Presentation is loading. Please wait.
1
Type conversion RITIKA SHARMA
2
Data Conversion int_var1 = int_ver2; obj3 = obj1+obj2; The assignment between types whether they are basic type or user defined types are handled by the compiler with no effort on our part, provided same data type is used on both sides of the assignment operator. RITIKA SHARMA
3
Data Conversion But what will happen if the variables on different sides of the = sign are different ???? Compiler doesn’t handle things automatically. We need to tell it what to do…… RITIKA SHARMA
4
Automatic Type Conversion
Student Book Automatic Type Conversion Automatic type conversion by the C++ compiler from the type that doesn’t fit, to the type it wants 4 types of situation might arise in the data conversion: conversion from basic type to basic type Conversion from basic type to class type Conversion from class type to basic type Conversion from class type to class type This is a powerful feature of the C++ language. It requires care, otherwise it can create ambiguity as to which type to convert to. RITIKA SHARMA
5
basic type to basic type
Intvar = floatvar; For ex: int x; float y=2.50; x=y; RITIKA SHARMA
6
basic type to class type
Constructors perform type conversions from the argument’s type to the constructor’s class type RITIKA SHARMA
7
basic type to class type
#include<iostream> using namespace std; class time { int hrs; int mins; public: time(int t) hrs=t/60; mins=t%60; } void display() cout<<hrs<<" hours"; cout<<mins<<" minutes"; }; int main() { int duration=85; time t=duration; t.display(); return 0; } RITIKA SHARMA
8
class type to basic type
constructor function does not support this operation So c++ allow us to define an overloaded casting operator that could be used to convert a class type data to basic type. It is also known as conversion function The casting operator function should satisfy following conditions It must be a class member It must not specify return type It must not have any arguments RITIKA SHARMA
9
USER DEFINED/CLASS TYPE TO BASIC TYPE
It can be accomplished by overloading the typecasr operator. The cast operator function must be defined in that class whose object needs to be converted. The overloded cast operator function is defined by specifying the keyword operator followed by the datatype to which conversion is desired. The syntax is: operator typename(){…} RITIKA SHARMA
10
Here typename is any of the basic data type.
This overloded typecast operator function doestnot have any return type(not even void) Because the returntype is the typename to whih the object is being converted. Moreover it does not take any parameter RITIKA SHARMA
11
class type to basic type
Syntax:- operator typename() { …………//function statements } operator double() { …….. } operator int() ……….. RITIKA SHARMA
12
class type to basic type
#include<iostream> using namespace std; class time { int hrs; int mins; public: time(int h,int m) {hrs=h; mins=m; } void display() { cout<<hrs<<" hours"; cout<<mins<<" minutes"; operator int() return hrs*60+mins; }; int main() { int duration; time t(3,20); t.display(); duration=t; cout<<duration; return 0; } RITIKA SHARMA
13
class type to class type
Using conversion function source class Using constructor function in destination class RITIKA SHARMA
14
class type to class type
In case of conversion between objects constructor function is applied to destination class. objX=objY Destination class Source class RITIKA SHARMA
15
cout<<"money in rupees"<<rs;
class rupee { int rs; public: rupee(dollar d) rs=d.doll*50; } void show() cout<<"money in rupees"<<rs; }; int main() dollar d1(5); d1.show(); rupee r1=d1; r1.show(); return 0; class dollar { public: int doll; dollar(int x) doll=x; } void show() cout<<"Money dollars="<<doll<<endl; }; RITIKA SHARMA
16
A conversion function is applied to source file
RITIKA SHARMA
17
int main() { dollar d1(5); d1.show(); rupee r1=d1; r1.show();
return 0; } class dollar { public: int doll; dollar(int x) doll=x; } operator rupee() rupee temp; temp.rs=doll*50; return temp; void show() cout<<"Money in dollars="<<doll<<endl; }; #include<iostream> using namespace std; class rupee { public: int rs; void show() cout<<"money in rupees"<<rs; } }; RITIKA SHARMA
18
Summary Conversion required Conversion take place in Source class
Destination class Basic->class NA constructor Class->basic Casting operator Class->class RITIKA SHARMA
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.