Question

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 tasks:

(a) add – Adds two Complex numbers: The real parts are added together and the imaginary parts are added together.
(b) subtract – Subtracts two Complex numbers. The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.
(c) toString – Returns a string representation of a Complex number in the form (a,b), where a is the real part and b is the imaginary part.

Write a main program to test your class. Your program should prompt the user to enter two Complex numbers. If the user hits enter without entering anything, then the default value of (1,1) used by the default constructor should be used. It should then print out the sum and difference of the two Complex numbers entered. The following is a sample run:

Enter a Complex number with the real and imaginary parts separated by a comma: 4,5
Enter a Complex number with the real and imaginary parts separated by a comma: 3,2

The sum of the two complex numbers is (7,7) and the difference is (1,3).

Another sample run would be:

Enter a complex number with the real and imaginary parts separated by a comma:
Enter a complex number with the real and imaginary parts separated by a comma: 3,2

The sum of the two Complex numbers is (4,3) and the difference is (-2,-1).

This is a C++ program and please comment so I can understand, will leave thumbs up!

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

//Comments are included inline

CODE:

#include <iostream>
#include <string>
#include <sstream>
#include <string>
#include <vector>
#include <tuple>

using namespace std;

class Complex
{
public:
   Complex():m_real(1),m_imaginary(1)
   {
       //Default constructor
      
   }

   Complex(double r,double i):m_real(r),m_imaginary(i){}
   Complex add(Complex c)
   {
       Complex sum;

       sum.m_real = this->m_real + c.m_real;
       sum.m_imaginary = this->m_imaginary + c.m_imaginary;

       return sum;
   }

   Complex subtract(Complex c)
   {
       Complex diff;

       diff.m_real = this->m_real - c.m_real;
       diff.m_imaginary = this->m_imaginary - c.m_imaginary;

       return diff;
   }

   string toString()
   {
       //convert into string the real and imaginary part
       return "(" + std::to_string(int(m_real)) + "," + to_string(int(m_imaginary)) + ")";
   }
private:
   double m_real;
   double m_imaginary;
};


std::tuple<double, double> getRealAndImaginary(string input)
{
   //function to convert the string into real and imaginary numbers
   if (input.length() == 0)
       return make_tuple(1, 1);
   std::stringstream ss1(input);
   std::string s1;
   vector<string> vec1;
   //split the string based on the delimiter ','
   while (getline(ss1, s1, ','))
   {
       vec1.push_back(s1);
   }

   return make_tuple(stod(vec1[0]), stod(vec1[1]));
}
int main()
{
   cout << "\nEnter a Complex number with real and imaginary part separated by a comma: ";
   string input_one;
   cin >> input_one;
   double real, imaginary;
   //getting the real and imaginary part from the function
   std::tie(real,imaginary) = getRealAndImaginary(input_one);

   //creating a complex object
   Complex c1(real, imaginary);

   cout << "\nEnter a Complex number with real and imaginary part separated by a comma: ";
   string input_two;
   cin >> input_two;

   double real1, imaginary1;
   std::tie(real1, imaginary1) = getRealAndImaginary(input_two);

   Complex c2(real1, imaginary1);

   cout << "\nSum of two complex numbers is " << c1.add(c2).toString() << " and the difference is " << c1.subtract(c2).toString();


   cout << "\nEnter a Complex number with real and imaginary part separated by a comma: ";
   string input_three;
   getline(cin, input_three);
   double real2, imaginary2;
   std::tie(real2, imaginary2) = getRealAndImaginary(input_three);

   Complex c3(real2, imaginary2);

   cout << "\nEnter a Complex number with real and imaginary part separated by a comma: ";
   string input_four;
   cin >> input_four;

   double real3, imaginary3;
   std::tie(real3, imaginary3) = getRealAndImaginary(input_four);

   Complex c4(real3, imaginary3);

   cout << "\nSum of two complex numbers is " << c3.add(c4).toString() << " and the difference is " << c3.subtract(c4).toString();

   return 0;

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...
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
  • 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++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to...

    C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + j imaginaryPart 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 is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks: Adding two Complex...

  • Create a class for working with complex numbers. Only 2 private float members are needed, the...

    Create a class for working with complex numbers. Only 2 private float members are needed, the real part of the complex number and the imaginary part of the complex number. The following methods should be in your class: a. A default constructor that uses default arguments in case no initializers are included in the main. b. Add two complex numbers and store the sum. c. Subtract two complex numbers and store the difference. d. Multiply two complex numbers and store...

  • 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...

  • Object Oriented Programming Please write the code in C++ Please answer the question in text form...

    Object Oriented Programming Please write the code in C++ Please answer the question in text form 9.5 (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 where i is V-I 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 in...

  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

  • Create the header file named “Complex.h” that contains the following class: The class Complex represents a...

    Create the header file named “Complex.h” that contains the following class: The class Complex represents a complex number which is a number of the form a + bi where a and b are real numbers and i2 = −1. The class should contain: Private double field named real. Private double field named imaginary. Public default constructor that assigns 1 to real and 0 to imaginary. Public overloaded constructor that takes a double as a parameter named real. It assigns real...

  • (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to h and would be stored in the object as 1 in the numerator...

  • 5. A complex number consists of two components: the real component and the imaginary component. A...

    C++ //add as many comments as possible 5. A complex number consists of two components: the real component and the imaginary component. An example of a complex number is 2+3i, where 2 is the real component and 3 is the imaginary component of the data. Define a class MyComplexClass. It has two data values of float type: real and imaginary This class has the following member functions A default constructor that assigns 0.0 to both its real and imaginary data...

  • Write a class called "Complex". The class has two private data members which correspond to the...

    Write a class called "Complex". The class has two private data members which correspond to the real and imaginary parts of a complex number. .The class Complex should have several (methods) functions: set the values, display the number in real/imaginary (re + j*im) format, and display the number in polar (r, theta) format. The main program should declare two objects of type Complex. It should set the first object to 3 +j2.5 and the second object to 2.7-j6.8. The program...

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
Active Questions
ADVERTISEMENT