Simple Program for Unary Operator Overloading Using C++ Programming
Definition
To write a program to find the complex numbers using unary operator overloading.
Unary operators:
- Increment (++) Unary operator.
- Decrement (--) Unary operator.
- The minus (-) unary.
- The logical not (!) operator.
Unary Operator Overloading Algorithm/Steps:
- Step 1: Start the program.
- Step 2: Declare the class.
- Step 3: Declare the variables and its member function.
- Step 4: Using the function getvalue() to get the two numbers.
- Step 5: Define the function operator ++ to increment the values
- Step 6: Define the function operator - -to decrement the values.
- Step 7: Define the display function.
- Step 8: Declare the class object.
- Step 9: Call the function getvalue()
- Step 10: Call the function operator ++() by incrementing the class object and call the function display.
- Step 11: Call the function operator - -() by decrementing the class object and call the function display.
- Step 12: Stop the program.
Simple Program for Unary Operator Overloading Program
#include<iostream.h>
#include<conio.h>
class complex {
int a, b, c;
public:
complex() {
}
void getvalue() {
cout << "Enter the Two Numbers:";
cin >> a>>b;
}
void operator++() {
a = ++a;
b = ++b;
}
void operator--() {
a = --a;
b = --b;
}
void display() {
cout << a << "+\t" << b << "i" << endl;
}
};
void main() {
clrscr();
complex obj;
obj.getvalue();
obj++;
cout << "Increment Complex Number\n";
obj.display();
obj--;
cout << "Decrement Complex Number\n";
obj.display();
getch();
}
Sample Output
Enter the two numbers: 3 6
Increment Complex Number
4 + 7i
Decrement Complex Number
3 + 6i
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming