Operator Overloading in C++

Operator Overloading in C++

Operator overloading in the C++ programming language allows C++ users to redefine or declare the default behavior of built-in C++ program operators (such as +, -, *, etc.) for user-defined data type member parameters (such as class or structure data types). This allows C++ users to make objects of user-defined class data types behave more like built-in data types. This allows C++ users to easily apply class operators to class or structure data types with a custom class object argument.

Operator Overloading in C++

Operator overloading in C++ programs allows C++ users to increase the readability of the source code of class or structure data types by allowing them to manipulate objects of existing classes using operators best defined for their specific data types.

Special features of class operator overloading in C++.

  • Syntax – C++ users can use the operator symbol followed by the operator keyword to overload an operator in a class or structure data type.
  • Member vs. Non-Member – Operator overloads in a class or structure data type can be defined as member data type functions or non-member functions (friend functions).
  • Preserving Original Functionality – C++ users can apply built-in operators to built-in types even when overloading them for their own user-defined custom data types in a class or structure data type.
  • Functionality – Operator overloading in a C++ program does not allow creating new operators or modifying or changing the precedence or associativity of existing C++ class operators.

Basic syntax of operator overloading in a C++ program.

Syntax of member functions in operator overloading.

class ClassName {

public:

ReturnType operator OPERATOR(Parameter/ArgumentList) {

// User-defined class function body statement

}

};

Syntax of non-member (friend function) in operator overloading.

class ClassName {

friend ReturnType operator OPERATOR(ClassName& obj, ArgumentType arg);

};

Explanation of member function and non-member function in operator overloading.

  • Member function – In a C++ class member function or structure program, the operator is a built-in member function, and it is passed implicitly as the left-hand operand of the current class member.
  • Non-member function – In a C++ class or non-member structure program, the operator is defined as a non-member function, and the C++ user must pass this left-hand operand explicitly.

Basic examples of operator overloading in a C++ class.

So, let’s create a complex program in a C++ class that overloads the + arithmetic operator to add two complex float data type members.

Overloading the + operator in a C++ class.

#include <iostream>

using namespace std;

class ComplexNumbers {

private:

float realnumbers;

float imaginarynumbers;

public:

// Here we define the constructor for ComplexNumbers to the class data type

ComplexNumbers(float r = 0, float i = 0) : realnumbers(r), imaginarynumbers(i) {}

// Here we overload the + arithmetic operator as a member of the class function

ComplexNumbers operator + (const ComplexNumbers& other) {

return ComplexNumbers(realnumbers + other.realnumbers, imaginarynumbers + other.imaginarynumbers);

}

// Here we define a preview function for displaying the ComplexNumbers object.

void preview() {

cout << realnumbers << ” + ” << imaginarynumbers << “i” << endl;

}

};

int main() {

ComplexNumbers cmplx1(7.4, 6.5), cmplx2(2.4, 3.5);

ComplexNumbers cmplx3 = cmplx1 + cmplx2; // Here we use the overloaded + operator method to add real and imaginary complex numbers.

cmplx3.preview(); // Result – 9.8 + 10i

return 0;

}

Explain operator overloading in a C++ class.

  • In this example, the + arithmetic operator is overloaded as a member function of the ComplexNumbers class.
  • The member function operator + takes a complex object as an argument and returns a new complex object representing the sum of two real and imaginary complex numbers.
  • The main() function in the ComplexNumbers class applies the overloaded + operator to return the total value of two complex objects.

Leave a Reply