Answer to Question #214078 in C++ for shayan abbasi

Question #214078

Write a program having a Class Mile with data member dis_mile. Another Class Kilometer with data member dis_kilo. Both classes stores distances. In main function create objects these two classes with parameterized constructors. Write down conversion function for converting Kilometer object to Mile class object


1
Expert's answer
2021-07-05T18:05:25-0400
#include <iostream>

class Mile
{
    double dis_mile;

  public:

    explicit Mile(double mile) : dis_mile(mile)
    {
    }

    double Get() const { return dis_mile; }
};

class Kilometer
{
    double dis_kilo;

  public:

    explicit Kilometer(double km) : dis_kilo(km)
    {
    }

    double Get() const { return dis_kilo; }
};

Mile Convert(Kilometer km)
{
    return Mile(km.Get() * 0.621371);
}

int main()
{
    Kilometer km(1);
    Mile mile(1);
    
    std::cout << km.Get() << " km = " << Convert(km).Get() << " miles\n";
    
    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

shayan abbasi
06.07.21, 15:54

Thank you

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS