Question

-For the COMPLEX class listed on the next page write code to: a) overload the addition...

-For the COMPLEX class listed on the next page write code to:

a) overload the addition operator for addition between two COMPLEX values.

b) Create a constructor that uses a single parameter to initialize the real member of a COMPLEX and sets the imaginary portion to 0.

c) Write the code to produce the insertion operator.

#include <iostream>
using namespace std;


class COMPLEX{
   double Re;
   double Im;
public:
   COMPLEX(double r, double im);
  
   COMPLEX(const COMPLEX &c)
   {
       Re = c.Re;
       Im = c.Im;
   }
  
   COMPLEX( )
   {
       Re = Im = 0;
   }
  
   ~COMPLEX( )
   {
   }
  
   COMPLEX operator*(const COMPLEX & b) const;
   COMPLEX operator*(const double &x) const;
   friend COMPLEX operator*(const double &x, const COMPLEX &c);
  
   COMPLEX operator-(const COMPLEX &b) const;
   COMPLEX operator-( ) const;
  
   friend ostream& operator<<(ostream&, const COMPLEX &);
};

Homework Answers

Answer #1

Here is the completed code for each question. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

//a) method declaration for overloading addition operator (should be placed inside

//the COMPLEX class)

COMPLEX operator+(const COMPLEX &b) const;

  

//a) method definition for overloading addition operator, should be placed outside class

COMPLEX COMPLEX::operator+(const COMPLEX &b) const{

                //creating a COMPLEX object after adding corresponding real and imaginary

                //values of this number and b

                COMPLEX result=COMPLEX(Re+b.Re , Im+b.Im);

                return result; //returning it

}

//end of question (a)

//b) declaration for constructor taking value for real part only

//(should be placed inside the COMPLEX class)

COMPLEX(double r);

  

//b) constructor definition, should be placed outside class

COMPLEX::COMPLEX(double r){

                Re=r;

                Im=0; //setting imaginary part to 0

}

//end of question (b)

//c) overloading << operator (insertion). this only needs definition as declaration

//is already done inside COMPLEX class. this should be placed outside class

ostream& operator<<(ostream& out, const COMPLEX &c){

                //printing the complex number in format a+bi or a-bi

                //checking if imaginary part is positive

                if(c.Im>=0){

                                //printing in format a+bi

                                out<<c.Re<<"+"<<c.Im<<"i";

                }else{

                                //printing in format a -bi

                                out<<c.Re<<c.Im<<"i";

                }

                return out;

               

}

//end of question (c)

Know the answer?
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
Similar Questions
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Please Use C++ I tried to calculate complex number by using *= and operator /= but...
Please Use C++ I tried to calculate complex number by using *= and operator /= but I got an incorrect result compared with the result of complex number calculator For example, When I calculate ( (c5 *= c4) *= c4) by using my operator function, the result was 1.08288e+06+1.11262e+07i on output, However, when using a complex calculator, the result was = −253987.448 − 355181.112i, so I got the wrong answer There is my code below. It compiles well, but my...
IN C++ format, Fill in the missing functions, using the concept of operator overloading. code: #include...
IN C++ format, Fill in the missing functions, using the concept of operator overloading. code: #include <string> #include <ostream> using namespace std; class Book { private:     string title;     string author;     unsigned isbn;     double price; public: /**Constructors*/    Book();    Book(string t, string a, unsigned i, double p);     /**Access Functions*/     string get_title();     string get_author();     unsigned get_isbn();     double get_price();     /**Manipulation Procedures*/    void set_title(string t);     void set_author(string a);     void set_isbn(unsigned...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits from base class Account and include an additional data member of type double that represents the fee charged per transaction (transactionFee). Write Checking- Account’s constructor that receives the initial balance, as well as a parameter indicating a transaction fee amount. If transaction fee is less than zero, the transactionFee will be set to zero. Write the chargeFee member function that updates the balance by...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
#ifndef BAG_H #define BAG_H #include <cstdlib> // Provides size_t using namespace std; class bag { public:...
#ifndef BAG_H #define BAG_H #include <cstdlib> // Provides size_t using namespace std; class bag { public: // TYPEDEFS and MEMBER CONSTANTS typedef int value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; // CONSTRUCTOR bag() {used = 0;} // MODIFICATION MEMBER FUNCTIONS size_type erase(const value_type& target); bool erase_one(const value_type& target); void insert(const value_type& entry); void operator +=(const bag& addend); void sort(const bag& b); //Sort the array in the bag object // CONSTANT MEMBER FUNCTIONS size_type size( ) const {...
Something is either messed up in my operator overload <<, covertopostfix function, or my main output....
Something is either messed up in my operator overload <<, covertopostfix function, or my main output. Cannot figure it out. please help. Please comment your changes too. Program below is supposed to be outputting like this: InFix is:   A+B-C Post fix is:   A B + C - InFix is:   A+C Post fix is:   A C + InFix is:   x*(y+z)-(w+t) Post fix is:   x y z + * w t + - InFix is:   A+B*(C+D)-E/F+G+H Post fix is:   A B C...
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the...
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the values for hours, minutes and seconds are a legal military time (i.e. 00 00 00 to 23 59 59) the program should display the formatted results (i.e. 12 34 56 should be displayed as 12:34:56). If there's an error for any of the entered values, an exception should be thrown and an error message should be displayed. Note, there are three exception conditions, one...
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...