Conversion Operator
_______________________________________________________________________
//PROGRAM 1
#include<iostream>
using namespace std;
class Meter
{
int data;
public:
Meter():data(0){cout<<"In the default constructor\n";}
Meter(int d){
data =d ;
cout<<"In the one arg constructor\n";
}
Meter& operator = (Meter& m)
{
cout<<"overloaded operator called..\n";
}
/* Meter(Meter& m)
{
this->data = m.data;
}*/
Meter& operator = (int x)
{
cout<<"new overloaded operator called..\n";
this->data = x;
return *this;
}
operator int()
{
cout<<"operator int is called.."<<endl;
return this->data;
}
void Display()
{
cout<<data <<" "<<endl;
}
~Meter(){ cout<<"In the destructor...\n";}
};
int main()
{
Meter m =50; /* This will call one agr constr.. You should not give the definition of any explicit copy constructor. ?? */
m.Display();
Meter m1 = m; /* This will not call any constructor */
m=60; //First it will look for overloaded assignment operator. If it is not found, it will call the one argumrnt constructor.
//If you define any oveloaded assignment operator function then you have to give the one overloaded version of it also which will take
//one argument as one interger.
// If there is not explicit overloaded assignment operator, one argument constructor will be called */
int a = m; // This will call the operator conversion function
cout<<"a= "<<a<<endl;
m.Display();
m1.Display();
Meter& mm = m;cout<<"Hi"<<endl;
mm.Display();cout<<endl;
}
//PROGRAM 2
/* write 2 Classes Dollor and Rupee having float data member amount;
Test Following code at client side
Dollar d(8);
Rupee r(500);
d = r; //Provide source & Destination class Solution
r = d; // Provide Source & Destination Class Solution
By convention the source class is onc which appears at the right hand side of the = operator, and the LHS is the destination class
for example in d = r expression , Rupee (r is object of Rupee) class is regarded as the source class and DOllar class is the
destination class
/*
/*
solution :
Dollar d(8);
Rupee r(500);
for the below two operations :
d = r;
r = d;
let assume we are providing both the implementation in the Dollar class only.
So, for d = r, one solution(first) is just provide an overloaded assignment operator function which takes one argument as Rupee object
2nd solution for d = r (in the absence of the first solution) is that it will look for an one agrgument constructor where Rupee object
will be passed as the parameter.
for r = d, one conversion operator is required in the Dollar class whilh will convert Dollar into Rupee ojbect.
If you want to give the implementation in the Rupee class for both the operations, you can implement in exactly the same way for the reverse operations
*/
#include<iostream>
using namespace std;
class Rupee;
class Dollar
{
double amount;
public:
Dollar():amount(0){}
Dollar(int amt):amount(amt){}
/*Dollar(const Rupee& r ) //2nd solution for d = r conversion. This is commented because the first solution is also available.
//in order to uncomment this, comment the first solution (overloaded = operator solution )
{
amount = r.GetRupee() / 46.5; //convrting rupee into dollar
}*/
void operator = (Rupee& r); //overloading = operator. first solution for d= r conversion
operator Rupee();
double GetDollar()
{
return amount;
}
};
class Rupee
{
double amount;
public:
Rupee(): amount(0){}
Rupee(int amt):amount(amt){}
double GetRupee()
{
return amount;
}
};
void Dollar::operator = (Rupee& r) //overloading = operator. first solution for d= r conversion
{
amount = r.GetRupee() / 46.5; //convirting rupee into dollar
}
Dollar::operator Rupee()
{
return Rupee(amount*46.5); //converting dollar into rupee
}
int main()
{
Dollar d(8);
Rupee r(500);
cout<<"dollar: "<<d.GetDollar()<<endl;
cout<<"rupee: "<<r.GetRupee()<<endl;
d = r; //Provide source & Destination class Solution
cout<<"Rs 500= $"<<d.GetDollar()<<endl;
d = 8; // This will call Dollar(int amt) constructor in the absence of overloaded operator = (int) method.
r = d;
cout<<"$8 = Rs "<<r.GetRupee()<<endl;
}
//PROGRAM 3
/* write 2 Classes Dollor and Rupee having float data member amount;
Test Following code at client side
Dollar d(8);
Rupee r(500);
d = r; //Provide source & Destination class Solution
r = d; // Provide Source & Destination Class Solution
By convention the source class is onc which appears at the right hand side of the = operator, and the LHS is the destination class
for example in d = r expression , Rupee (r is object of Rupee) class is regarded as the source class and DOllar class is the
destination class
/*
/*
solution :
Dollar d(8);
Rupee r(500);
for the below two operations :
d = r;
r = d;
let assume we are providing both the implementation in the Dollar class only.
So, for d = r, one solution(first) is just provide an overloaded assignment operator function which takes one argument as Rupee object
2nd solution for d = r (in the absence of the first solution) is that it will look for an one agrgument constructor where Rupee object
will be passed as the parameter.
for r = d, one conversion operator is required in the Dollar class whilh will convert Dollar into Rupee ojbect.
If you want to give the implementation in the Rupee class for both the operations, you can implement in exactly the same way for the reverse operations
*/
#include<iostream>
using namespace std;
class Rupee;
class Dollar
{
double amount;
public:
Dollar():amount(0){}
Dollar(int amt):amount(amt){}
Dollar(Rupee& r ); // 2nd solution for d = r conversion. This is now uncommented and the the first solution
// (overloaded = operator solution ) is commented
// void operator = (Rupee& r); //overloading = operator. first solution for d= r conversion. This is commented as the 2nd
// solution (one parameter constructor) is present
operator Rupee();
double GetDollar()
{
return amount;
}
};
class Rupee
{
double amount;
public:
Rupee(): amount(0){}
Rupee(int amt):amount(amt){}
double GetRupee()
{
return amount;
}
};
Dollar::Dollar(Rupee& r ) // 2nd solution for d = r conversion. This is now uncommented and the the first solution
// (overloaded = operator solution ) is commented
{
amount = r.GetRupee() / 46.5; //convrting rupee into dollar
}
/*void Dollar::operator = (Rupee& r) //overloading = operator. first solution for d= r conversion
{
amount = r.GetRupee() / 46.5; //convirting rupee into dollar
}*/
Dollar::operator Rupee() //conversion operator for converting Dollar into external object called Rupee here.
{
return Rupee(amount*46.5); //converting dollar into rupee
}
int main()
{
Dollar d(8);
Rupee r(500);
cout<<"dollar: "<<d.GetDollar()<<endl;
cout<<"rupee: "<<r.GetRupee()<<endl;
d = r; //Provide source & Destination class Solution
cout<<"Rs 500= $"<<d.GetDollar()<<endl;
d = 8; // This will call Dollar(int amt) constructor in the absence of overloaded operator = (int) method.
r = d;
cout<<"$8 = Rs "<<r.GetRupee()<<endl;
}
_______________________________________________________________________
Copyright © Open Sky Technology |
Corporate Office: #4, RR Complex, 2nd Floor, Munnekolala, Marathahalli, Bangalore – 560037. (Landmark: near Munnekolala Bus Stop) Mobile: 9886333765,