Design a class for complex numbers called Complex. It must have two private vari
ID: 3847443 • Letter: D
Question
Design a class for complex numbers called Complex. It must have two private variables—one for the real part and one for the imaginary part of the complex number. Write appropriate constructor, accessor and mutator functions to create, get and set the objects and member variables. Implement the following for the class.
a) A friend function that adds two Complex objects and returns the sum as a Complex object to be assigned to a different Complex object.
b) A member function that accepts another Complex object as an argument, computes the sum, and returns the sum as a Complex object to be assigned to a different Complex object.
c) Write operator overloading functions for + (plus), -(minus) and *(multiply) to perform addition, subtraction and multiplication of complex numbers. Your functions must accept two constant Complex objects passed by reference.
d) Write operator overloading functions for >> and << to read and write Complex objects from the keyboard and on the screen respectively. While displaying the object, you need to make sure, they are displayed in the format of Re+Imi, where Re is the real part, Im is the imaginary part and i indicates the imaginary part of the complex number.
For example, if real part is 6 and imaginary part is -3, display 6-3i.
e) Create two objects in your main function and assign values to their member variables. Use any method you see fit. Demonstrate the functions you wrote in (a) through (c) using the objects you have just created. You’ll need to use appropriate objects to store the results of each operation. Use the accessor function to show the result of each operation.
f) Declare a new object of type Complex. Use your overloaded operators >> and << with cin and cout to get the values for the object and then display the object.
Explanation / Answer
#include <iostream>
using namespace std;
class Complex
{
private:
double real, img;
public:
//default constructor
Complex()
{
real = img = 0;
}
//constructor with 2 parameters
Complex(double rl, double im)
{
real = rl;
img = im;
}
//accessor and mutaotr functions
double getReal()
{
return real;
}
double getImaginary()
{
return img;
}
void setReal(double r)
{
real = r;
}
void setImaginary(double i)
{
img = i;
}
//member function to add another complex number adn return the result
Complex add(const Complex &other) const
{
return Complex(real + other.real , img + other.img);
}
// friend function to add to return sum of 2 complex numbers
friend Complex sum(const Complex &c1, const Complex &c2);
//friend functions for + , - , *
friend Complex operator +(const Complex &c1, const Complex &c2);
friend Complex operator -(const Complex &c1, const Complex &c2);
friend Complex operator *(const Complex &c1,const Complex &c2);
//>> and << operators
friend ostream & operator <<(ostream &out, const Complex &c);
friend istream & operator >>(istream &in, Complex &c);
};
Complex sum(const Complex &c1, const Complex &c2)
{
return c1.add(c2);
}
Complex operator +(const Complex &c1, const Complex &c2)
{
double r = c1.real+ c2.real;
double i = c1.img + c2.img;
return Complex(r, i);
}
Complex operator -(const Complex &c1, const Complex &c2)
{
double r = c1.real - c2.real;
double i = c1.img - c2.img;
return Complex(r, i);
}
Complex operator *(const Complex &c1, const Complex &c2)
{
double r = (c1.real * c2.real) - (c1.img * c2.img);
double i = (c1.img * c2.real )+ (c1.real * c2.img);
return Complex(r, i);
}
ostream & operator << (ostream &out, const Complex &c)
{
if(c.img < 0) //check if we should show a - sign instead of +
out << c.real << " - " << (-c.img) << " i";
else
out << c.real << " + " << c.img << " i";
return out;
}
istream & operator >>(istream &in, Complex &c)
{
in >> c.real >> c.img;
return in;
}
int main()
{
Complex c1(4,5), c2(2, 3);
Complex c3 = c1+c2, c4 = c1 - c2, c5 = c1 * c2;
cout<< "c1 = " << c1.getReal() << " + " << c1.getImaginary() << " i "<<endl;
cout<< "c2 = " << c2.getReal() << " + " << c2.getImaginary() << " i "<<endl;
cout<< "c3 = c1 + c2 = " << c3.getReal() << " + " << c3.getImaginary() << " i "<<endl;
cout<< "c4 = c1 - c2 = " << c4.getReal() << " + " << c4.getImaginary() << " i "<<endl;
cout<< "c5 = c1 * c2 = " << c5.getReal() << " + " << c5.getImaginary() << " i "<<endl;
Complex c6;
cout<<"Enter real and imaginary part for a complex number: ";
cin>>c6;
cout<<"You enter the following complex number " << c6 << endl;
}
output
c1 = 4 + 5 i
c2 = 2 + 3 i
c3 = c1 + c2 = 6 + 8 i
c4 = c1 - c2 = 2 + 2 i
c5 = c1 * c2 = -7 + 22 i
Enter real and imaginary part for a complex number: 5 -3
You enter the following complex number 5 - 3 i
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.