Using C++ Create a class called Complex for performing arithmetic with complex n
ID: 3874635 • Letter: U
Question
Using C++
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
S-17
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).
Explanation / Answer
#include <iostream> // for "ostream", "istream" below
#include <sstream>
using namespace std;
// Class Complex definition
class Complex
{
friend ostream& operator<<(ostream& , const Complex& );
friend istream& operator>>(istream& , Complex& );
public:
// Prototype of constructor
Complex();
Complex(double real, double imaginary);
// Accessors
double getReal() const;
double getImaginary() const;
// Mutator
void setReal(double r);
void setImaginary (double im);
friend Complex Add(const Complex, const Complex);
friend Complex Subtract(const Complex, const Complex);
string toString();
private:
// To store real part.
double real;
// To store imaginary part
double imaginary;
};// End of class
// Default constructor
Complex::Complex()
{
real = 1;
imaginary = 1;
}// End of constructor
// Overloading Input redirection operator to accept data
istream& operator>>(istream& is, Complex& c)
{
char symbol;
is >> c.real >> symbol >> c.imaginary;
if (symbol == '-')
c.imaginary = -c.imaginary;
return is;
}// End of function
// Function to return real part
double Complex::getReal() const
{
return real;
}// End of function
// Function to return imaginary part
double Complex::getImaginary() const
{
return imaginary;
}// End of function
// Function to set real part
void Complex::setReal(double r)
{
real = r;
}// End of function
// Function to set imaginary part
void Complex::setImaginary (double im)
{
imaginary = im;
}// End of function
// Function to calculate Sum
Complex Add(const Complex c1, const Complex c2)
{
Complex temp;
temp.real = c1.real + c2.real;
temp.imaginary = c1.imaginary + c2.imaginary;
return temp;
}// End of function
// Function to calculate Difference
Complex Subtract(const Complex c1, const Complex c2)
{
Complex temp;
temp.real = c1.real - c2.real;
temp.imaginary = c1.imaginary - c2.imaginary;
return temp;
}// End of function
// Overloading output redirection operator to display data
ostream& operator<<(ostream& os, const Complex& c)
{
if (c.imaginary == 0)
return (os << c.real); // no imaginary part
if (c.real == 0)
return (os << c.imaginary << 'i'); // no real part
os << c.real; // both parts
if (c.imaginary > 0)
os << " + " << c.imaginary<< 'i';
else
os << " - " << -c.imaginary << 'i';
return os;
}// End of function
// toString method to display the output
string Complex::toString()
{
string res = "";
std::ostringstream strs;
strs << this->real;
std::string str = strs.str();
res += str;
res += ",";
std::ostringstream strs1;
strs1 << this->imaginary;
std::string str1 = strs1.str();
res += str1;
res += ")";
return res;
}// End of method
// Main method definition
int main()
{
Complex c1, c2, c3;
cout<<" Enter a Complex number with the real and imaginary parts separated by a comma: ";
cin>>c1;
cout<<" Enter a Complex number with the real and imaginary parts separated by a comma: ";
cin>>c2;
c3 = Add(c1, c2);
cout<<"The sum of the two complex numbers is("<<c3.toString();
c3 = Subtract(c1, c2);
cout<<"and the difference is("<<c3.toString();
}
Sample Run:
Enter a Complex number with the real and imaginary parts separated by a comma: 5,2
Enter a Complex number with the real and imaginary parts separated by a comma: 2,1
The sum of the two complex numbers is(7,3)and the difference is(3,1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.