CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@namivrm • 25 Aug, 2009
Hello, this is Nami. I am a student of computer science. While learning C++, I faced a query about operator overloading using friend function. I am pretty clueless about how to code that. Please help me out!
@Kaustubh Katdare • 25 Aug, 2009
namivrm
operator overloading in C++ using friend function
Yep, what about it?
@itchap • 25 Aug, 2009
// Using friend functions to
// overload addition and subtarction
// operators
#include <iostream.h>

class myclass
{
int a;
int b;

public:
myclass(){}
myclass(int x,int y){a=x;b=y;}
void show()
{
cout<<a<<endl<<b<<endl;
}

// these are friend operator functions
// NOTE: Both the operans will be be
// passed explicitely.
// operand to the left of the operator
// will be passed as the first argument
// and operand to the right as the second
// argument
friend myclass operator+(myclass,myclass);
friend myclass operator-(myclass,myclass);

};

myclass operator+(myclass ob1,myclass ob2)
{
myclass temp;

temp.a = ob1.a + ob2.a;
temp.b = ob1.b + ob2.b;

return temp;
}

myclass operator-(myclass ob1,myclass ob2)
{
myclass temp;

temp.a = ob1.a - ob2.a;
temp.b = ob1.b - ob2.b;

return temp;
}

void main()
{
myclass a(10,20);
myclass b(100,200);

a=a+b;

a.show();
}
I think you are asking about operator overloading program in C++ using friend function.
From now onwards please give the exact details about your query.
@namivrm • 26 Aug, 2009 thanks... cn u hlp me in opereator overloading usng member function...
@itchap • 26 Aug, 2009
namivrm
thanks... cn u hlp me in opereator overloading usng member function...
Yeah I can help you but first of all stop using this sms text in your posts anywhere on CE.
Now tell me what do you want to know about operator overloading using member function..
@itchap • 26 Aug, 2009 Overloading operators using a member function is very similar to overloading operators using a friend function. When overloading an operator using a member function:
The leftmost operand of the overloaded operator must be an object of the class type.
The leftmost operand becomes the implicit *this parameter. All other operands become function parameters.

Most operators can actually be overloaded either way, however there are a few exception cases:
If the leftmost operand is not a member of the class type, such as when overloading operator+(int, YourClass), or operator<<(ostream&, YourClass), the operator must be overloaded as a friend.
The assignment (=), subscript ([]), call (()), and member selection (->) operators must be overloaded as member functions.

Overloading the unary negative (-) operator

The negative operator is a unary operator that can be implemented using either method. Before i show you how to overload the operator using a member function, here’s a reminder of how to overloaded it using a friend function:

class Cents
{
private:
int m_nCents;

public:
Cents(int nCents) { m_nCents = nCents; }

// Overload -cCents
friend Cents operator-(const Cents &cCents);
};

// note: this function is not a member function!
Cents operator-(const Cents &cCents)
{
return Cents(-cCents.m_nCents);
}
Now let’s overload the same operator using a member function instead:

class Cents
{
private:
int m_nCents;

public:
Cents(int nCents) { m_nCents = nCents; }

// Overload -cCents
Cents operator-();
};

// note: this function is a member function!
Cents Cents:: operator-()
{
return Cents(-m_nCents);
}
Note that this method is very similar. However, the member function version of operator- doesn’t take any parameters.A member function has an implicit *this pointer which always points to the class object the member function is working on. The parameter we had to list explicitly in the friend function version (which doesn’t have a *this pointer) becomes the implicit *this parameter in the member function version.

Remember that when C++ sees the function prototype Cents Cents:😲perator-();, the compiler internally converts this to Cents operator-(const Cents *this), which you will note is almost identical to our friend version Cents operator-(const Cents &cCents)!
@Saandeep Sreerambatla • 26 Aug, 2009 Itchap good programs 😀

A small suggestion dont give all the codes let people try themselves.

Mr.namivrm I hope you are not using these codes for your assignments!!!
@itchap • 26 Aug, 2009
English-Scared
Itchap good programs 😀

A small suggestion dont give all the codes let people try themselves.

Mr.namivrm I hope you are not using these codes for your assignments!!!
Ok i will take care of that.😎
@namivrm • 27 Aug, 2009 hey... i'll nt use dis code in my assignments.... i had doubt... thanks for ur help....
@Kaustubh Katdare • 27 Aug, 2009
namivrm
hey... i'll nt use dis code in my assignments.... i had doubt... thanks for ur help....
Please refrain from using SMS text in your posts on CrazyEngineers.
9.5k views

Related Posts

@fisfiseani · Aug 18, 2012

Dear All, I have one Assembly Language programme which i want to load in micro-controller IC(89C51),what is the procedure for that?
5.8k views

@demetron · Apr 7, 2009

hi after engineering i thought to go for CDAC(may be acts pune) but is there placement is good after that economic recession.and any body knows what is highest payed salary...
15.9k views

@logiclover · Jan 17, 2008

My pen drive was affected by fun.exe virus. I started my XP in Safe mode and scanned my pen drive through AVG. It deleted all the infected files. Now I...
9.3k views

@Kaustubh Katdare · Aug 23, 2014

Xiaomi, the company known as 'Apple of China' will introduce its entry level smartphones - Redmi 1S and Redmi Note in India on August 26. The company has sent out...
9.7k views

@syed.saifuddin · Oct 22, 2007

HI DEAR FRDS IAM SAIF IAM PERSUING MY ELECTRICAL AND ELECTRONIC ENGG FROM JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY FROM HYDERABAD. I NEED SOME ABOUT THE PROJECTS THAT CAN BE DONE IN...
4.7k views