What is Operator overloading? Design a Distance class with data members in meters, centimeters, and millimeters. Write a program to overload addition and subtraction operators respectively on Distance class and derive the data members using getter and setter functions. Also, overload the input/output operators for the Distance class.


Q.) What is Operator overloading? Design a Distance class with data members in meters, centimeters, and millimeters. Write a program to overload addition and subtraction operators respectively on Distance class and derive the data members using getter and setter functions. Also, overload the input/output operators for the Distance class.

Subject: Object Oriented Programming and Methodology

Operator overloading is a feature in C++ where different operators are used to perform more than one operation. This means that the same operator behaves differently with different types. For example, the '+' operator can be used to add two integers as well as join two strings and merge two lists. It is an example of compile time polymorphism.

In C++, we can overload:

  • Unary operators: ++, --, -, !
  • Binary operators: +, -, *, /, %, etc.
  • Relational operators: ==, !=, <, >, <=, >=
  • Logical operators: &&, ||, !
  • Assignment operators: =, +=, -=, *=, /=, etc.
  • Misc operators: [], (), ->, new, delete, etc.

Now, let's design a Distance class with data members in meters, centimeters, and millimeters. We will overload addition and subtraction operators for this class and derive the data members using getter and setter functions. We will also overload the input/output operators for the Distance class.

#include
using namespace std;

class Distance {
    private:
        int meter;
        int centimeter;
        int millimeter;

    public:
        // Constructor
        Distance(int m=0, int cm=0, int mm=0) : meter(m), centimeter(cm), millimeter(mm) {}

        // Getter functions
        int getMeter() { return meter; }
        int getCentimeter() { return centimeter; }
        int getMillimeter() { return millimeter; }

        // Setter functions
        void setMeter(int m) { meter = m; }
        void setCentimeter(int cm) { centimeter = cm; }
        void setMillimeter(int mm) { millimeter = mm; }

        // Overloading + operator
        Distance operator+(const Distance&amp; d) {
            Distance dist;
            dist.meter = this-&gt;meter + d.meter;
            dist.centimeter = this-&gt;centimeter + d.centimeter;
            dist.millimeter = this-&gt;millimeter + d.millimeter;
            return dist;
        }

        // Overloading - operator
        Distance operator-(const Distance&amp; d) {
            Distance dist;
            dist.meter = this-&gt;meter - d.meter;
            dist.centimeter = this-&gt;centimeter - d.centimeter;
            dist.millimeter = this-&gt;millimeter - d.millimeter;
            return dist;
        }

        // Overloading &lt;&lt; operator
        friend ostream&amp; operator&lt;&lt;(ostream&amp; out, const Distance&amp; d) {
            out &lt;&lt; d.meter &lt;&lt; " meter " &lt;&lt; d.centimeter &lt;&lt; " centimeter " &lt;&lt; d.millimeter &lt;&lt; " millimeter";
            return out;
        }

        // Overloading &gt;&gt; operator
        friend istream&amp; operator&gt;&gt;(istream&amp; in, Distance&amp; d) {
            in &gt;&gt; d.meter;
            in &gt;&gt; d.centimeter;
            in &gt;&gt; d.millimeter;
            return in;
        }
};

int main() {
    Distance d1(1, 20, 300);
    Distance d2(2, 40, 600);

    Distance d3 = d1 + d2;
    cout &lt;&lt; "Sum: " &lt;&lt; d3 &lt;&lt; endl;

    Distance d4 = d1 - d2;
    cout &lt;&lt; "Difference: " &lt;&lt; d4 &lt;&lt; endl;

    return 0;
}

In the above program, we have overloaded '+' and '-' operators to perform addition and subtraction of Distance objects. We have also overloaded '<<' and '>>' operators to perform output and input operations on Distance objects. The '<<' operator must be overloaded as a global function, while the '>>' operator can be overloaded as a member function. However, in both cases, it is customary to overload these operators as global functions.