Compile Time Polymorphism IV
Develop a program to overload binary operators.
The class Complex has the following public attributes
Data typeVariable nameintrealintimaginary
Include default constructor to initialize real and imaginary values to 0
Include the following functions in the Complex class and overload the binary operators
Member functionFunction descriptionvoid getvalue()This function is used to read .
void display()This function will display the result value
Sample Input and Output :
[All texts in bold represents input and rest represents output statements]
Enter the value of Complex Numbers a,b :4 5
Enter the value of Complex Numbers a,b :7 8
Input Values :
Output Complex number : 4+5i
Output Complex number : 7+8i
Result :
Output Complex number : 11+13i
Output Complex number : -3-3i
#include<iostream>
#include<conio.h>
using namespace std;
class complex {
int a, b;
public:
void getvalue() {
cout << "Enter the value of Complex Numbers a,b:";
cin >> a>>b;
}
complex operator+(complex object) {
complex x;
x.a = a + object.a;
x.b = b + object.b;
return (x);
}
complex operator-(complex object) {
complex x;
x.a = a - object.a;
x.b = b - object.b;
return (x);
}
void display() {
cout << a << "+" << b << "i" << "\n";
}
};
int main() {
complex object1, object2, output, output1;
object1.getvalue();
object2.getvalue();
output = object1 + object2;
output1 = object1 - object2;
cout << "Input Values:\n";
object1.display();
object2.display();
cout << "Result:"<<endl;
output.display();
output1.display();
getch();
}
Comments
Leave a comment