CPP Program to Implement the concept of Unary Operator Overloading

Aim

To implement the concept of unary operator overloading by creating a C++ program.

Description:

  • C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.
  • An overloaded declaration is a declaration that is declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation).
  • When you call an overloaded function or operator, the compiler determines the most appropriate definition to use, by comparing the argument types you have used to call the function or operator with the parameter types specified in the definitions.
  • The process of selecting the most appropriate overloaded function or operator is called overload resolution.

Operators Overloading in C++

  • You can redefine or overload most of the built-in operators available in C++.
  • Thus, a programmer can use operators with user-defined types as well.
  • Overloaded operators are functions with special names: the keyword “operator” followed by the symbol for the operator being defined.
  • Like any other function, an overloaded operator has a return type and a parameter list.
Box operator+(const Box&);
  • declares the addition operator that can be used to add two Box objects and returns the final Box object.
  • Most overloaded operators may be defined as ordinary non-member functions or as class member functions.
  • In case we define the above function as a non-member function of a class then we would have to pass two arguments for each operand as follows −
Box operator+(const Box&, const Box&);
  • Following is the example to show the concept of operator over loading using a member function.
  • Here an object is passed as an argument whose properties will be accessed using this object, the object which will call this operator can be accessed using this operator

The unary operators operate on a single operand and the following are the examples of Unary operators −

  1. The increment (++) and decrement (–) operators.
  2. The unary minus (-) operator.
  3. The logical not (!) operator.

The unary operators operate on the object for which they were called and normally, this operator appears on the left side of the object, as in !obj, -obj, and ++obj but sometime they can be used as postfix as well like obj++ or obj–.

Algorithm:

  1. Start the Program
  2. Create a class named space and declare necessary data members and member functions and operator function as a member function
  3. The operator unary minus is overloaded to perform the operation of changing sign
  4. Define member function getdata (), to get three values that are passed as arguments.
  5. The operator function is defined where the sign of values is changed.
  6. Function display () is defined where the values are displayed before and after sign change.
  7. Stop the program.

Program:

#include<iostream>
using namespace std;
class SPACE
{
private:
int a;
public:
void getdata(int);
SPACE operator -();
void disp();
};   
void SPACE::getdata(int d)
{
a=d;
}
SPACE SPACE::operator -()
{
a=-a;
}
void SPACE::disp()
{
cout<<a<<endl;
}
int main()
{
SPACE a,b,c;
int x,v,z;
cout<<"ENTER THE VALUE OF A :";cin>>x;cout<<x<<endl;
a.getdata(x);
cout<<"ENTER THE VALUE OF B :";cin>>v;cout<<v<<endl;
b.getdata(v);
cout<<"ENTER THE VALUE OF C :";cin>>z;cout<<z<<endl;
c.getdata(z);
a.operator -();
b.operator -();
c.operator -();
cout<<"VALUE OF A :";
a.disp();
cout<<"VALUE OF B :";
b.disp();
cout<<"VALUE OF C :";
c.disp();
return 0;
}

Execution:

INPUT :

ENTER THE VALUE OF A: 5
ENTER THE VALUE OF B: 7
ENTER THE VALUE OF C: 9

OUTPUT :

VALUE OF A: - 5
VALUE OF B: - 7
VALUE OF C: - 9

Result:

Thus, the Implement the concept of Unary Operator Overloading was executed successfully.

Leave a Comment