0% found this document useful (0 votes)
18 views

Overloading Through Friend Functions

This document discusses overloading operators using friend functions in C++. It notes that friend functions require two arguments explicitly, unlike member functions. It also lists the rules for overloading operators using friend functions - the symbol must be the same in the class and function, the same operator can be overloaded for different data type argument lists, and certain operators like assignment cannot be overloaded. The example shows defining a complex number class and overloading the + operator as a friend function to add two complex numbers.

Uploaded by

JPR EEE
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Overloading Through Friend Functions

This document discusses overloading operators using friend functions in C++. It notes that friend functions require two arguments explicitly, unlike member functions. It also lists the rules for overloading operators using friend functions - the symbol must be the same in the class and function, the same operator can be overloaded for different data type argument lists, and certain operators like assignment cannot be overloaded. The example shows defining a complex number class and overloading the + operator as a friend function to add two complex numbers.

Uploaded by

JPR EEE
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 2

overloading through friend functions

The difference between operators using member functions and using friend is, friend function requires two argument explicitly.

Rules:
It requires two arguments. The symbol in the class and in the function should be same. Same operator can be overloaded for different data type argument list. Friend function cannot be overloaded with the given operators. Assignment = Function call () Class member class operator -> Subscripting operator []

#include<iostream.h> #include<conio.h> class complex { float x,y; public: void setdata(float r,float i) { x=r; y=i; } void print() { cout<<x<<"+"<<y<<"\n"; } friend complex operator+(complex,complex); }; complex operator+(complex c1,complex c2) { complex temp; temp.x=c1.x+c2.x; temp.y=c1.y+c2.y; return(temp); }

void main() { clrscr(); complex c1,c2,c; c1.setdata(12.4,3.2); c2.setdata(6.5,2.7); c=c1+c2; cout<<"\n the result is:"; c.print(); getch(); }

You might also like