Answer to Question #48825 in Engineering for Syn

Question #48825
Using C++ , design and implement a class COMPLEX having the following functionality:
a) allows representation of complex numbers
b) allows addition of two complex numbers
c) has overloading constructors and a copy constructor
d) allows assignment of one complex number to another
1
Expert's answer
2014-11-18T00:39:48-0500
#ifndef __COMPLEX_DEFINITION_ABC__
#define __COMPLEX_DEFINITION_ABC__
#include <fstream>
#include <cmath>
using namespace std;
class Complex
{
public:
Complex() : _real(0), _img(0)
{ }
Complex(double real, double img) : _real(real), _img(img)
{ }
Complex(const Complex & obj) : _real(obj._real), _img(obj._img)
{ }
Complex & operator=(const Complex & obj)
{
if(this == &obj) return *this;
_real = obj._real;
_img = obj._img;
return *this;
}
friend ostream & operator<<(ostream & os, const Complex & obj);
friend Complex operator+(const Complex & obj1, const Complex & obj2);
private:
double _real, _img;
};
ostream & operator<<(ostream & os, const Complex & obj)
{
os << obj._real;
if(obj._img >= 0) os << " +";
else os << " -";
os << abs(obj._img);
return os;
}
Complex operator+(const Complex & obj1, const Complex & obj2)
{
return Complex(obj1._real+ obj2._real, obj1._img+ obj2._img);
}
#endif /*__COMPLEX_DEFINITION_ABC__*/


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS