Addition of Distance objects

Description:

write a C++ program to add two distance objects using binary + operator Test Case 1 Input (stdin) 22 10 78 17 Expected Output Sum of Distance is: 102 Feet 3 Inches Test Case 2 Input (stdin) -5 10 45 -7 Expected Output Sum of Distance is: 40 Feet 3 Inches

Program :

/*C++ program to add two distances using binary plus (+) operator overloading.*/

 

#include<iostream>

using namespace std;

 

class Distance

{

    private:

        int feet,inches;

         

    public:

        //function to read distance

        void readDistance(void)

        {

            //cout << "Enter feet: ";

            cin >>feet;

            //cout << "Enter inches: ";

            cin >>inches;

        }

        //function to display distance

        void dispDistance(void)

        {

            cout << feet<< " Feet " << inches<< " Inches" ;//;< endl;

        }

        //add two Distance using + operator overloading

        Distance operator+(Distance &dist1)

        {

            Distance tempD;    //to add two distances

            tempD.inches= inches + dist1.inches;

            tempD.feet = feet  + dist1.feet + (tempD.inches/12);

            tempD.inches=tempD.inches%12;

            return tempD;

        }

};

int main()

{

    Distance D1,D2,D3;

     

   // cout << "Enter first distance:" << endl;

    D1.readDistance();

  // cout << endl;

     

    //cout << "Enter second distance:" << endl;

    D2.readDistance();

    //cout << endl;

     

    //add two distances

    D3=D1+D2;

     

    cout << "Sum of Distance is:\n";

    D3.dispDistance();

 

    cout << endl;

    return 0;

}