Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// complex.cpp //complex.h 2. (10 points) Write a method to implement operator++ for the Complex class from Lecture 3. This method should result in the

image text in transcribed

// complex.cpp

image text in transcribed

image text in transcribed

image text in transcribed

//complex.h

image text in transcribed

2. (10 points) Write a method to implement operator++ for the Complex class from Lecture 3. This method should result in the number having one added to the real component and return the new value of the number. This is the prefix version of the operator, so it would be used like this: Complex b = ++a; Write the code that would be in the cpp file. The header file contains: Complex &operator++ (); // increments a Complex number by one FYI: The postfix version is similar, but uses a dummy int parameter to distinguish itself from the prefix version. The postfix version also returns by value, instead of reference. #include #include "Complex.h" //------------------------------ Complex ------------------------------------ // default constructor: parameters are numerator and denominator respectively Complex:: Complex(float r, float i) { real = r; imag = i; ---------- + ------- // -------- 1/ over loaded +: addition of 2 Complexs, current object and parameter Complex Complex:: operator+(const complex& a) const { Complex sum; sum.real = a.real + real, sum.imag = a.imag + imag; return sum; // ------- 1/ over loaded - subtraction of 2 Complexs, current object and parameter Complex Complex::operator - (const Complex& S) const { Complex sub; sub.real = real - s.real; sub.imag = imag - s.imag; return sub; //------ 1/ over loaded *: multiplication of 2 Complexs, current object and parameter Complex Complex::operator* (const Complex& m) const { Complex mult; mult.real = m.real * real - m.imag * imag; mult.imag = m.real * imag + real * m.imag; return mult; //----- ---------- ------- // over Loaded /: division of 2 Complexs, current object and parameter, 1 division by zero terminates prog Complex Complex:: operator/(const complex& v) const { Complex div; if (v.real != 0 || v.imag != 0) { // make sure new denominator != zero float denom = v.real * v.real + v.imag * v.imag; div.real = (real * V.real + imag * v.imag) / denom; div.imag = (imag * v.real - real * v.imag) / denom; else { cout : true if current object magnitude is > parameter, otherwise false bool Complex:: operator>(const Complex& r) const { return magnitude() > r. magnitude(); // ------------- // over Loaded = true if current object is >= parameter, otherwise false bool Complex:: operator>=(const complex& r) const { return!(*this r); - - - - - - - - - - - - - - - // over loaded == true if current object is == parameter, otherwise false bool Complex:: operator==(const complex& r) const { return (real == r.real && imag == r.imag); //-- ------- = ------- // over loaded !=: true if current object is != parameter, otherwise false bool Complex:: operator !=(const complex& r) const { return !(*this == n); //.................-------------- + ---------------------- 1/ overloaded += current object = current object + parameter const complex& Complex:: operator+=(const complex& a) { *this = *this + a; return *this; //--- // over Loaded -= current object = current object - parameter const complex& Complex::operator-=(const complex& a) { *this = *this - a; return *this; //--- // over Loaded *= current object = current object * parameter const Complex& Complex:: operator*=(const complex& a) { *this = *this * a; return *this; 1 //-------- --------------- = ------------------- // over loaded /= current object = current object / parameter const Complex& Complex::operator/=(const Complex& a) { *this = *this / a; return *this; } // - -...----- ---- --- // over loaded = 0) output r.real > ------ // over loaded >>: takes 2 ints as numerator and denominator, does no error checking, standard c casting between floats, char, etc occurs istream& operator>>(istream &input, Complex &r) { input >> r.real >> r. imag; return input; - - - - - - - - - - - - - - - - - - - - - - - - - LYHIL L U --------------- magnitude -------------- // compute magnitude of float and cast it into template float type float Complex::magnitude() const { return float(sqrt(real*real + imag* imag)); #pragma once #include using namespace std; //--- 1/ Complex numbers: complex numbers are defined as a + bi where i is // is the square root of -1. //--- class Complex { // over loaded : friend ostream& operator>: takes two number and converts to a complex friend istream& operator>>(istream&, Complex &); public: // default constructor: parameters are real and imaginary, respectively Complex(float = 0, float = 0); // arithmetic operators Complex operator+(const complex & const; Complex operator - (const Complex & const; Complex operator*(const complex & const; Complex operator/(const Complex &) const; // add 2 Complexs // subtract 2 Complexs // multiply 2 Complexs // divide 2 Complexs // boolean comparison operators bool operator>(const Complex & const; bool operator=(const Complex & const; bool operators (const Complex & const; bool operator==(const Complex & const; bool operator !=(const Complex & const; // is object > parameter? // is object = parameter? // is object >= parameter? // is object == parameter? // is object != parameter? // assignment operators const Complex &operator+=(const Complex &); const Complex &operator -=(const Complex &); const Complex &operator*=(const Complex &); const Complex &operator/=(const Complex &); // current object += parameter // current object = parameter // current object i= parameter // current object /= parameter private: float real; float imag; float magnitude() const; // real part of the complex // imaginary part of the complex // find magnitude of the complex

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Larry Ellison Database Genius Of Oracle

Authors: Craig Peters

1st Edition

0766019748, 978-0766019744

More Books

Students also viewed these Databases questions

Question

BPR always involves automation. Group of answer choices True False

Answered: 1 week ago