AJ/Handout 9 -1- Object-Oriented Programming
Lesson 9
Objectives
Data Conversion
Conversion between Basic data types
Conversion between Objects and Basic data types
Conversion from Basic to User-Defined & User-Defined to Basic data type
Conversion between C-String and string Objects
We have seen in many situations that the ‘=’ assignment operator for assigning a basic data type to
other and also an object being assigned to other object. Now we will see the situations where
different data types (user define, basic, object etc) can be converted to each other as well. Starting
the discussion from the topic below we shall proceed to next cases.
Conversion between Basic data types
Implicit Cast: such kind of cast is take place automatically. This is carried out by calling some
special routines by compiler to convert the given basic data type. Simple rule for the implicit cast
is that if the size of the variable on right side of ‘=’ is less than or equal to that of left hand side.
For example a character can be stored in integer, an integer to double and etc. These conversions
are called implicit because they are not apparent in the program.
Explicit Cast: sometimes we want to force the compiler to convert one type to another. To
do this we use the cast operator. For instance, to convert float to integer, we can say
intvar = static_cast <int> (floatvar); this will provide and explicit conversion. It is obvious that
static_cast <int> () is intended to convert from float to int. However, such conversions use the
same built-in routines as implicit conversions.
Conversion between Objects and Basic types
Now our requirement is to make a conversion between user data type (Objects) and Basic
types. For this we can’t rely on built-in routines since compiler does not know the required
operation unless we tell it. In other words we have to write these routines ourselves. This program
will show this conversion.
#include<iostream>
using namespace std;
///////////////////////////
class Distance
{
private:
const float MTF; //meter to feet
int feet;
float inches;
public:
Distance() : feet(0), inches (0.0), MTF (3.280833F)
AJ/Handout 9 -2- Object-Oriented Programming
{}
Distance (float meters) : MTF(3.280833F)
{
float fltfeet = MTF * meters;
feet = int (fltfeet);
inches = 12 * (fltfeet - feet);
}
Distance (int ft, float in) : feet(ft), inches(in), MTF(3.280833F)
{}
void getdist()
{
cout<<”Enter Feet:”; cin>>feet;
cout<<”Enter Inches:”; cin>>inches;
}
void showdist() const
{cout<<feet<<”\”<<inches;}
operator float() const //conversion operator
{
float fracfeet = inches/12; //converts Distance to meter
fracfeet +=static_cast<float>(feet); //add the feet
return fracfeet/MTF; //convert to meter
}
};
////////////////////////////////////
void main()
{
float mtrs;
Distance dist1=2.35F; //uses one arg ctor for converting meter to Distance
Cout<<”Dist 1: ”; dist1.showdist();
Mtrs = static_cast<float>(dist1); //uses conversion operator for Distance to meters
Cout<<”Dist 1:”<<mtrs;
Distance dist2(5,10.25);
Mtrs= dist2; //uses conversion operator
Cout<<”Dist2:”<<mtrs;
// Dist2=mtrs; //causes an error
}
Here basically two things performed
1) From Basic to User-Defined
Here we have the lines in above program like
Distance (float meters) : MTF(3.280833F)
{
float fltfeet = MTF * meters;
feet = int (fltfeet);
inches = 12 * (fltfeet - feet);
}
AJ/Handout 9 -3- Object-Oriented Programming
Here we can see that ultimately meter is converted to feet and inches (Distance type Object) and
assigned to the Object made by this constructor. That is, Distance dist1=2.35;
2) From User-Define to Basic
Now the same program is being capable of doing this just going the other way round. There is a
trick to do this carried out by an operator called a conversion operator. In the listing,
operator float() const //conversion operator
{
float fracfeet = inches/12; //converts Distance to meter
fracfeet +=static_cast<float>(feet); //add the feet
return fracfeet/MTF; //convert to meter
}
this operator takes the value of the Distance object of which it is member, converts it to a float
value representing meters, and returns this value. This operator can be called with an explicit cast.
Mtrs = static_cast<float>(dist1); or simply
Mtrs = dist2;
Both forms convert the Distance object to its equivalent float value in meters.
Conversion between C-String and string Objects
Here is another example of conversion that uses a one-argument constructor and conversion
operator.
//strconv.cpp
//conversion between ordinary string and C++ string class
#include <iostream>
using namespace std;
#include<string.h>
////////////////////////////////
class String
{
private:
enum {SZ = 80};
char str[SZ];
public:
String()
{str[0] = ‘\0’;}
String(char s[] )
{strcpy(str,s);}
void display () const
{cout << str;}
operator char* ()
{return str;}
};
/////////////////////////////////////
void main()
{
String s1;
char xstr[] = “William”;
s1 = xstr; //uses one argument constructor to convert C-string to String
s1.display();
String s2 = “Shakespeare”; //uses 1 arg ctor to initialize String same approach
AJ/Handout 9 -4- Object-Oriented Programming
cout<<static_cast<char*>(s2); //use conversion operator to convert String to C-string
//before sending to << operator
}
Here ‘*’ in this expression means pointer to. It means pointer to char, which is very similar to
array of type char. Thus “char*” is similar to “char []”
Note that we can’t use an implicit assignment statement to convert a String to a C-string
xstr = s2; while in subsequent lectures we shall see its possibility.