Question

Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 file...

Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file)

Operator Overloading – Chapter 14

  1. Design a class Complex for representing complex numbers and the write overloaded operators for adding, subtracting, multiplying and dividing 2 complex numbers. Also create a function that will print a complex number on the screen. Provide appropriate constructors and destructor.

Over load the operators + (add), - (minus), * (multiply), = , != (comparison), and provide the operators << and >> to printout and read in respectively. See section 14.9 of Liang C++ textbook for help with some operators.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________


// Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
public :
Complex(double real = 0.0, double imag = 0.0); // constructor
void setReal(double real);
void setImag(double imag);
double getReal();
double getImag();
  
Complex add(const Complex&)const ; // operator+()
Complex minus(const Complex&)const ; // operator-()
Complex multiply(const Complex&)const ; // operator*()
bool operator!=(const Complex&)const ; // operator*()

/**
* Friend function declarations
* Output complex number to an output stream, Example 3+4i
*/
friend std::ostream & operator <<(std::ostream &dout, const Complex&);
  
/**
* read complex number to an input stream
*/
friend std::istream & operator >>(std::istream &din, Complex &);

private:
double real; // real part
double imag; // imaginary part
};
#endif // COMPLEX_H
_____________________________

// Complex.cpp


#include <iostream>
using namespace std;
#include "Complex.h"
//Parameterized constructor
Complex::Complex(double real,double imag)
{
this->real=real;
this->imag=imag;
}

//Function implementation which add two complex numbers by using the operator overloading '+'
Complex Complex::add(const Complex &c)const
{
Complex temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}
//Function implementation which subtract two complex numbers by using the operator overloading '-'
Complex Complex::minus(const Complex &c)const
{
Complex temp;
temp.real=real-c.real;
temp.imag=imag-c.imag;
return temp;
}
//Function implementation which subtract two complex numbers by using the operator overloading '*'
Complex Complex::multiply(const Complex &c)const
{
Complex temp;
temp.real=real*c.real;
temp.imag=imag*c.imag;
return temp;
}
//Function implementation which display complex numbers by using the operator overloading '<<'
ostream & operator << (ostream &dout, const Complex &c)
{
dout<<"("<<c.real<<", "<<c.imag<<")";
return dout;
}
//Function implementation which read complex numbers by using the operator overloading '>>'
istream & operator >> (istream &din, Complex &c)
{
char comma,bracket;
din>>bracket;
din>>c.real;
din>>comma;
din>>c.imag;
din>>bracket;
  
return din;   
}
//function implementation for the overloading != operator
bool Complex::operator !=(const Complex &c)const
{
if(this->real!=c.real || this->imag!=c.imag)
return true;
else
return false;
}
______________________________

// main.cpp

#include <iostream>
using namespace std;
#include "Complex.h"
int main(int argc, char **argv)
{
Complex c1,c2,add,sub,mul;
cout<<"Enter Complex Number#1 in (a, b) form :";
cin>>c1;
cout<<"Enter Complex Number#2 in (a, b) form :";
cin>>c2;
add=c1.add(c2);
cout<<c1<<" + "<<c2<<" = "<<add<<endl;
sub=c1.minus(c2);
cout<<c1<<" - "<<c2<<" = "<<sub<<endl;
mul=c1.multiply(c2);
cout<<c1<<" * "<<c2<<" = "<<mul<<endl;
  

  
if(c1!=c2)
cout<<c1<<" is not equal to "<<c2<<endl;
else
cout<<c1<<" is equal to "<<c2<<endl;
  
  
return 0;
}

______________________________

Output:

./ComplexAddMinusMultiply nter Complex Number#1 nter Complex Number#2 in <a, b) in 〈a, b) form forn :(4.3, :(3.3, 8.2) 1.1) (

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT