0% found this document useful (0 votes)
7 views

CH5_2

The document discusses data conversion techniques, focusing on converting variables between different types, including implicit and explicit conversions. It explains the use of constructors and conversion functions for type conversions, detailing scenarios such as basic to class type and class to basic type conversions. Additionally, it covers the use of casting operators and conversion constructors for converting between different class types.

Uploaded by

herohero9851
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CH5_2

The document discusses data conversion techniques, focusing on converting variables between different types, including implicit and explicit conversions. It explains the use of constructors and conversion functions for type conversions, detailing scenarios such as basic to class type and class to basic type conversions. Additionally, it covers the use of casting operators and conversion constructors for converting between different class types.

Uploaded by

herohero9851
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Conversion

Introduction 2

 Data conversion deals with techniques for converting


variables from one type to another.
 The type conversion is either implicit or explicit as far as
data types involved are built in types.
 We can also use the assignment operator in case of
objects to copy values of all data members of right hand
object to the object on left hand. The objects in this case
are of same data type.
 But of objects are of different data types we must apply
conversion rules for assignment.
Introduction 3

 Conversion of one type to another is achieved by the use of


constructors and type conversion functions.
 Three types of situations might arise for data conversion
between different types :
 (i) Conversion from basic type to class type.
 (ii) Conversion from class type to basic type.
 (iii) Conversion from one class type to another class type.
Conversion from basic to class 4

 To convert basic data type to class type, we use a


constructor which takes single argument whose type is to be
converted
 This one-argument constructor is called “conversion
constructor”
Conversion from basic to class 5
class time
main()
{ int h,m,s;
{
public:
time t1;
time(){}
int duration=7285;
time (int t) //conversion constructor
{
t1=duration; //implicit
h=t/3600;
t1.display();
m=t%3600/60;
s=t%3600%60;
time t2(7285); //explicit
}
t2.display();
void display()
}
{ cout<<h<<":"<<m<<":"<<s<<endl; }
};
Conversion from class to basic 6

 The constructor functions do not support conversion from a class to


basic type.
 C++ allows us to define a overloaded casting operator that convert
a class type data to basic type.
 The general form of an overloaded casting operator function, also
referred to as a “conversion function”, is:
operator typename ( )
{
//Program statments
}

 This function converts a class type data to typename. For example,


the operator double( ) converts a class object to type double
Conversion from class to basic 7

 The casting operator should satisfy the following conditions.


 It must be a class member.
 It must not specify a return type.
 It must not have any arguments. Since it is a member function, it is
invoked by the object and therefore, the values used for, Conversion
inside the function belongs to the object that invoked the function.
As a result function does not need an argument.
 The casted operator is used as,
 int x = int(obj); // explicit
 int x = static_cast<int>(obj); // explicit
 int x = obj; ); // implicit
Conversion from class to basic 8
class time time::operator int()

{ int h,m,s; {
int duration;
public:
duration=h*3600 + m*60 +s;
time(){}
return duration;
time (int h,int m,int s)
}
{ this->h=h;
main()
this->m=m; {
this->s=s; time t1(2, 1, 25);
} int total;
void display() t1.display();

{ cout<<h<<":"<<m<<":"<<s<<endl; } total=t1;
cout<<"Total sec:"<<total;
operator int(); //conversion function
}
};
Conversion from one class to other class 9

 Obj1 = Obj2 ; //Obj1 and Obj2 are objects of different


classes.

 Obj1 is an object of class one and Obj2 is an object of class


two. The class two type data is converted to class one type
data and the converted value is assigned to the Obj1.
Since the conversion takes place from class two to class
one, two is known as the source and one is known as the
destination class.
Conversion from one class to other class 10

 Such conversion between objects of different classes can


be carried out by either a constructor or a conversion
function.
 Which form to use, depends upon where we want the
type-conversion function to be located, whether in the
source class or in the destination class.
Conversion from one class to other 11

class – Casting operator in source class


 Operator typename( ) converts the class object of which it is
a member to typename. The type name may be a built-in
type or a user defined one(another class type) .
 In the case of conversions between objects, typename refers
to the destination class. Therefore, when a class needs to be
converted, a casting operator function can be used.
 The conversion takes place in the source class and the result is
given to the destination class object.
Conversion from one class to other class
12
– Casting operator in source class
class dest void display()
{ int duration; { cout<<h<<":"<<m<<":"<<s<<endl; }
public: operator dest() //conversion function
dest(){} {
dest(int d):duration(d){ } return dest(h*3600 + m*60 +s);
void display() }
{ cout<<"Total duration:"<<duration<<endl; } };
};
class src main()
{ int h,m,s; { src s1(2, 1, 25);
public: dest d1;
src(){} s1.display();
src (int h,int m,int s) d1=s1;
{ this->h=h; this->m=m; this->s=s; } d1.display();
}
Conversion from one class to other 13

class – Conversion constructor in destination class


 Let us consider a single-argument constructor function which serves as an
instruction for converting the argument's type to the class type of which it
is a member.
 The argument belongs to the source class and is passed to the destination
class for conversion. Therefore the conversion constructor must be placed
in the destination class.
 When a conversion using a constructor is performed in the destination
class, we must be able to access the data members of the object sent (by
the source class) as an argument.
 Since data members of the source class are private, we must use special
access functions in the source class to facilitate its data flow to the
destination class.
Conversion from one class to other class
– Conversion constructor in destination class 14
class dest
class src
{ int duration;
{ int h,m,s;
public:
public:
dest(){}
src(){}
dest(src s)
src (int h,int m,int s)
{ duration = 3600*s.get_h() + 60*s.get_m() + s.get_s(); }
{ this->h=h; this->m=m; this->s=s; }
void display()
void display()
{ cout<<"Total duration:"<<duration<<endl; }
{ cout<<h<<":"<<m<<":"<<s<<endl; }
};
int get_h()
main()
{ return h; }
{ src s1(2, 1, 25);
int get_m()
dest d1;
{ return m; }
d1=s1;
int get_s()
s1.display(); d1.display();
{ return s; }
}
};
Explicit Constructors 15

 Constructor with one argument is used for implicit conversion in


which the argument type is converted to object of class in which
it is defined.
 If we do not want automatic conversion, we define explicit
constructor so that the type conversion is done explicitly.

class xyz main()


{
{ int a; xyz x1(20); // OK
public: xyz x2 = xyz(30); // OK
// xyz x3 = 10; is not allowed
explicit xyz(int i) }
{ a=i ;}
};

You might also like