Answer to Question #148844 in C++ for Raksha thakur

Question #148844
Class distance consist of length in feet and inches.class distance contain ..
1.one default constructor
2.one perameterized constructor .
3. Function getdata () to take the values of feet and inches
4. Function show() to display
5. Overload +=operator in the distance class
Overload <operator to compare two distance
1
Expert's answer
2020-12-06T10:22:43-0500
class Distance
{
private:
    int feet;
    int inches;
    
public:
    // Default constructor
    Distance() {
        feet = inches = 0;
    }    
    // Parametrrized constructor
    Distance(int f, int i) {
        inches  = i % 12;
        feet = f + i / 12;
    }
    // Return data
    int getFeet() {
        return feet;
    }
    int getInches() {
        return inches;
    }
    void show() {
        std::cout << feet << " feet " << inches << " inches";
    }
    
    Distance& operator+=(Distance other) {
        feet += other.feet;
        inches += other.inches;
        if (inches > 12) {
            inches -= 12;
            feet++;
        }
        return *this;
    }
    bool operator<(Distance other) {
        if (feet< other.feet)
            return true;
        else if (feet == other.feet && inches < other.inches)
            return true;
        else
            return false;            
    }
};

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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog