Javatpoint Logo
Javatpoint Logo

Operator Overloading in C++ Using Friend Function

C++'s strong and crucial Operator overloading feature enables you to modify the behaviour of built-in operators for user-defined data types. As an object-oriented programming language, C++ has this as one of its primary characteristics.

To simplify and improve the readability of your code, you can make your classes and objects act like built-in data types via operator overloading. Utilizing buddy functions or member functions, you can overload operators in C++. We will concentrate on Operator overloading utilizing buddy functions in this extensive article.

Understanding Operator Overloading

Let's first grasp the idea of Operator overloading in general before delving into the intricacies of Operator overloading with friend functions.

Operators are used in C++ to perform various operations on built-in data types. For instance, you can multiply two floats with the '*' Operator or add two integers using the '+' Operator. Operator overloading enables you to define specific behaviours for these operators when working with user-defined types like classes and structs.

When you add more than one instance of an operator, you give it a new definition or behaviour when applied to objects from your unique class or struct. Your classes will feel more natural and expressive to write because of this, which makes them feel more like built-in types.

For instance, you could design a class called 'Vector' to represent 2D vectors and overload the + Operator to combine two vectors. This improves the readability and clarity of the code:

Utilizing a member function or another method without operator overloading would be necessary, which could result in less legible code.

Operator Overloading with Member Functions vs Friend Functions

Utilizing buddy functions or member functions, you can overload operators in C++. Before we get into Operator overloading utilizing buddy functions, let's discuss the main distinctions between these two strategies.

1. Overloading the Operator with Member Functions:

The Operator is handled as a class member when you overload an operator with a member function. According to this, a class member that defines the operator function must be one of the operands. Any compatible type, including additional user-defined types, may be used for the other operand.

The general syntax for utilizing a member function to overload an operator is as follows:

Here, 'op' represents the Operator you want to overload (e.g., '+', '-', '*', '/'), and 'parameters' represent the Operator's operands. The 'return_type' specifies the type of value the Operator should return.

For example, let's overload the '+' operator for a 'Vector' class using a member function:

The addition of the relevant components of two 'Vector' objects is accomplished in this example via the '+' Operator, which is overloaded as a member function. A brand-new 'Vector' object is the outcome.

2. Operator Overloading with Friend Functions:

Another method for overloading operators with non-member functions designated as friends inside the class is friend functions. Although friend functions are not class members, they can access the class's private members.

The general syntax for utilizing a friend function to overload an operator is as follows:

friend return_type operator op(parameters);

The function is declared a 'friend' of the class with this syntax, enabling it to access its private members. The remaining portions of the grammar resemble member function overloading.

Let's now talk about the benefits and applications of Operator overloading with buddy functions:

Benefits of Overloading Operators with Friend Functions:

  • Symmetry: When overloading binary operators, you can achieve symmetric behaviour using friend functions. With member functions, you can only access the data of the currently selected object, which might occasionally result in asymmetry. Since friend functions can access both operands' private members, the behaviour is guaranteed to be symmetric.
  • Non-Member Function: Friend functions can overload operators for user-defined types without altering the class declaration because they are not class members. This is helpful when you need help editing the original class.
  • Global Functions: Friend functions are global because they are defined outside the class. This promotes code reuse because they can be used for multiple classes or even different projects.
  • Enhanced Readability: When dealing with complex expressions involving several objects and operators, buddy functions sometimes produce more legible code.
  • Flexibility: When choosing which operators to overload, friend functions offer flexibility. Without being constrained by the member functions of the class, you can decide to overload only the operators that make sense for your class.

Use Cases for Friend Functions to Reduce Operator Overload

Operator overloading with friend functions is more appropriate in some situations, even if Operator overloading with member functions is frequently the better option for simple classes:

  • Mathematical Operations: Operator overloading with buddy functions can result in more symmetrical and natural behaviour when dealing with mathematical classes like complex numbers, matrices, or vectors.
  • Overloading External Types: Because you cannot change the original class definitions, buddy functions are required when you need to overload operators for built-in or external types (like 'int', 'double', or 'std::string') to operate with your custom classes.
  • Cross-Class actions: You can use friend functions to execute actions between objects of different classes without breaking encapsulation.

Now that we are familiar with the fundamentals of Operator overloading with buddy functions let's see how to put them into practice.

Operator Overloading with Friend Functions: Implementation Steps:

It would be best if you did the following actions to overload an operator using a friend function:

  • Create the class in which the Operator should be overloaded.
  • Declare the operator function inside the class as a friend. The friend function can now access the class's private members.
  • Implement the friend function and provide the Operator's behavior.
  • With class objects, treat the overloaded Operator as if it were a built-in operator.


Let's use several operators and classes as examples to demonstrate this procedure.

Example 1: Adding a Friend Function to the '+' Operator:

Let's say you wish to overload the '+' Operator to add two 'Complex' objects, and you have a class called 'Complex' to represent complex numbers. Here is how to go about it:

A 'Complex' class has been built to represent complex integers with real and imaginary components in this illustration. The '+' Operator is implemented as a non-member function outside the class and declared a friend function inside the class. This friend feature has access to 'Complex's' private members.

We construct two Complex objects, 'c1' and 'c2', then combine them using the overloaded '+' Operator in the main function. The 'display' member function saves the outcome in the 'sum' variable.

Example 2: Overloading the '<<' Operator with a Friend Function:

Overloading the "<<"Operator to produce unique output for objects of a class is another frequent application of buddy functions. This is frequently done to make it simple to print objects using 'std::cout'. Here's an illustration:

In this illustration, a 'Student' class has been created to represent student data that includes a name and an age. The "<<"Operator was implemented as a non-member function outside the class and declared a friend function inside the class. This buddy function alters the output format for Student objects using a 'std::ostream' object ('os') and a 'const Student&'.

The two Student objects, s1, and s2 are created in the main method, and their data is then printed using the overloaded "<<" operator and 'std::cout'.

Example 3: Adding a Friend Function to the '[]' Operator:

Another intriguing example is the '[]' Operator being overloaded to allow array-like access to class members. Let's say you have a Matrix class, and you want users to be able to access matrix elements by utilizing the '[]' Operator. How to do it is as follows:

In this illustration, a 'Matrix' class has been defined to represent a matrix with a predetermined number of rows and columns. The '[]' Operator is implemented as a non-member function outside the class and declared a friend function inside the class. Thanks to this companion function, we can use array-like indexing to access matrix elements.

Using the overloaded '[]' Operator, we construct a Matrix object 'mat' in the 'main' method, fill it with data, and then use the display member function to 'display' the matrix.

Best Practices and Important Considerations

Operator overloading with friend functions is a potent weapon, but it should be used cautiously. The following are some key factors to bear in mind and best practices to follow:

  • Select Meaningful Operators Operator overloads: Choose a strategy to overload operators that makes sense for your class. Be careful not to overload operators in a way that can cause confusion or unexpected behaviour.
  • Maintain Symmetry: Make sure the behaviour is symmetric while overloading binary operators. Make sure it works for both 'a + b' and 'b + a', for instance, if you overload the '+' Operator to add objects of your class.
  • Avoid Using Too Many Operators in One Class: Using too many operators in one class can make the code confusing and difficult to understand. Keep your overloading to the operators that are most pertinent to the usage of your class.
  • Prefer Member Functions: When you have access to the class definition, overload operators using member functions instead of other methods. Use friend functions to overload operators for built-in or external types.
  • Use a buddy. Use friend functions sparingly: Only when necessary. Avoid them if you can accomplish the same task with member or non-friend functions because they compromise encapsulation.
  • Document Your Overloads: When overloading operators, clearly document how they act differently from the built-in operators. This clarifies the behavior of your class to other developers.
  • Gracefully Handle Potential Mistakes: Ensure that your Operator overloads gracefully handle any potential mistakes or unusual instances. For instance, look for array indices outside the range or division by zero.
  • Testing and Validation: To ensure your overloaded operators behave as expected, thoroughly test them using a range of test cases. This is particularly crucial for sophisticated or unique operators.
  • Operator Precedence: When you overload operators, the operator precedence rules remain unchanged. Ensure that the built-in operators your overloaded operators emulate have the same priority.
  • Operator Overloading for Efficiency: Consider the effects of Operator overloading on performance. Providing specialized member functions for particular activities rather than overloading operators may be more effective in some circumstances.
  • Consistency: If you overload an operator for a class, make sure that it behaves in a way compatible with the other operations and methods specified for the class.
  • Avoid Overloading the '&&', '||', and ',' Operators: It's generally advised to avoid overloading the logical AND ('&&'), logical OR ('||'), and comma (',') operators since they have unpredictable short-circuit behaviour.

You can effectively use Operator overloading to increase the usability and expressiveness of your C++ classes by adhering to these guidelines and recommended practices.


Operator overloading with buddy functions is a cornerstone for creating unique and understandable classes in C++ programming. This robust feature dramatically improves the readability and expressiveness of code by enabling developers to modify the behaviour of operators for user-defined data types. In this

thorough investigation of Operator overloading with buddy functions, we have uncovered its complexities and illustrated its uses with concrete examples.

In essence, operator overloading allows you to change the meaning of operators like "+," "-," "*," and "/" when they are used with instances of your custom classes. By enabling your objects to mimic the behaviour of built-in data types, you may improve the readability and flow of your code. Consider the '+' Operator for adding two vector objects or the '==' Operator for comparing two complex integers. These procedures replicate the behaviour of their built-in counterparts and become simple and intuitive.

Although the adaptability of buddy functions might be empowering, utilize them carefully and sparingly because they can disrupt encapsulation to some extent. When accessing the class definition, use member functions instead of friend functions. In situations where you must interact with private members of external or built-in types, use friend functions.

It is essential to test and validate your overworked operators thoroughly. This guarantees that edge cases are gracefully handled and that your custom behaviours conform to your expectations. Consider how operator overloading affects performance, especially for complicated or frequently performed procedures. Sometimes, using specialized member functions is a better option.

The cornerstone of C++ programming, Operator overloading with friend functions, enables programmers to design clear and expressive classes. You may use Operator overloading to create code that is not only functional but also elegant and maintainable by adhering to best practices and the guidelines presented in this article. Remember that Operator overloading with buddy functions is a powerful tool in your armory that can simplify difficult tasks and make your code more understandable and expressive as you continue your C++ programming adventure.

Next TopicOstream in C++

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA