Replies
Welcome, guest
Join CrazyEngineers to reply, ask questions, and participate in conversations.
CrazyEngineers powered by Jatra Community Platform
-
@thebigk • Aug 25, 2009
Yep, what about it?namivrmoperator overloading in C++ using friend function -
@itchap-RKJQDB • Aug 25, 2009
// Using friend functions to
I think you are asking about operator overloading program in C++ using friend function.
// 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();
}
From now onwards please give the exact details about your query. -
@namivrm-w4iQGm • Aug 26, 2009
thanks... cn u hlp me in opereator overloading usng member function... -
@itchap-RKJQDB • Aug 26, 2009
Yeah I can help you but first of all stop using this sms text in your posts anywhere on CE.namivrmthanks... cn u hlp me in opereator overloading usng member function...
Now tell me what do you want to know about operator overloading using member function..
-
@itchap-RKJQDB • Aug 26, 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
Now let’s overload the same operator using a member function instead:
{
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);
}
class Cents
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.
{
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);
}
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-hWHU1M • Aug 26, 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-RKJQDB • Aug 26, 2009
Ok i will take care of that.😎English-ScaredItchap 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!!! -
@namivrm-w4iQGm • Aug 27, 2009
hey... i'll nt use dis code in my assignments.... i had doubt... thanks for ur help.... -
@thebigk • Aug 27, 2009
Please refrain from using SMS text in your posts on CrazyEngineers.namivrmhey... i'll nt use dis code in my assignments.... i had doubt... thanks for ur help....