0% found this document useful (0 votes)
236 views

Unary Minus Operator Overloading in C++

The document discusses overloading the unary minus operator in C++. It explains that unary operators operate on a single operand with user-defined data types. As an example, it focuses on overloading the unary minus operator. It provides sample C++ code that defines a class with integer data members, overloads the unary minus operator to negate those values, creates an object of the class, applies the overloaded operator to the object, and displays the results. The sample code demonstrates how to overload the unary minus operator to function on user-defined types by negating data members.

Uploaded by

Tarun Saini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
236 views

Unary Minus Operator Overloading in C++

The document discusses overloading the unary minus operator in C++. It explains that unary operators operate on a single operand with user-defined data types. As an example, it focuses on overloading the unary minus operator. It provides sample C++ code that defines a class with integer data members, overloads the unary minus operator to negate those values, creates an object of the class, applies the overloaded operator to the object, and displays the results. The sample code demonstrates how to overload the unary minus operator to function on user-defined types by negating data members.

Uploaded by

Tarun Saini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like