Construct the C++ program using operator function with type conversion to convert the distance in meters in centimeters and vice versa.
#include <iostream>
using namespace std;
class Meters;
class Centimeters{
public:
float x;
Centimeters(){}
Centimeters(float y): x(y) {}
void operator=(Meters);
};
class Meters{
public:
float x;
Meters(){}
Meters(float y): x(y) {}
void operator=(Centimeters cm){
x = cm.x / 100;
}
};
void Centimeters::operator=(Meters m){
x = 100 * m.x;
}
int main(){
cout<<"Enter centimeters to convert to meters: ";
float x; cin>>x;
Centimeters cm(x);
Meters m;
m = cm;
cout<<m.x<<" meters";
cout<<"\nEnter meters to convert to centimeters: ";
cin>>x;
m = Meters(x);
cm = m;
cout<<cm.x<<" centimeters";
return 0;
}
Comments
Leave a comment