Write a program to overload unary (-) operator.
SOLUTION FOR ABOVE QUESTION
#include <iostream>
using namespace std;
#include<conio.h>
class complex {
//declarare three variables
int variable_1, variable_2, c;
public:
complex() {
}
//A function to get value from the user
void getvalue() {
cout << "Enter the Two Numbers:";
cin >> variable_1>>variable_2;
}
void operator++() {
variable_1 = ++variable_1;
variable_2 = ++variable_2;
}
void operator--() {
variable_1 = --variable_1;
variable_2 = --variable_2;
}
//Definition of a method for dispaly/ show
void display() {
cout << variable_1 << "+\t" << variable_2 << "i" << endl;
}
};
//definition of the main method
void main() {
clrscr();
complex my_object;
my_object.getvalue();
my_object++;
cout << "Increment Complex Number\n";
my_object.display();
my_object--;
cout << "Decrement Complex Number\n";
my_object.display();
getch();
return 0;
}
Comments
Leave a comment