2082 3

BIT153 ยท 2082 ยท 10 marks

Explain operator overloading with example?Write a C++ class Number to store an integer. Overload addition (+) and comparison (==) operators, and show conversion between int and Number. Write a program to perform these operations.[2+8]

Explain operator overloading with example?Write a C++ class Number to store an integer. Overload addition (+) and comparison (==) operators, and show conversion between int and Number. Write a program to perform these operations.[2+8]

Object Oriented ProgrammingChapter 7: Operator Overloading and Type ConversionOperator overloading definition and purposes

Quick answer

Operator overloading is a feature of C++ that allows existing operators (such as +, -, ==, <<, etc.) to be given additional meaning when applied to user-defined types (classes/objects). It enables objects of a class to be used with operators in a natural, intuitive way, just like built-in data types.

Operator Overloading in C++

(a) Explanation of Operator Overloading

Key Points:

  • The keyword operator followed by the operator symbol is used to define the overloaded function.
  • Syntax: return_type operator symbol (parameter list)
  • It does not change the precedence or associativity of operators.
  • At least one operand must be a user-defined type.

(b) C++ Program

#include <iostream>
using namespace std;

class Number {
private:
    int value;

public:
    // Default constructor
    Number() {
        value = 0;
    }

    // Parameterized constructor
    Number(int v) {
        value = v;
    }

    // -----------------------------------------------
    // Conversion: int to Number (done via constructor)
    // Conversion: Number to int (conversion operator)
    // -----------------------------------------------
    operator int() {
        return value;
    }

    // -----------------------------------------------
    // Overload addition operator (+)
    // Number + Number
    // -----------------------------------------------
    Number operator+(const Number& obj) {
        Number temp;
        temp.value = this->value + obj.value;
        return temp;
    }

    // -----------------------------------------------
    // Overload comparison operator (==)
    // -----------------------------------------------
    bool operator==(const Number& obj) {
        if (this->value == obj.value)
            return true;
        else
            return false;
    }

    // Display function
    void display() {
        cout << "Value = " << value << endl;
    }
};

// -----------------------------------------------
// Main function to demonstrate operations
// -----------------------------------------------
int main() {
    // int to Number conversion (via constructor)
    int x = 10;
    Number n1 = x;          // implicit conversion: int -> Number
    Number n2(20);          // direct initialization
    Number n3 = 10;         // implicit conversion: int -> Number

    cout << "n1: "; n1.display();
    cout << "n2: "; n2.display();
    cout << "n3: "; n3.display();

    // Addition operator overloading
    Number n4 = n1 + n2;    // calls operator+
    cout << "\nAfter n4 = n1 + n2:" << endl;
    cout << "n4: "; n4.display();

    // Comparison operator overloading
    cout << "\nComparing n1 and n3 (both have value 10):" << endl;
    if (n1 == n3)
        cout << "n1 == n3: TRUE" << endl;
    else
        cout << "n1 == n3: FALSE" << endl;

    cout << "\nComparing n1 and n2:" << endl;
    if (n1 == n2)
        cout << "n1 == n2: TRUE" << endl;
    else
        cout << "n1 == n2: FALSE" << endl;

    // Number to int conversion (via conversion operator)
    int result = n4;        // implicit conversion: Number -> int
    cout << "\nNumber to int conversion (n4 -> int): " << result << endl;

    return 0;
}

Output

n1: Value = 10
n2: Value = 20
n3: Value = 10

After n4 = n1 + n2:
n4: Value = 30

Comparing n1 and n3 (both have value 10):
n1 == n3: TRUE

Comparing n1 and n2:
n1 == n2: FALSE

Number to int conversion (n4 -> int): 30

Explanation of Key Concepts Used

FeatureHow it is done
int to NumberVia constructor Number(int v) - implicit conversion
Number to intVia conversion operator operator int()
+ overloadingNumber operator+(const Number& obj)
== overloadingbool operator==(const Number& obj)
  • Number n1 = x; calls the constructor Number(int v), converting int to Number.
  • int result = n4; calls operator int(), converting Number to int.
  • n1 + n2 calls operator+, returning a new Number object with the sum.
  • n1 == n3 calls operator==, returning true if values are equal.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.