Answer to Question #236291 in C++ for vishal

Question #236291

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


1
Expert's answer
2021-09-12T18:56:32-0400
#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();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS