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...
Using C++, write the following program: The Point.h file declares the class and you will create...
Using C++, write the following program: The Point.h file declares the class and you will create a Point.cpp that contains the implementation and a main() that instantiates the Point class to add additional tests to cover the various overloaded operators Use the Point.h file that is found below (This class contains a point on a plane and this class is going to contain a X coordinate and Y coordinate. The class is also going to contain a member function that...
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent...
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent points in the cartesian plane #include <iostream> using namespace std; class Point { public:     Point()      // default constructor         Point(double x, double y); // another constructor         double x() const; // get function, return _x         double y() const; // get function, return _y private:         double _x, _y; }; (ii) Here is a test driver file for the Point class...
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...
Circle.cpp /******************************************************* * * Assignment: Lab 2 *    * Class member functions look like other...
Circle.cpp /******************************************************* * * Assignment: Lab 2 *    * Class member functions look like other functions * with the following differences: * 1) The function name has the format Class::Method * 2) The function can access any private or public data * defined for the class, e.g. "radius" in this example * 3) The constructor function(s) have no return data type ********************************************************/ #include <iostream> #include <string> #include "circle.h" // the definion of our Circle class using namespace std; /****************************************************...
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...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) //...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) // The Set Difference between two sets A and B is the set that consists of the elements of A which are not elements of B. Bag bag1 = (1,2,3) and Bag bag2 = (2,4,5) then bag1-=bag2 should return 1,3,4,5. //parameter a_bag to be subtracted from this (the calling) bag //post removes all data from items_ that is also found in a_bag //Since type is...
#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 {...