Object Oriented Programing Operator Overloading Data Conversion 1 Object Oriented Programming |
[email protected]
• Conversion from • User define data type to basic data types • Basic data types to user define data type • User define to user define data type
Today Lecture Object Oriented Programming |
[email protected]
2
• Data type conversion is know as casting • There are two ways in which the casting can be implemented • Implicit Type Casting • Explicit Type Casting
Data Type Conversion Object Oriented Programming |
[email protected]
3
• Example of Implicit casting • int a ; • float f = 20.5; • a = f;
• Example of explicit casting • Casting operator / function: static_cast
(); • Usage: a = static_cast(f);
Data Type Conversion Object Oriented Programming | [email protected]
4
• Cast operator; static_cast() • Forces conversions among built-in types • float fVal = static_cast(intVal);
• We can Specify conversions between user defined and built-in types • Conversion operator must be a non-static member function • Cannot be a friend function • Do not specify return type • By default the return type is the type to which the object is being converted
Data Type Conversion Object Oriented Programming | [email protected]
5
• For user-defined class A A::operator char *() const;
• Declares an overloaded cast operator function for creating a char * out of an A’s object A::operator int() const;
• Declares an overloaded cast operator function for converting an object of A into an integer A::operator otherClass() const;
• Declares an overloaded cast operator function for converting an object of A into an object of otherClass
Data Type Conversion Object Oriented Programming | [email protected]
6
• Compiler and casting • If an object s of user-defined class String appears in a program where an ordinary char * is expected, such as cout << s;
The compiler calls the overloaded cast operator function operator char * to convert the object into a char * and uses the resulting char * in the expression
Data Type Conversion Object Oriented Programming | [email protected]
7
• If there is need to covert the user defined data type into basic data types compiler will unable to convert this type of casting implicitly nor it can be performed explicitly. • For Example we have the user defined data type Distance. • If we want to assign the object of Distance to a variable of type int • int a = d1; // generate an error “cannot convert Distance to int”
Data Type Conversion Object Oriented Programming | [email protected]
8
• C++ provides the facility to deal with such type of cases using data type overloading. • The mechanism of data type overloading is same as operator overloading in C++. • To do this we have to overload the data type int in the class Distance.
Data Type Conversion Object Oriented Programming | [email protected]
9
class Distance { private: int Dist; public: Distance (): Dist(1) { } Distance (int dist): Dist(dist) { } int getDist() { return Dist; } void setDist(int dist) { Dist = dist; } operator int()const { return Dist; } };
Data Type Conversion Object Oriented Programming | [email protected]
10
int main() { Distance d; int a=d; cout<<"\n The Distance ="<
Data Type Conversion Object Oriented Programming | [email protected]
11
• The previous Distance class is simple one. • Lets make some extension to this class. • In this extension you will also learn about • const and final keyword’s • Both data member & member methods can be declared as const • Unlike static, as by name suggest; const having the same meaning and function; as non-variable; one that can’t change its state
Extension to distance class 12 Object Oriented Programming | [email protected]
class Distance { private: const float MTF; //meters to feet int feet; float inches;
Extension to distance class 13 Object Oriented Programming | [email protected]
public:
//constructor (no args) Distance() : feet(0), inches(0.0), MTF(3.280833F) {} //constructor (one arg) Distance(float meters) : MTF(3.280833F) { //convert meters to Distance float fltfeet = MTF * meters; //convert to float feet feet = int(fltfeet); //feet is integer part inches = 12*(fltfeet-feet); //inches is what’s left } //constructor (two args) Distance(int ft, float in) : feet(ft), inches(in), MTF(3.280833F) {}
Extension to distance class Object Oriented Programming | [email protected]
14
void getdist() //get length from user { cout << "\nEnter feet: "; cin rel="nofollow">> feet; cout << "Enter inches: "; cin >> inches; } void showdist() const //display distance { cout << endl<(feet); //add the feet return fracfeet/MTF; //convert to meters } };
Extension to distance class Object Oriented Programming | [email protected]
15
int main() { float mtrs; //convert meters to Distance Distance dist1 = 2.35F; //uses 1-arg constructor to dist1.showdist(); mtrs = dist1; //uses conversion operator //for Distance to meters cout << endl<<"dist1 = " << mtrs << " meters\n"; Distance dist2(5, 10.25); //uses 2-arg constructor mtrs = dist2; //also uses conversion op cout << endl<<"dist2 = " << mtrs << " meters\n"; // dist2 = mtrs; //error, = won’t convert return 0; }
Extension to distance class Object Oriented Programming | [email protected]
16
• To go from a basic type—float in this case—to a user-defined type such as Distance, we use a constructor with one argument. These are sometimes called conversion constructors Distance(float meters) : MTF(3.280833F) { //convert meters to Distance float fltfeet = MTF * meters; //convert to float feet feet = int(fltfeet); //feet is integer part inches = 12*(fltfeet-feet); //inches is what’s left }
From Basic to User-Defined Object Oriented Programming | [email protected]
17
• This function is called when an object of type Distance is created with a single argument. • The function assumes that this argument represents meters. It converts the argument to feet and inches, and assigns the resulting values to the object. • Thus the conversion from meters to Distance is carried out along with the creation of an object in the statement • Distance dist1 = 2.35;
From Basic to User-Defined Object Oriented Programming | [email protected]
18
• from a user-defined type to a basic type? • The trick here is to create something called a conversion operator: operator float() { float fracfeet = inches/12; fracfeet += float(feet); return fracfeet/MTF; }
From User-Defined to Basic Object Oriented Programming | [email protected]
19
• Consider the following two classes • Celsius and Fahrenheit
• Each one having an attribute Temp for storing temperature in Celsius and Fahrenheit respectively. • Each class has two overloaded definitions of constructors one for 0-argument and second for 1-argument to initialize the attribute Temp. • Another method showTemp() in both classes is defined to display the contents of Temp from the instance invoking the method
Conversion between user’s defined data types Object Oriented Programming | [email protected]
20
• Formula’s of conversion b/w Celsius and Fahrenheit Celsius to Fahrenheit: °F = (°C × 9/5) + 32 Fahrenheit to Celsius: °C = (°F − 32) x 5/9 °F to °C
Deduct 32, then multiply by 5, then divide by 9
°C to °F
Multiply by 9, then divide by 5, then add 32
Conversion between user’s defined data types Object Oriented Programming | [email protected]
21
class Fahrenheit { float Temp; public: Fahrenheit ():Temp(0.0f){ } Fahrenheit (float Val):Temp(Val){ } void showTemp()const { cout<<endl<<"The Temperature in Fahrenheit="<
Conversion between user’s defined data types Object Oriented Programming | [email protected]
22
class Celsius { float Temp; public: Celsius ():Temp(0.0f){ } Celsius (float Val):Temp(Val){ } void showTemp()const{ cout<<endl<<"The Temperature in Celsius="<
Conversion between user’s defined data types Object Oriented Programming | [email protected]
23
int main() {
Fahrenheit F1; Celsius C1(36.0f); F1.showTemp(); C1.showTemp(); F1=static_cast(C1); //equivalent to F1 = C1; cout<<endl<<"After Conversion"; F1.showTemp(); return 0; }
Conversion between user’s defined data types Object Oriented Programming | [email protected]
24
End for a while Enjoy the day 25 Object Oriented Programming | [email protected]