Question

This is a C++ programming question. Please provide the correct, workable code. Use the following three programs to help solve the problem. Provide comments throughout the code.

Problem to solve:

1. Use the Complex class posted online. Modify the Complex h and Complex.cpp to support operator overloading for the operator

Code 1: Complex.h

#pragma once
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
private:
   double real;
   double imag;
public:
   // initialize the complex number to 0.0
   Complex() : real(0.0), imag(0.0) {}
   // initialize the complex number at declaration or new
   Complex(double r, double i) : real(r), imag(i) {}
   // this is actually the same as the default copy constructor provided by the compiler
   Complex(const Complex &c) : real(c.real), imag(c.imag) {}  

   void setReal(double r) {real = r;}
   void setImag(double i) {imag = i;}
   void setComplex(const Complex &c) {real = c.real; imag = c.imag;}
   double getReal() const {return real;}
   double getImag() const {return imag;}
   Complex getComplex() const {return *this;}

   Complex add(const Complex &) const;
   Complex sub(const Complex &) const;
   Complex mul(const Complex &) const;
   Complex div(const Complex &) const;

   void print() const;
   void print(const Complex &) const;
};

#endif

Code 2: Complex.cpp

#include <iostream>
using namespace std;
#include "Complex.h"

Complex Complex::add(const Complex &c) const {
   Complex result;
   result.real = real + c.real;
   result.imag = imag + c.imag;
   return result;
}
Complex Complex::sub(const Complex &c) const {
   Complex result;
   result.real = real - c.real;
   result.imag = imag - c.imag;
   return result;
}
Complex Complex::mul(const Complex &c) const {
   Complex result;
   result.real = real * c.real - imag * c.imag;
   result.imag = imag * c.real + real * c.imag;
   return result;
}
Complex Complex::div(const Complex &c) const {
   double t = c.real * c.real + c.imag * c.imag;
   Complex result;
   result.real = (real * c.real + imag * c.imag) / t;
   result.imag = (imag * c.real - real * c.imag) / t;
   return result;
}

void Complex::print() const {
   cout << "( " << real << ", " << imag << " )\n";
}

void Complex::print(const Complex &c) const {
   c.print();
}

Code 3: Application.cpp

#include <iostream>
#include "Complex.h"
using namespace std;

void main() {
   Complex a(4.0, 6.0), b(3.0, 5.0), *c = new Complex(1.0, 1.0);
   Complex d(a); d.print();
   a.add(b).add(*c).print();
   c->print(a);
}

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

Screenshot of program code:-

//Complex.h

#pragma once #ifndef COMPLEX_H #define COMPLEX_H class Complex { private: double real; double imag; public: VI initialize the

//Print functions prototype void print() const; void print(const Complex & const; }; #endif

//Complex.cpp #include <iostream> using namespace std; #include Complex.h //addition(+) operator function overloading Compl

//division(/) operator function overloading Complex Complex:: operator/(const Complex &c) const { double t = c.real * c.real

//Application.cpp #include <iostream> #include Complex.h using namespace std; int main() { Complex a(1.0, 2.0), 5(3.0, 4.0)

//Testing overloaded / operator c = a / b; cout << Complex number division result: ; c.print(); return 0; مه

Screenshot of output:-

Copy constructor result: (1, 2 > Complex number addition result: (4, 6 > Complex number subtraction result: < -2,-2 > Complex

Program code to copy:-

//Complex.h
#pragma once
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
private:
   double real;
    double imag;
public:
    // initialize the complex number to 0.0
    Complex() : real(0.0), imag(0.0) {}
    // initialize the complex number at declaration or new
    Complex(double r, double i) : real(r), imag(i) {}
    // this is actually the same as the default copy constructor provided by the compiler
    Complex(const Complex &c) : real(c.real), imag(c.imag) {}

    void setReal(double r) {real = r;}
    void setImag(double i) {imag = i;}
    void setComplex(const Complex &c) {real = c.real; imag = c.imag;}
    double getReal() const {return real;}
    double getImag() const {return imag;}
    Complex getComplex() const {return *this;}

   //Overloaded functions prototype
    Complex operator+(const Complex &) const;
    Complex operator-(const Complex &) const;
    Complex operator*(const Complex &) const;
    Complex operator/(const Complex &) const;

   //Print functions prototype
    void print() const;
    void print(const Complex &) const;
};

#endif

//Complex.cpp
#include <iostream>
using namespace std;
#include "Complex.h"

//addition(+) operator function overloading
Complex Complex::operator+(const Complex &c) const {
Complex result;
result.real = real + c.real;
result.imag = imag + c.imag;
return result;
}

//subtraction(-) operator function overloading
Complex Complex::operator-(const Complex &c) const {
Complex result;
result.real = real - c.real;
result.imag = imag - c.imag;
return result;
}

//multiplication(*) operator function overloading
Complex Complex::operator*(const Complex &c) const {
Complex result;
result.real = real * c.real - imag * c.imag;
result.imag = imag * c.real + real * c.imag;
return result;
}

//division(/) operator function overloading
Complex Complex::operator/(const Complex &c) const {
double t = c.real * c.real + c.imag * c.imag;
Complex result;
result.real = (real * c.real + imag * c.imag) / t;
result.imag = (imag * c.real - real * c.imag) / t;
return result;
}

//Functions to print the result
void Complex::print() const {
cout << "( " << real << ", " << imag << " )\n";
}

void Complex::print(const Complex &c) const {
c.print();
}

//Application.cpp
#include <iostream>
#include "Complex.h"
using namespace std;

int main() {
Complex a(1.0, 2.0), b(3.0, 4.0), c;

//testing copy constructor
Complex d(a);
cout << "Copy constructor result: ";
d.print();

//Testing overloaded + operator
c = a + b;
cout << "Complex number addition result: ";
c.print();

//Testing overloaded - operator
c = a - b;
cout << "Complex number subtraction result: ";
c.print();

//Testing overloaded * operator
c = a * b;
cout << "Complex number multiplication result: ";
c.print();

//Testing overloaded / operator
c = a / b;
cout << "Complex number division result: ";
c.print();

return 0;
}

Add a comment
Know the answer?
Add Answer to:
This is a C++ programming question. Please provide the correct, workable code. Use the following three...
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
  • Please implement the following problem in basic C++ code and include detailed comments so that I...

    Please implement the following problem in basic C++ code and include detailed comments so that I am able to understand the processes for the solution. Thanks in advance. // personType.h #include <string> using namespace std; class personType { public: virtual void print() const; void setName(string first, string last); string getFirstName() const; string getLastName() const; personType(string first = "", string last = ""); protected: string firstName; string lastName; }; // personTypeImp.cpp #include <iostream> #include <string> #include "personType.h" using namespace std; void...

  • Design a class Complex for handling Complex numbers and include the following: _ real: a double...

    Design a class Complex for handling Complex numbers and include the following: _ real: a double _ imaginary: a double The class has the following member functions. a. A constructor initializing the number with default parameters. b. Getters and Setters of the class data members as given below _ void setReal(double r) _ double getReal()const _ void setImaginary(double i) _ double getImaginary() const d. Overload unary ! operator which returns true if the real and the imaginary parts are zero,...

  • I need assistance with the C++ code below to remove the "this" pointers while maintaining the...

    I need assistance with the C++ code below to remove the "this" pointers while maintaining the code's current output and functionality. Also, there should be a private class in the header file, but I'm not sure what I should include there. Any help would be greatly appreciated! ***main.cpp driver file*** #include <iostream> #include "vector.h" using namespace std; int main() {   vectorInfo v1(7, 6);   vectorInfo v2(5, 4);       v1.set(3, 2);   cout << "v1 : ";   v1.print();       cout << "v2 :...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • Given two complex numbers, find the sum of the complex numbers using operator overloading. Write an...

    Given two complex numbers, find the sum of the complex numbers using operator overloading. Write an operator overloading function     ProblemSolution operator + (ProblemSolution const &P) which adds two ProblemSolution objects and returns a new ProblemSolution object. Input     12 -10      -34 38     where, Each row is a complex number. First element is real part and the second element is imaginary part of a complex number. Output     -22 28 Two complex numbers are 12-10i and -34+38i. Sum of complex numbers are =...

  • I cannot get the C++ code below to compile correctly. Any assistance would be greatly appreciated!...

    I cannot get the C++ code below to compile correctly. Any assistance would be greatly appreciated! ***main.cpp driver file*** #include #include #include "vector.h" using namespace std; int main() {     vectorInfo v1(3, -1);     vectorInfo v2(2, 3);     cout << "v1 : ";     v1.print();     cout << "v2 : ";     v2.print();     cout << "v1 magnitude : " << v1.magnitude() << endl;     cout << "v1 direction : " << v1.direction() << " radians" << endl;     cout...

  • c++ 2) Complex Class A complex number is of the form a+ bi where a and...

    c++ 2) Complex Class A complex number is of the form a+ bi where a and b are real numbers and i 21. For example, 2.4+ 5.2i and 5.73 - 6.9i are complex numbers. Here, a is called the real part of the complex number and bi the imaginary part. In this part you will create a class named Complex to represent complex numbers. (Some languages, including C++, have a complex number library; in this problem, however, you write the...

  • C++: questions are in fish.h. Write the code in fish.cpp and main.cpp #ifndef FISH_H #define FISH_H...

    C++: questions are in fish.h. Write the code in fish.cpp and main.cpp #ifndef FISH_H #define FISH_H #include <vector> class Fish { public: // (1 point) // Write code to initialize edible to is_edible, age to 0, size to 1. Fish (bool is_edible); // (1 point) // Print the vital stats (size and age) of the fish. void Print(); // (1 point) // If fish is at least the age of reproduce_age, reproduce with probability // of reproduce_probability. If reproduce, return...

  • 2. Enter, compile, and run Program 11.1. he same name as the ave a return type...

    2. Enter, compile, and run Program 11.1. he same name as the ave a return type nction as a member Chapter 11 Program 11.1 of this function the parameters include <iostream ing namespace std. *onging to the declaration section class Complex private: 1 double realPart; I notice the colon after the keyword pri eters, real ssigns the he func- ouble imaginary Part; maginaryPart. // function prototypes data // data member the key as also public: 11 again, notice the colon...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

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