Question

Given two complex numbers, find the sum of the complex numbers using operator overloading. Write an...

Given two complex numbers, find the sum of the complex numbers using operator overloading.

Write an operator overloading function

    ProblemSolution operator + (ProblemSolution const &P)

which adds two ProblemSolution objects and returns a new ProblemSolution object.

Input

    12 -10
     -34 38

    where,

  • Each row is a complex number.
  • First element is real part and the second element is imaginary part of a complex number.

Output

    -22 28

Two complex numbers are 12-10i and -34+38i.

Sum of complex numbers are = -22+28i.

Assume that,

  • Real and imaginary numbers are integers within the range [-2,000,000,000 to 2,000,000,000]

#include<iostream>
using namespace std;

class ProblemSolution {
private:
int real, imag;
public:

ProblemSolution(int r = 0, int i =0) {
real = r; imag = i;
}

void displayResult(){
cout<<real<<" "<<imag;
}

   //write your code here

};

int main()
{   
int real,imag;
cin>>real>>imag;
ProblemSolution problemSolution1(real, imag);
cin>>real>>imag;
ProblemSolution problemSolution2(real,imag);
ProblemSolution problemSolution3 = problemSolution1 + problemSolution2;
problemSolution3.displayResult();
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//ProblemSolution.cpp

#include<iostream>
using namespace std;

class ProblemSolution
{
   private:
       int real, imag;
   public:

   ProblemSolution(int r = 0, int i =0)
   {
       real = r; imag = i;
   }
   void displayResult()
   {
      
       cout<<real;
       //if imaginary part is positive
       //display + symbol then imaginary part.
       if(imag >= 0)
       {
           cout<<"+";
           cout<<imag<<"i";
       }
       //if negative display negative
       //and convert the imag part to positive using - operator
       else
       {
           cout<<"-";
           cout<<-imag<<"i";
       }
      
   }
   //overloaded operator +
   ProblemSolution operator + (ProblemSolution const &P)
   {
       //create new object of ProblemSolution with
       //addition real part of calling object and passed real value as new real part of the result.
       //same occurs for imaginary part.
      
       ProblemSolution res(real + P.real, imag+ P.imag);
       //return the result
       return res;
   }
};

int main()
{   
   int real,imag;
   cin>>real>>imag;
   ProblemSolution problemSolution1(real, imag);
   cin>>real>>imag;
   ProblemSolution problemSolution2(real,imag);
   ProblemSolution problemSolution3 = problemSolution1 + problemSolution2;
   cout<<"Two Complex Numbers are ";
   problemSolution1.displayResult();
   cout<<" and ";
   problemSolution2.displayResult();
   cout<<endl;
   cout<<"Sum of Complex Numbers are = ";
   problemSolution3.displayResult();
}

Add a comment
Know the answer?
Add Answer to:
Given two complex numbers, find the sum of the complex numbers using operator overloading. Write an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (1) Declaration and implementation of complex class, overloading operator +, -, != , /, --(prefix), --(postfix)...

    (1) Declaration and implementation of complex class, overloading operator +, -, != , /, --(prefix), --(postfix) as friend functions.         (2)The program should be in Separating Class Specification, Implementation(complex.h,complex.cpp), and Client Code(Task2.cpp).             (3) Run the program and capture screenshots of output. Declaration and implementation of complex class, overloading operator +, -, != , /=, --(prefix), --(postfix) as friend function */ #include "Complex.h" #include <iostream> using namespace std; int main(){       Complex c1(5, 4), c2(2, 10), c3;       cout <<...

  • This is a C++ programming question. Please provide the correct, workable code. Use the following three...

    This is a C++ programming question. Please provide the correct, workable code. Use the following three programs to help solve the problem. Provide comments throughout the code. Problem to solve: Code 1: Complex.h #pragma once #ifndef COMPLEX_H #define COMPLEX_H class Complex { private:    double real;    double imag; public:    // initialize the complex number to 0.0    Complex() : real(0.0), imag(0.0) {}    // initialize the complex number at declaration or new    Complex(double r, double i) :...

  • IN C++ Given ProblemSolution class. Use a pointer to object of “ProblemSolution” class to invoke the...

    IN C++ Given ProblemSolution class. Use a pointer to object of “ProblemSolution” class to invoke the “CalculateSum” and “Print” methods of “ProblemSolution”. Write a function:    void Solution(int N1, int N2) that accepts two integers N1 and N2. The function should instantiate “ProblemSolution” object with N1 and N2 values and call “CalculateSum” and “Print” method by creating a pointer to the object of “ProblemSolution”. Input    12 5 Output    17 Assume that, N1, N2 and sum are integers within...

  • 61. The following program is accepted by the compiler:         int sum( int x, int y...

    61. The following program is accepted by the compiler:         int sum( int x, int y )         {             int result;             result = x + y;            }                            T__   F__ 62. The following implementation is accepted by the compiler:         void product()         {             int a; int b; int c; int result;             cout << "Enter three integers: ";             cin >> a >> b >> c;             result = a * b * c;            ...

  • Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...

    Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form: realPart + imaginaryPart * i where i is √-1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...

  • EXPLAIN HOW THIS PROGRAM WORKS, USING DEV C++ #include <iostream> #include <cmath> #define PI 3.1415926535897 using...

    EXPLAIN HOW THIS PROGRAM WORKS, USING DEV C++ #include <iostream> #include <cmath> #define PI 3.1415926535897 using namespace std; typedef struct ComplexRectStruct {    double real;    double imaginary; } ComplexRect; typedef struct ComplexPolarStruct {    double magnitude;    double angle; } ComplexPolar; double radToDegree (double rad) {    return (180.00 * rad) / PI; } double degToRadian (double deg) {    return (deg * PI) / 180; } ComplexPolar toPolar (ComplexRect r) {    ComplexPolar p;    p.magnitude = round(sqrt(pow(r.real,...

  • CC++ Compiler: CPP (gcc-5.x) 0 Simple Calculator Given two integers and a string, create a simple...

    CC++ Compiler: CPP (gcc-5.x) 0 Simple Calculator Given two integers and a string, create a simple calculator which performs the following operations: • Addition if the string is "+" • Difference if the string is "-" Multiplication if the string is "*" • Return quotient (obtained by dividing the first number by the second) if the string is "/" • Return greater value if the string is "max" • Return lesser value if the string is "min" solution.cpp 2 Create...

  • C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a...

    C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + imaginaryPart * i where i is Squareroot -1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when ifs declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that...

  • C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the r...

    C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the real part of the complex number and   is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT