Sunteți pe pagina 1din 3

11/2/2014

Program for unary minus operator overloading in c++ | mindfreakerstuffmindfreakerstuff

Home > Tutorials > C++ programs > Program for unary minus operator overloading in c++

Program for unary minus operator


overloading in c++
By Prathamesh Posted on July 22, 2013

Reply

Program for unary minus operator overloading in


c++
Unary Operator operates on single operand with the user defined data types.
Examples of Unary Operator are:
1. Unary minus (-) operator.
2. Increment (++) and Decrement () operator.
3. Logical not (!) operator.
A unary operator are a nonstatic member function has no parameters, or a
nonmember function has one parameter.
This unary minus (-) operator appears on left side of object.
In this operand becomes call and hence no arguments are required.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19

#include<iostream.h>
#include<conio.h>
class UnaryOp
{
public:
int x,y,z;
UnaryOp()
{
x=0;
y=0;
z=0;
}
UnaryOp(int a,int b,int c)
{
x=a;
y=b;

http://www.mindfreakerstuff.com/2013/07/program-for-unary-operator-overloading-in-c/

1/3

11/2/2014

Program for unary minus operator overloading in c++ | mindfreakerstuffmindfreakerstuff

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

z=c;

void display()
{
cout<<"\n\n\t"<<x<<"
}

};

"<<y<<"

"<<z;

// Overloaded minus (-) operator


UnaryOp operator- ()
{
x= -x;
y= -y;
z= -z;
}

int main()
{
clrscr();
UnaryOp u1(10,-40,70);
cout<<"\n\nNumbers are :::\n";
u1.display();
-u1;

// call unary minus operator function

cout<<"\n\nNumbers are after applying overloaded minus (-) operator :::\n"

u1.display();
getch();

// display u1

OUTPUT

Program for unary minus operator overloading in c++

Y OU MA Y A L S O L IK E -

http://www.mindfreakerstuff.com/2013/07/program-for-unary-operator-overloading-in-c/

2/3

11/2/2014

Program for unary minus operator overloading in c++ | mindfreakerstuffmindfreakerstuff

Conditional
comments for IE
6,7,8,9

Program for to find


largest of three
numbers in c++

Program for check


whether number is
prime or not in c++

Program for
constructor
overloading in c++

Tweet

Program for unary


decrement operator
overloading in c++

TAGS : B.SC.IT , BCA , C++


THIS ENTRY WAS POSTED IN C++ PROGRAMS |
2,399 VIEWS

http://www.mindfreakerstuff.com/2013/07/program-for-unary-operator-overloading-in-c/

3/3

S-ar putea să vă placă și