Question
// Complex.h header file #pragma once #ifndef COMPLEX_H #define COMPLEX_H class Complex { private: double real; double imag; public: Complex() : real(0.0), imag(0.0) {} Complex(double
// Complex.h header file
#pragma once
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
private:
double real;
double imag;
public:
Complex() : real(0.0), imag(0.0) {}
Complex(double r, double i) : real(r), imag(i) {}
Complex(const Complex &c) : real(c.real), imag(c.imag) {}
// above statement must be pass-by-reference so that the default copy constructor is not used.
// if it is pass-by-value, the above statement is not a copy constructor and function
// overloading occurs the compiler cannot resolve
void setReal(double r) { real = r; }
void setImag(double i) { imag = i; }
void setComplex(Complex c) { real = c.real; imag = c.imag; }
double getReal() { return real; }
double getImag() { return imag; }
Complex add(const Complex &) const;
Complex sub(const Complex &) const;
Complex mul(const Complex &) const;
Complex div(const Complex &) const;
void print() const;
};
#endif
// Complex.cpp file
#include
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
}
// Application.cpp file
#include
#include "Complex.h"
using namespace std;
void main()
{
Complex a(4.0, 6.0), b(3.0, 5.0), *c = new Complex;
Complex d(a); d.print();
a.add(b).print();
}
Write a derived class for the Complex class to support (1) complex conjugate, (2) negative (negate both real and imaginary parts), and (3) swapping of real part and imaginary part. You must use the Complex class posted online (not your own class)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started