Question #149259

2. Class Distance consists of length in feet and inches. Class Distance contains

1. one default constructor

IF. one parameterized constructor III. Function getdata () to take the value of feet and inches.

IV. Function show () to display.

V. Overload += operator in the Distance class.

Overload < operator to compare two distances.

Expert's answer

#include <iostream>


using namespace std;


class Distance{
    private:
        int feet;
        int inches;
    public:
        Distance(){}
        Distance(int ft, int in): feet(ft), inches(in){}
        void show(){
            cout << feet << " feet and " << inches << " inches" << "\n";
        }
        Distance operator+=(const Distance& d){
            this->feet = feet+d.feet;
            this->inches  = inches+d.inches;
            return *this;
        }
        void getdata(int ft, int in){
            feet = ft;
            inches = in;
        }
        bool operator<(const Distance& d){
            if(feet < d.feet) {
                return true;
            }
            if(feet == d.feet && inches < d.inches) {
                return true;
            }


            return false;
      }


};

LATEST TUTORIALS
APPROVED BY CLIENTS