Question

(1) Declaration and implementation of complex class, overloading operator +, -, != , /, --(prefix), --(postfix)...

(1) Declaration and implementation of complex class, overloading operator +, -, != , /, --(prefix), --(postfix) as friend functions.

        (2)The program should be in Separating Class Specification, Implementation(complex.h,complex.cpp), and Client Code(Task2.cpp).

            (3) Run the program and capture screenshots of output.

Declaration and implementation of complex class, overloading operator +, -, != , /=, --(prefix), --(postfix) as friend function */

#include "Complex.h"

#include <iostream>

using namespace std;

int main(){

      Complex c1(5, 4), c2(2, 10), c3;

      cout << "c1 = ";

      c1.display();

      cout << "c2 = ";

      c2.display();

      // Test the + and - operators

      c3 = c1 + c2;

      cout << "c1 + c2 = ";

      c3.display();

      c3 = c1 - c2;

      cout << "c1 - c2 = ";

      c3.display();

     

      if(c1!=c2) cout<<"c1 is not equal to c2."<<endl;

      else cout<<"c1 is equal to c2."<<endl;

     

      c3/=2.0;

      cout << "c3 is halfed as: ";

      c3.display();

      cout << "c3 is postfix -- as: ";

      (c3--).display();

      cout << "c3 is prefix -- as: ";

      (--c3).display();

      return 0;

}

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

//Complex.h

#include<iostream>
using namespace std;

class Complex{
   private :
       double real;
       double img;
      
   public:
       Complex();
       Complex(double , double);
       friend Complex operator + (Complex & , Complex &);
       friend Complex operator - (Complex & , Complex &);
       friend Complex operator /= (Complex & , double);
       friend Complex operator-- (Complex &);
       friend Complex operator-- (Complex & ,int);
       friend bool operator!= (Complex & , Complex &);
       void display();
      
};

//Complex.cpp

#include "Complex.h"

Complex::Complex(){
   real = 0;
   img = 0;
}
Complex::Complex(double r, double i){
   real = r;
   img = i;
}
Complex operator + (Complex & c1, Complex & c2){
   double r = c1.real + c2.real;
   double i = c1.img + c2.img;
  
   return *(new Complex(r,i));
}
Complex operator - (Complex & c1, Complex & c2){
   double r = c1.real - c2.real;
   double i = c1.img - c2.img;
  
   return *(new Complex(r,i));
}

Complex operator /= (Complex & c1, double val){
   c1.real /=val;
   c1.img /=val;
  
   return c1;
}
Complex operator-- (Complex & c1){
   double r = --c1.real;
   double i = --c1.img;
   return *(new Complex(r,i));
}

Complex operator-- (Complex & c1,int){
   double r = c1.real--;
   double i = c1.img--;
   return *(new Complex(r,i));
}

bool operator!= (Complex & c1, Complex &c2){
   return (c1.real!=c2.real) || (c1.img != c2.img);
}

void Complex::display(){
   cout<<this->real <<" + "<<this->img<<"i"<<endl;
}

Add a comment
Know the answer?
Add Answer to:
(1) Declaration and implementation of complex class, overloading operator +, -, != , /, --(prefix), --(postfix)...
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