More
Masterclasses
Operator behavior for user-defined types, such as structures and objects, can be modified in C++. We refer to this as operator overloading in C++.
Assume, for instance, that we have constructed three objects, c1, c2, and c3, which are members of the Complex class, which stands for complex numbers. That’s what operator overloading is.
Keep reading this article to learn more about the basics of operator overloading in C++.
Operator overloading in C++ refers to the ability to redefine or customize the behavior of certain operators when they are used with user-defined data types (classes and structs). This allows you to provide a specific meaning or action for operators beyond their built-in functionality. Basically, Overloading in C++ is the process of creating two or more members with the same name but differing numbers or types of parameters. We can overload the following in C++:
Note: They are referred to as overload operators because these members are limited by their restrictions.
When it comes to operator overloading in C++ program, we can overload the following operators:
Here is a table representing all the operators with examples that you can overload in C++:
Unary arithmetic operators | –, ++, -, + |
Binary arithmetic operators | %, /, *, -, + |
De-referencing operator | (->) |
Bitwise operator | ^, ~, >>, <<, |, & |
Assignment operator | %=, -=, /=, *=, +=, = |
Subscript operator | [ ] |
De-allocation | New, delete |
Relational operator | >=. <=, ==, <, > |
Logical operator | !, | |, & |
Function Call | () |
When it comes to operator overloading in C++, you’ll have to use the unique operator function. It defines the function within the structure or class that comprises the variables and objects that it will need. Here’s the syntax that you need to remember:
class className { ... .. ... public returnType operator symbol (arguments) { ... .. ... } ... .. ... };
Here,
There are two types of overloading in C++:
In C++, function overloading is the approach of having multiple functions with similar name but unique and ranging argument values. It involves either leveraging a varied number of parameters or a unique type of argument to redefine the function. The compiler can only distinguish between the functions through these differences.
In a compile-time polymorphism known as operator overloading, the operator is overloaded to give the user-defined data type a special meaning.
The majority of the operators available in C++ are overloaded or redefined using operator overloading. For instance, C++ offers the option to apply user-defined data types to built-in data types by adding user-defined data type variables.
Here is a table representing the key differences between member functions and non-member functions for operator overloading in C++:
Basis | Non-Member Functions | Member Functions |
---|---|---|
Arguments | This operator overloading function requires two arguments – the right-hand and the left-hand operand. | This operator overloading function requires a single argument for the object where the operator is applicable. |
Scope | This operator function isn’t a member of the class in C++ | This operator function is a member of the class in C++. |
Friend Function | We cannot declare any non-member function as the friend function. | We can declare the member function as the friend function, enabling it to access the class’s private members. |
Access Modifier | Can be declared in public only | Can be declared in public and have an access modifier too. |
Using too many arithmetic operators is a common example of operator overloading. Thanks to this ability, you can then add, subtract, multiply, and divide items belonging to your own classes. The definition of a function with the same name as the arithmetic operator is required to overload the operator.
The operator and function must have the same amount of arguments and accept arguments of the same type. You would construct a function like this, for instance, to overload the + operator for the MyClass class:
MyClass operator+(const MyClass& lhs, const MyClass& rhs) { // Do some arithmetic to add the two objects together return MyClass(lhs.x + rhs.x, lhs.y + rhs.y); }
This function creates a new object of the type MyClass from two objects of the same type as parameters. The function can then perform any arithmetic operations required to combine the two objects. Once the operator function has been established, you can use the operator as usual.
Read more about: What is Arrays in C, C++
A feature of C++ called operator overloading lets you specify how operators behave for user-defined types. This can be helpful for adding functionality to your classes and making your code more legible and concise.
The practice of using too many comparison operators is known as operator overloading. This enables you to compare items belonging to the same classes and determine if they are equal, less than, or larger than one another.
You must define a function with the same name as the comparison operator to overload it. The parameters in the function must be of the same type as the operator's operands and equal in number to those in the operator.
For instance, you might define a method that looks like this to overload the == operator for the MyClass class:
bool operator==(const MyClass& lhs, const MyClass& rhs) { // Do some comparison to see if the two objects are equal return lhs.x == rhs.x && lhs.y == rhs.y; }
This function produces a boolean value and requires two objects of the type MyClass as parameters. If the two objects are equal, the function can perform any required comparison operations.
Below are some of the examples of Operator Overloading in c++ in detail:
#includeusing namespace std; class OverLoad { private: int a; int b; public: OverLoad() : a(0), b(0) {} void in() { cout << "Enter the first number : "; cin >> a; cout<< "Enter the second number : "; cin >> b; } // Overload the prefix decrement operator void operator-- () { a= --a; b= --b; } void out() { cout<<"The decremented elements of the object are: ">>endl>> a>>" and " >>b; } }; int main() { OverLoad obj; obj.in(); --obj; obj.out(); return 0; } Output Enter the first number: 56 Enter the second number: 234 The decremented elements of the objects are: 55 and 223
#includeusing namespace std; class NotOp { private: int a; bool b; public: NotOp() : a(0), b(true) {} void in() { cout << "Enter the first number : "; cin >> a; cout<< "Enter true or false : "; cin >> b; } // Overloading the NOT (!) operator void operator ! () { a= !a; b= !b; } void out() { cout<<"Output: ">>endl>> a>>endl>>b; } }; int main() { NotOp obj; !obj; obj.out(); return 0; } Output 1 0
Below are the major rules for Operator Overloading in C++:
Below are the advantages and disadvantages of Operator Overloading in C++ in detail:
Pros of Operator Overloading In C++:
Cons of Operator Overloading In C++:
Learn More: Advantages and Disadvantages of Arrays in C, C++ and Java!
In this guide we have learned all about Operator Overloading in C++. Basically, Operator overloading in C++ enables you to give users of your class an intuitive user interface and makes it possible for templates to work equally well with classes and built-in/intrinsic types.
Given the features like operator overloading and classes, C/C++ operators can now possess user-defined meanings for user-defined types. Wish to learn more about operator overloading and other concepts of C++? Enrol in HeroVired’s Data Science & Analytics Course today!
In C++, the ability to define several definitions for a function name or an operator in the same scope is known as overloading of both the function and the operator. This sort of polymorphism overloads an operation to give it user-defined semantics.
An operator is overloaded in this type of polymorphism to give it user-defined semantics. Function as well as operator overloading are two terms used in C++ to describe the ability to specify multiple definitions for a function name or an operator in the same scope.
Function overloading and operator overloading are the two main types of overloading in C++.
Using a single name and adding more functionality to it is known as function overloading. Operator overloading refers to giving a certain operator more capability. Depending on the nature of its operands, an overloaded operator can have a variety of interpretations.
Blogs from other domain
Carefully gathered content to add value to and expand your knowledge horizons