Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
421 views
in JECRC University B.Tech(CSE-III Sem) Object Oriented Programming Lab by Goeduhub's Expert (7.1k points)

Operator Function as Member function and/or Friend Function

Goeduhub's Top Online Courses @Udemy

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
    More Courses

1 Answer

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

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;
}

//Operator overloading function as class member
Check operator -(Check c)
{
Check temp;
temp.x=x-c.x;
temp.y=y-c.y;
return temp;
}

//Declaring a friend function for operator overloading
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


For more Object Oriented Programming Lab Experiments Click Here

3.3k questions

7.1k answers

394 comments

4.6k users

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...