0% found this document useful (0 votes)
6 views17 pages

PPT9_Polymorphism_TypeConversion

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views17 pages

PPT9_Polymorphism_TypeConversion

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

UTA 018: OOPs

Poly-morphism
(Type Conversion)

1
What is Type Conversions?
• When constants and variables of different types are mixed in an
expression, C++ applies automatic type conversion to the operand
as per certain rules. Similarly, an assignment operator also causes
the automatic type conversion.
• The type conversions are automatic as long as the data types
involved are built-in types.

 What happens when they are user defined data types?


 What if one of the operands is an object and the other is built-in
type variable?
 What if they belong to two different classes?
What is Type Conversions?
• Since the user-defined data types are designed by us to suit our
requirements, the compiler does not support automatic type
conversions for such data types.

• Types of conversions: Three type of situations might arise in


the data conversion between incompatible types:
• Basic to User-defined
• User-defined to Basic
• One User-defined to Another User-defined
Basic to User-defined (Class Type)
• In this type of conversion, the source type is basic type and the
destination type is class type, i.e. basic data type is converted into
the class type.

• The conversion from basic type to the class type can be


performed in two ways:

 Using constructor
 Using Operator Overloading
Example (Using Constructor)
#include <iostream> int main() {
using namespace std; int distance;
cout<<"Enter distance in meters";
class Distance { cin>>distance;
private: Distance d = 5; // Implicit conversion from int to Distance
int feet;
float inches; d.display(); // Output: 16 feet 4.12 inches
public: return 0;
// Constructor for basic to user-defined conversion }

Distance(int meters) {
feet = meters * 3.281; // Convert meters to feet During type conversion using the
inches = (meters * 3.281 - feet) * 12; constructor we can pass only one
} argument and we can do type
conversion at the type of
void display() { initialization only.
cout << feet << " feet " << inches << " inches" << endl;
}
};
Using Operator Overloading
• Type conversion from basic to class type can also be done by
operator overloading.

• Assignment operator can be overloaded for this purpose.

• Previous example of Disptance class can be rewritten for type


conversion using operator overloading concept to overload the
assignment operator (=).
Example (Using (=) Operator Overloading)
#include <iostream>
using namespace std;
int main() {
class Distance { Distance d; // Default Distance object
private: int meters = 5; // Basic type (meters)
int feet;
float inches; // Assign the basic type (int) to a user-defined object
public: using overloaded assignment operator
Distance(int f = 0, float i = 0) : feet(f), inches(i) {}
d = meters;
Distance& operator=(int meters) {
// Convert meters to feet and inches // Display the converted distance
feet = meters * 3.281; // 1 meter = 3.281 feet d.display(); // Output: 16 feet 3.36 inches
inches = (meters * 3.281 - feet) * 12;
return *this; return 0;
} }

void display() const {


cout << feet << " feet " << inches << " inches" << endl;
}
};
User-defined to Basic Type
• In this type of conversion the source type is class type and the
destination type is basic type, i.e. class data type is converted into
the basic type.
• The constructor functions do not support this operation.
• It requires special casting operator function.
• The syntax for an overloaded casting operator function, usually
referred to as a conversion function, is:

operator typename( )
{
//Function statements
}
Continued…
• The conversion function should satisfy the following condition:

 It must be a class member.


 It must not specify the return value.
 It must not have any argument.
Example #include<iostream>
using namespace std; class Time
{
int hrs, min;
public:
Time (int ,int); // constructor
operator int(); // casting operator function
~Time() // destructor
{
cout<<"Destructor called..."<<endl;
}
};
Time::Time (int a,int b)
{
cout<<"Constructor called with two parameters..."<<endl; hrs=a;
min=b;
}
Continued…
Time :: operator int()
{
cout<<"Class Type to Basic Type Time t(h,m); // construct object
Conversion..."<<endl; duration = t; // casting conversion
return(hrs*60+min); OR duration = (int)t
}
int main()
cout<<"Total Minutes are "<<duration;
{
cout<<"2nd method operator overloading "<<endl;
int h, m, duration;
duration = t.operator int();
cout<<"Enter Hours ";
cout<<"Total Minutes are "<<duration;
cin>>h;
return 0;
cout<<"Enter Minutes ";
}
cin>>m;
int main() {
Example-2 Distance d(10, 6); // 10 feet 6 inches

#include <iostream> // Implicit conversion from Distance to int


using namespace std; int totalFeet = d;
cout << "Total feet (as int): " << totalFeet << endl;
class Distance {
private: // Implicit conversion from Distance to float (in meters)
int feet; float totalMeters = d;
float inches; cout << "Total meters (as float): " << totalMeters << "
public: meters" << endl;
Distance(int f, float i) : feet(f), inches(i) {}
operator int() { // convert Distance to int return 0;
return feet + static_cast<int>(inches / 12); }
}
operator float() { static_cast<int> is a type of casting operator in C++ that is
float totalFeet = feet + (inches / 12); used to perform explicit type conversions between
return totalFeet / 3.281; // Convert feet to meters compatible types. It is a safer and more precise alternative
} to C-style casting (type)value and is part of C++'s explicit
type conversion mechanisms.
void display() const {
cout << feet << " feet " << inches << " inches" << endl; Syntax: static_cast<new_type>(expression);
}
};
User-defined to User-defined Type
• In this type of conversion both the type that is source type and
the destination type are of class type.

• Conversion from one class to another class can be performed


either by using the constructor or type conversion function.
Example
#include <iostream>
class MetricDistance {
using namespace std;
private:
float meters;
class Distance {
public:
private:
// Conversion constructor from Distance to MetricDistance
int feet;
float inches;
MetricDistance(Distance d) {
public:
meters = (d.getFeet() + d.getInches() / 12) / 3.281;
Distance(int f, float i) : feet(f), inches(i) {}
}
int getFeet() { return feet; }
float getInches() { return inches; }
void display() {
};
cout << meters << " meters" << endl;
}
};
int main() {
Distance d(10, 6); // 10 feet 6 inches
MetricDistance md = d;
// Conversion to MetricDistance
md.display(); // Output: 3.2 meters
return 0;
}
Example
#include<iostream> class Minute{ int main()
using namespace std; private: {
int min; Time t1(2,30);
class Time{ public: t1.display();
private: Minute():min(0){} Minute m1;
int hrs, min; m1.display();
public: void operator=(Time T) { // conversion from
Time(int h=0,int m=0): hrs(h), min(m){} min=T.getMinutes(); m1 = t1; Time to Minute
int getMinutes(){ }
int tot_min = ( hrs * 60 ) + min ;
void display() t1.display();
return tot_min;
} { m1.display();
void display() { cout<<"\n Total Minutes : " <<min<<endl; return 0;
cout<<"Hours: "<<hrs<<endl; } }
cout<<" Minutes : "<<min <<endl ; };
}
};
Type Conversions Summary

Conversion Conversion takes place in


required Source Class Destination Class
Basic to Class Not Applicable Constructor

Class to Basic Casting Operator Not Applicable

Class to Class Casting Operator Constructor


Thanks

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy