![]() |
In C++, operator overloading is a feature of the OOPs concept that allows you to redefine the behavior for different operators when they are used with objects of user-defined classes. The plus operator (+) is a binary operator generally used for addition. In this article, we will learn how to overload the (+) plus operator in C++ for a user-defined class. Overloading the Plus(+) Operator in C++To overload the plus operator, we have to create an operator+ function inside our class and define its behavior inside this function’s body. The following syntax shows how to do it: myClass operator+ (const myClass& obj) const { // new behaviour } C++ Program to Overload the Plus(+) Operator for a ClassC++
Output
Result: 15 To know more about operator overloading, refer to the article – Operator Overloading in C++ |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |