23 Nov 2019

  • November 23, 2019
  • Amitraj
* Overloading unary operators:-
   
 A minus operator when used as a unary, takes just one operand . we knows that this operator changes the sign of an operand when applied to a basic item.

we can overload this unary minus operator so that it can be applied to an object in the same way as is applied to an int or float variable.


// C++ program to overload unary minus operator.


#include<iostream>
using namespace std;
class space
{
int x,y,z;
public:
 void getdata(int a,int b,int c)
 {
  x=a;
  y=b;
  z=c;
 }
 void display()
 {
  cout<<x<<" "<<y<<" "<<z<<"\n";
 }
 void operator-()
 {
  x=-x;
  y=-y;
  z=-z;
 }
};
int main()
{
space s;
s.getdata(10,-23,40);
s.display();
    -s;      // Operator - function call
s.display();
return 0;
}

OUTPUT:

10 -23 40

-10 23 -40


Related Posts:

  • Operators in C and C++ 3.) Relational - Operator:- -> It is used to compare two numbers by checking whether they are equal or not, less than, less than or equal to, greater than, greater than or equal to. -> This type of operator are use t… Read More
  • Type cast Operator in C++ Type cast Operator:   C++ permitts Explicit type conversion of variables or expressions using the type cast operator.      (type name)  expression     // C notation     &… Read More
  • Implicit Conversions in C++ Implicit Conversions:-  we can mix data types in expressions.  for example:                              m= 5+2.75; is a valid statement… Read More
  • Operators in C and C++ 4.) Logical - Operator:-  -> It refers to the boolean values which can be expressed as:    1. Binary logical operations, which involves two variables: AND and OR    2. Unary logical operati… Read More
  • Operators in C and C++ 5.)  Bitwise - Operators:- -> It is based on the principle of performing operations bit by bit which is based on boolean algebra. It increases the processing speed and hence the efficiency of the program. -> Bit… Read More

Translate

Popular Posts