C++ unary operator

C++ Overloading Unary Operators

Let us consider unary minus operator. A minus operator used when as a unary, takes just only one operand. We know that this operator changes the sign of an operand when applied to a basic one operand.

We will see here how to overload this operator so that it can be applied to an object in much the same way as is applied to an int or float variable. The minus unary when applied to an object when should change the sign of each of its data items.

Overloading UNARY MINUS
#include<iostream>
using namespace std;
class space
{
int x;
int y;
int z;
public: void getdata(int a,int b,int c);
void display();
void operator~(); // overload unary minus
};
void space:: getdata(int a,int b, int c)
{
x=a;
y=b;
z=c;
}
void space:: display()
{
std::cout<< x <<" ";
std::cout<< y <<" ";
std::cout<< z <<"\n";
}
void space :: operator~()
{
x=-x;
y=-y;
z=-z;
}
int main()
{
space s;
s.getdata(10,-20,30);
std::cout<<"S :";
s.display();
return 0;
}

Output:
S: 10 -20 30
S: -10 20 -30

Note: This function operator~() takes no argument. Then what does this operator function do?
It changes the sign of data members of the object S. since this function is a member function of the same class, it can directly access the members of the object which activated it.

Remember, a statement like

s2=-s1;

Will not work because, the function operator-() does not return any value.it can work if the function is modified to return an object.

it is possible to overload a unary minus operator using a friend function as follows:

Friend void operator-(space &s); // declaration
void operator-(space &s) // definition

{ s.x=-s.x;
s.y=-s.y;
s.z=-s.z;
}

Note: That the argument is passed by reference,it will not work if we pass argument by value because only a copy of the object that activated the call is passed to operator-() Therefore, the changes made inside the operator function will not reflect in the called object.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise