Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. C++ Program Create a class called Complex for performing arithmetic with comp

ID: 3874378 • Letter: 1

Question

1. C++ Program

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.

Explanation / Answer

#include<iostream>
#include<string>
#include<sstream>

using namespace std;

class Complex {

   private:
       double real;
       double img;
   public:
      Complex(){
          real = 1;
          img = 1;
      }
      Complex(double a, double b){
          real = a;
          img = b;
      }
      void setReal(double a){
          real = a;
      }
      void setImg(double a){
          img = a;
      }
      double getReal(){
          return real;
      }
      double getImg(){
          return img;
      }
      string toString(){
         stringstream ss;
         ss << "(" << real << "," << img << ")";
         return ss.str();
      }
      Complex add( Complex b){
         double rl = real + b.getReal();
         double im = img + b.getImg();
         return Complex(rl,im);
      }
      Complex subtract(Complex b){
         double rl = real - b.getReal();
         double im = img - b.getImg();
         return Complex(rl,im);
      }
};

int main(){

   string s;
   double rl,img;
   Complex c1,c2,c3;
   cout << "Enter real and imaginery part of first complex number:";
   getline(cin,s);
  
   if (s.length() != 0){
      stringstream ss(s);
      ss >> rl >> img;
      c1.setReal(rl);
      c1.setImg(img);
   }
  
   cout << "Enter real and imaginery part of first complex number:";
   getline(cin,s);
  
   if (s.length() != 0){
      stringstream ss1(s) ;
      ss1 >> rl >> img;
      c1.setReal(rl);
      c1.setImg(img);
   }
  
   c3 = c1.add(c2);
   cout << "c1 = " << c1.toString() << endl;
   cout << "c2 = " << c2.toString() << endl;
  
   cout << "Sum of c1 + c2 :" << c3.toString()<< endl;
   c3 = c1.subtract(c2);
   cout << "Difference of c1 - c2 :" << c3.toString()<< endl;
   return 0;
}