# Operator Function as Member function and/or Friend Function

0 like 0 dislike
421 views

Operator Function as Member function and/or Friend Function

### For Indian Students- INR 360/- || For International Students- \$9.99/-

S.No.

Course Name

Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence || Labeled as Highest Rated Course by Udemy

Apply Coupon

2.

Complete Machine Learning & Data Science with Python| ML A-Z Apply Coupon

3.

Complete Python Programming from scratch | Python Projects Apply Coupon

0 like 0 dislike
by Goeduhub's Expert (7.1k points)
edited

Operator Function as Friend Function

An operator overloading function can be a member of class or a friend of class. In case of member function it takes only one argument at time of calling but in case of friend function it takes two arguments as both the objects are passed as argument.

Example

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Check
{
private:
float x,y;
public:
void setData(int a,int b)
{
x=a; y=b;
}
void getData()
{
cout<<"\nx:"<<x<<" y:"<<y;
}

Check operator -(Check c)
{
Check temp;
temp.x=x-c.x;
temp.y=y-c.y;
return temp;
}

Check friend operator +(Check,Check);
};

//Defining a friend function to overload operator +
Check operator +(Check c1,Check c2)
{
Check temp;
temp.x=c1.x+c2.x;
temp.y=c1.y+c2.y;
return temp;
}
void main()
{
clrscr();
Check c1,c2,c3;
c1.setData(10,20);
c2.setData(20,30);
c3=c1+c2;
c3.getData();
Check c4=c2-c1;
c4.getData();
getch();
}

Output

x:30 y:50
x:10 y:10