67 301
Assignments Done
99,1%
Successfully Done
In November 2018

Answer to Question #48825 in Other 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
Expert's answer
#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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions