Answer to Question #186172 in C++ for Ajith

Question #186172

Construct the C++ program using operator function with type conversion to convert the distance in meters in centimeters and vice versa.


1
Expert's answer
2021-04-28T20:16:19-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS