Knowee
Questions
Features
Study Tools

Create a C++ programme that accomplishes all arithmetic operations on two complex objects, including addition, subtraction, multiplication, and division. (X + i * Y) represents the Complex Object (Where X and Y are two real numbers and 'i' is an Imaginary). As a programmer, create a class called "COMPLEX" containing the data members X and Y and also getData() and putData() methods to pefrom read and write for the above said Data Members. Define a separate functions for the operations such as Addition, Subtraction, Multiplication, and Division.Input :Read the Two Complex Objects (Each with X and Y)Output :Addition of Two Complex ObjectsSubtraction of Two Complex ObjectsMultiplication of Two Complex ObjectsDivision of Two Complex ObjectsNote :Both X and Y are Non-Negative, otherwise print "Invalid"Define a separate functions with return type and with arguments of two complext objects.

Question

Create a C++ programme that accomplishes all arithmetic operations on two complex objects, including addition, subtraction, multiplication, and division. (X + i * Y) represents the Complex Object (Where X and Y are two real numbers and 'i' is an Imaginary). As a programmer, create a class called "COMPLEX" containing the data members X and Y and also getData() and putData() methods to pefrom read and write for the above said Data Members. Define a separate functions for the operations such as Addition, Subtraction, Multiplication, and Division.Input :Read the Two Complex Objects (Each with X and Y)Output :Addition of Two Complex ObjectsSubtraction of Two Complex ObjectsMultiplication of Two Complex ObjectsDivision of Two Complex ObjectsNote :Both X and Y are Non-Negative, otherwise print "Invalid"Define a separate functions with return type and with arguments of two complext objects.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a simple C++ program that accomplishes all arithmetic operations on two complex objects:

#include<iostream>
using namespace std;

class COMPLEX {
    float X, Y;
public:
    void getData() {
        cout << "Enter real and imaginary parts respectively: ";
        cin >> X >> Y;
        if(X < 0 || Y < 0) {
            cout << "Invalid" << endl;
            exit(0);
        }
    }
    void putData() {
        cout << "Complex number: " << X << " + i" << Y << endl;
    }
    COMPLEX operator + (COMPLEX);
    COMPLEX operator - (COMPLEX);
    COMPLEX operator * (COMPLEX);
    COMPLEX operator / (COMPLEX);
};

COMPLEX COMPLEX::operator + (COMPLEX c2) {
    COMPLEX temp;
    temp.X = X + c2.X;
    temp.Y = Y + c2.Y;
    return temp;
}

COMPLEX COMPLEX::operator - (COMPLEX c2) {
    COMPLEX temp;
    temp.X = X - c2.X;
    temp.Y = Y - c2.Y;
    return temp;
}

COMPLEX COMPLEX::operator * (COMPLEX c2) {
    COMPLEX temp;
    temp.X = X * c2.X - Y * c2.Y;
    temp.Y = Y * c2.X + X * c2.Y;
    return temp;
}

COMPLEX COMPLEX::operator / (COMPLEX c2) {
    COMPLEX temp;
    float denom = c2.X * c2.X + c2.Y * c2.Y;
    temp.X = (X * c2.X + Y * c2.Y) / denom;
    temp.Y = (Y * c2.X - X * c2.Y) / denom;
    return temp;
}

int main() {
    COMPLEX c1, c2, c3;
    c1.getData();
    c2.getData();
    c3 = c1 + c2;
    cout << "Addition of two complex numbers: ";
    c3.putData();
    c3 = c1 - c2;
    cout << "Subtraction of two complex numbers: ";
    c3.putData();
    c3 = c1 * c2;
    cout << "Multiplication of two complex numbers: ";
    c3.putData();
    c3 = c1 / c2;
    cout << "Division of two complex numbers: ";
    c3.putData();
    return 0;
}

This program defines a class COMPLEX with two data members X and Y representing the real and imaginary parts of a complex number. The getData() method is used to read the complex number from the user and the putData() method is used to display the complex number. The arithmetic operations are performed using operator overloading. The main() function creates two complex numbers, performs the arithmetic operations, and displays the results.

This problem has been solved

Similar Questions

Write a program that does basic arithmetic operations (addition, subtraction, multiplication,and division). The inputs to the program are two numbers (in double format) and theoperation required. Provide a function for each operation and the identifiers for addition,subtraction, multiplication, and division are ‘+’, ‘-‘, ‘*’, and ‘/’ respectively.

operations on complex numbers Write a program to find out the addition and multiplication of two complex numbers using structures and functions. Note: Write the code in the functions readC(), display(), add() and product().

Create a C++ program to calculate the subtraction of two complex numbers by overloading the  '-' binary operator.Complex numbers are the numbers that are expressed in the form of a+ib where, a,b are real numbers and 'i' is an imaginary number called “iota”. The value of i = (√-1). For example, 10+3i is a complex number, where 10 is a real number and 3i is an imaginary number .Sample Input// Read the real and imaginary parts of the first complex number59// Read the real and imaginary parts of the second complex number37Sample OutputNumber1:5+9iNumber2:3+7iAnswer is 2-2i

Program that performs arithmetic operations with values of type char.

Create a Complex class to represent complex numbers. Overload the addition (+), subtraction (-), multiplication (*), and division (/) operators for complex numbers. The prompt ask 2 complex number from the user and display the respective output for the user input.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.