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

Object Oriented Programming. Coding in C++ What you will learn Implementing clas

ID: 3743659 • Letter: O

Question

Object Oriented Programming. Coding in C++

What you will learn Implementing classes .File /O More operator overloading Error checking Coding exercise 1. Create a class called complexNumber that stores a complex number of the form a+bi, where i is V-1. a is the real and b is the imaginary part of the number1. You should be able to get the real and imaginary parts of the number. a and b can be negative, e.g., "3+4i", "-3+4i", "3-4i", and-3- 4i". 2. Implement the ability to add, subtract, and multiply two complexNumber objects and save the . Overload the operators>>and

Explanation / Answer

ANS:

CODING:

//Complex.h

#pragma once

#include<iostream>

#include<string>

#include<fstream>

using namespace std;

class Complex

{

double real;

double imaginary;

public:

Complex();

Complex(string s);

Complex operator+(Complex &obj);

Complex operator-(Complex &obj);

Complex operator*(Complex &obj);

friend ostream& operator<<(ostream& out, Complex &obj);

friend ifstream& operator>>(ifstream& out, Complex &obj);

};

--------------------------------------------------

//Complex.cpp

#include"Sep_5_Complex.h"

Complex::Complex()

{

real = 0;

imaginary = 0;

}

Complex::Complex(string s)

{

string a, b;

bool isOp = false;

for (int i = 0; i < s.length(); i++)

{

if (s[i] != '+' || s[i] != '-')

{

if (!isOp)

a += s[i];

else

b += s[i];

}

else

{

isOp = true;

}

//convert a and b to double and store in real and imaginary respectively

real = stod(a);

imaginary = stod(b);

}

}

Complex Complex::operator+(Complex &obj)

{

Complex result;

result.real = real+obj.real;

result.imaginary = imaginary+obj.imaginary;

return result;

}

Complex Complex::operator-(Complex &obj)

{

Complex result;

result.real = real - obj.real;

result.imaginary = imaginary - obj.imaginary;

return result;

}

Complex Complex::operator*(Complex &obj)

{

Complex result;

result.real = real * obj.real;

result.imaginary = real * obj.imaginary;

result.imaginary += imaginary * obj.real;

result.imaginary += imaginary * obj.imaginary;

return result;

}

ostream& operator<<(ostream& out, Complex &obj)

{

//out << "Complex number is: ";

if(obj.imaginary > 0)

out << obj.real << "+" << obj.imaginary << "i";

else

out << obj.real << obj.imaginary << "i";

return out;

}

ifstream& operator>>(ifstream& in, Complex &obj)

{

string s;

string a, b;

bool isOp = false;

in >> s;

for (int i = 0; i < s.length(); i++)

{

if (s[i] != '+' && s[i] != '-' )

{

if (!isOp)

{

if(isdigit(s[i]))

a += s[i];

}

else

{

if (isdigit(s[i]))

b += s[i];

}

}

else

{

isOp = true;

}

}

//convert a and b to double and store in real and imaginary respectively

obj.real = stod(a);

obj.imaginary = stod(b);

return in;

}

-------------------------------------------------

//Main.cpp

#include<iostream>

#include<string>

#include<fstream>

#include"Sep_5_Complex.h"

using namespace std;

int main()

{

ifstream in;

ofstream out;

string a, b;

//open file for reading

in.open("complexInput.txt");

if (!in)

{

cout << "Nit able to open input file " << endl;

return -1;

}

//opern file for writing output

out.open("ComplexOutput.txt");

//check output file is open

if (!out)

{

cout << "Not able to open output file" << endl;

return -1;

}

//declare two objects of complex ttpe

Complex num1, num2,result;

char op;

while (!in.eof())

{

in >> num1;

in >> op;

in>>num2;

switch (op)

{

case '+':

result = num1 + num2;

out << "Addition of two complex numbers " << num1 << " and " << num2 << " is : " << result << endl;

break;

case '-':

result = num1 - num2;

out << "Subtraction of two complex numbers " << num1 << " and " << num2 << " is : " << result << endl;

break;

case '*':

result = num1 * num2;

out << "Multiplication of two complex numbers " << num1 << " and " << num2 << " is : " << result << endl;

break;

default:

cout << "Operator not implemented ";

}

}

}

/*input file Complexinput.txt has below content

3+4i + 5+8i
12+14i - 8+20i
12+14i * 8+10i

---------------------------------------------------

output in file called ComplexOutput.txt

Addition of two complex numbers 3+4i and 5+8i is : 8+12i

Subtraction of two complex numbers 12+14i and 8+20i is : 4-6i

Multiplication of two complex numbers 12+14i and 8+10i is : 96+372i

*/

PLEASE RATE THUMBSUP PLEASE