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
  • Start by using the starter code provided for the Line class. It implements operator overloading so...

    Start by using the starter code provided for the Line class. It implements operator overloading so you can use cin/cout with line objects. A line will be made up of two points. Create an object to implement a “line” class which allows the programmer to store a line. This class must use the “point” class developed in Part B. The object should have two constructors, appropriate set/get functions, and overloaded I/O (cin/cout) functions. It should include functions the return the...

  • C++ Class and Operator Overloading part 1 The source file contains a C++ program that declares...

    C++ Class and Operator Overloading part 1 The source file contains a C++ program that declares and initializes an array of Circles. The program is using a for loop statement to find the largest circle in the array and display it on the output screen. However, the program is currently not working. The problem is the program uses logical operator ' > ' for comparison of circles and it is not working unless we overload such operator for the class...

  • In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we...

    In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class should contain the following functions: •...

  • Overview This checkpoint is intended to help you practice the syntax of operator overloading with member...

    Overview This checkpoint is intended to help you practice the syntax of operator overloading with member functions in C++. You will work with the same money class that you did in Checkpoint A, implementing a few more operators. Instructions Begin with a working (but simplistic) implementation of a Money class that contains integer values for dollars and cents. Here is my code: /********************* * File: check12b.cpp *********************/ #include using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test...

  • Implement the operator +=, -=, +, -, in the class Date class Date { public: Date(int y=0, int m=1, int d=1); static bool leapyear(int year); int getYear() const; int getMonth() const; int...

    Implement the operator +=, -=, +, -, in the class Date class Date { public: Date(int y=0, int m=1, int d=1); static bool leapyear(int year); int getYear() const; int getMonth() const; int getDay() const; // add any member you need here }; You implementation should enable the usage like this: void f() { Date date = d; cout << "date = " << date << endl; cout << "date+1 = " << date+1 << endl; cout << "date-1 = "...

  • Project 1 - Operator Overloading (FlashDrive 2.0) In C++, many of the keyboard symbols that are...

    Project 1 - Operator Overloading (FlashDrive 2.0) In C++, many of the keyboard symbols that are used between two variables can be given a new meaning. This feature is called operator overloading and it is one of the most popular C++ features. Any class can choose to provide a new meaning to a keyboard symbol. Not every keyboard letter is re-definable in this way, but many of the ones we have encountered so far are like +, -, *, /,...

  • This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleTyp...

    This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...

  • Task 1 The purpose of this practical is to examine more complex types of operator overloading...

    Task 1 The purpose of this practical is to examine more complex types of operator overloading in the context of two classes. 2.1.1 Ingredient The ingredient class consists of two files: ingredient.h and ingredient.cpp. The class has the following definition: ingredient -name:string -price:double -weight:int ---------------- +ingredient(n:string,p:double,w:int) +~ingredient() +friend operator<<(output:ostream &,t:const ingredient &):ostream & +getName():string +getPrice():double +getWeight():int The class variables are as follows: • name: the name of the ingredient • price: the base price for each ingredient • weight: the...

  • C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header ...

    C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header files given. Then make a main program using Book and Warehouse to read data from book.dat and have functions to list and find book by isbn Objectives: Class Association and operator overloading This project is a continuation from Project 1. The program should accept the same input data file and support the same list and find operations. You will change the implementation of...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

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