Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,398 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,230 people online right now. Registration is fast and FREE... Join Now!




operator overloadind

 
Reply to this topicStart new topic

operator overloadind, whether the given program is operator overloaded

balaji_eee08
post 29 Aug, 2008 - 10:55 AM
Post #1


New D.I.C Head

*
Joined: 29 Aug, 2008
Posts: 1

[font=Garamond][size=4] hi
can u clarify whether this one is overloaded or not
class op
{
int a;
public:
void operator-()
{
a=-a;
}
};
void main()
{
op a;
-a;
}
my doudt is : operator - has to operate only upon data member a, so it acts on one operand. so i think it is not overloaded.
can u clarify me.
User is offlineProfile CardPM

Go to the top of the page

sensui
post 29 Aug, 2008 - 10:16 PM
Post #2


D.I.C Head

Group Icon
Joined: 24 Aug, 2008
Posts: 132



Thanked 20 times

Dream Kudos: 50
My Contributions


Of course it is overloaded. If you try this code ( I added the methods set() and get() to clarify you ):
cpp
#include <iostream>
class op
{
private:
int a;
public:
void set( int x )
{
a = x;
};
int get()
{
return a;
};
void operator-()
{
a=-a;
};
};
int main()
{
op a;
a.set( 2 );
std::cout << a.get() << " ";
-a;
std::cout << a.get() << std::endl;
return EXIT_SUCCESS;
}


It will output:
CODE
2 -2
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 07:28AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month