Wednesday 16 August 2017

Create a class distance to add an overloaded - operator that subtracts two distances. It should allow statements like dist3=dist1-dist2;. Assume that the operator will never be used to subtract a larger number from a smaller one (that is, negative distances are not allowed).



Create a class distance to add an overloaded - operator that subtracts two distances. It should allow statements like dist3=dist1-dist2;. Assume that the operator will never be used to subtract a larger number from a smaller one (that is, negative distances are not allowed).

SOLUTION:
#include "stdafx.h"
#include "iostream"
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////
class Distance
{
            private:
                        int feet;            float inches;
            public:
                        Distance( ): feet(0), inches(0.0)
                                    {/*Empty Body*/}
                        Distance(int ft, float in): feet(ft), inches(in)
                                    {/*Empty Body*/}
                        void getdist( )
                        {
                                    cout << "Enter Feet: ";                        cin >> feet;
                                    cout << "Enter Inches: ";         cin >> inches;
                        }
                        void showdist( ) const
                        {
                                    cout << feet << "'" << inches << "\"";
                        }
                        Distance operator - (Distance) const;
};
//---------------------------------------------------------------------------------------
Distance Distance::operator - (Distance d2) const
{
            int f=0; float i=0.0;
            if(feet > d2.feet)
            {
                        f = feet - d2.feet;
                        if(inches > d2.inches)
                        {
                                    i = inches - d2.inches;
                                    if(i >= 12.0)
                                    {
                                                i-=12.0;
                                                f++;
                                    }
                        }
                        else if(inches < d2.inches)
                        {
                                    i = d2.inches - inches;


                                    if(i >= 12.0)
                                    {
                                                i-=12.0;
                                                f++;
                                    }
                        }
            }
            else if(feet < d2.feet)
            {
                        f = d2.feet - feet;
                        if(inches > d2.inches)
                        {
                                    i = inches - d2.inches;
                                    if(i >= 12.0)
                                    {
                                                i-=12.0;
                                                f++;
                                    }
                        }
                        else if(inches < d2.inches)
                        {
                                    i = d2.inches - inches;
                                    if(i >= 12.0)
                                    {
                                                i-=12.0;
                                                f++;
                                    }
                        }
            }
            return Distance(f,i);
}
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
{
            Distance dist1, dist2(10,6.8), dist3, dist4;
            dist1.getdist( );
            dist3 = dist1 - dist2;
            dist4 = dist1 - (dist2 - dist3);
            cout << "dist1 = ";                   dist1.showdist();
            cout << "\ndist2 = ";                dist2.showdist();
            cout << "\ndist3 = ";                dist3.showdist();
            cout << "\ndist4 = ";                dist4.showdist();
            cout << endl;
            system("pause");
}

OUTPUT:


0 comments: