Conversions: Measure to meters, meters to Measure - C++ Class

C++ examples for Class:Operator Overload

Description

Conversions: Measure to meters, meters to Measure

Demo Code

#include <iostream>
using namespace std;
class Measure//www.j  a  v a 2 s  .  c o m
{
   private:
       const float MTF;            //meters to feet
       int feet;
       float inches;
   public:
       Measure() : feet(0), inches(0.0), MTF(3.280833F)
       {  }                     //constructor (one arg)
       Measure(float meters) : MTF(3.280833F)
       {                        //convert meters to Measure
           float fltfeet = MTF * meters;  //convert to float feet
           feet = int(fltfeet);           //feet is integer part
           inches = 12*(fltfeet-feet);    //inches is what's left
       }
        
        Measure(int ft, float in) : feet(ft),
        
        inches(in), MTF(3.280833F)
        {  }
        
        void getdist()              //get length from user
        {
           cout << "\nEnter feet: ";  cin >> feet;
           cout << "Enter inches: ";  cin >> inches;
        }
        
        void showdist() const
        { cout << feet << "\'-" << inches << '\"'; }
        
        operator float() const      //conversion operator
        {                        //converts Measure to meters
            float fracfeet = inches/12;     //convert the inches
            fracfeet += static_cast<float>(feet); //add the feet
            return fracfeet/MTF;            //convert to meters
        }
};
int main()
{
    float mtrs;
    Measure dist1 = 2.35F;        //uses 1-arg constructor to

    //convert meters to Measure

    cout << "\ndist1 = "; dist1.showdist();
    mtrs = static_cast<float>(dist1); //uses conversion operator

    //for Measure to meters
    cout << "\ndist1 = " << mtrs << " meters\n";
    Measure dist2(5, 10.25);      //uses 2-arg constructor
    mtrs = dist2;                  //also uses conversion op
    cout << "\ndist2 = " << mtrs << " meters\n";

    // dist2 = mtrs;                  //error, = won't convert
    return 0;
}

Result


Related Tutorials