Problem Description: Declare and define a class for a complex number. A complex
ID: 3591971 • Letter: P
Question
Problem Description:
Declare and define a class for a complex number. A complex number in mathematics is defined as x+yi , where x defines the real part of the number and y is the imaginary part.
Requirements (Program to be written in C++):
Define a complex class that has the following member functions:
o constructor that initializes the complex number by default arguments
o set function that sets the real part of the complex number
o set function that sets the imaginary part of the complex number
o get function that returns the real part of the complex number
o get function that returns the imaginary part of the complex number
o a function that displays the complex number
Add the following nonmember functions in your complex header file and implementation file :
o A function that returns the sum of two complex numbers
o A function that returns the difference of two complex numbers
o A function that returns the product of two complex numbers
o A function that returns the quotient of two complex numbe
Write the class in header and implementation files , and compile it separately from the client program
Explanation / Answer
Here is the header file:
#include <iostream>
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
// A complex number in mathematics is defined as x+yi ,
//where x defines the real part of the number and y is the imaginary part.
double real;
double imag;
public:
//o constructor that initializes the complex number by default arguments
Complex();
Complex(double, double);
//o set function that sets the real part of the complex number
void setReal(double);
//o set function that sets the imaginary part of the complex number
void setImag(double);
//o get function that returns the real part of the complex number
double getReal();
//o get function that returns the imaginary part of the complex number
double getImag();
//o a function that displays the complex number
void display();
};
//Add the following nonmember functions in your complex header file and implementation file :
//o A function that returns the sum of two complex numbers
Complex add(Complex, Complex);
//o A function that returns the difference of two complex numbers
Complex diff(Complex, Complex);
//o A function that returns the product of two complex numbers
Complex mul(Complex, Complex);
//o A function that returns the quotient of two complex numbers
Complex div(Complex, Complex);
#endif
And the .cpp file is:
#include <iostream>
#include "Complex.h"
using namespace std;
Complex::Complex()
{
real = imag = 0;
}
Complex::Complex(double real, double imag)
{
this->real = real;
this->imag = imag;
}
void Complex::setReal(double real) { this->real = real; }
void Complex::setImag(double imag) { this->imag = imag; }
double Complex::getReal() { return real; }
double Complex::getImag() { return imag; }
void Complex::display()
{
cout << real << "+" << imag << "i" << endl;
}
Complex add(Complex first, Complex second)
{
return Complex(first.getReal() + second.getReal(), first.getImag() + second.getImag());
}
Complex diff(Complex first, Complex second)
{
return Complex(first.getReal() - second.getReal(), first.getImag() - second.getImag());
}
Complex mul(Complex first, Complex second)
{
return Complex(first.getReal() * second.getReal() - first.getImag() * second.getImag(), first.getImag() * second.getReal() + second.getImag() * first.getReal());
}
Complex div(Complex first, Complex second)
{
double denominator = second.getReal() * second.getReal() + second.getImag() * second.getImag();
double realTemp = (first.getReal() * second.getReal() + first.getImag() * second.getImag()) / denominator;
double imagTemp = (first.getImag() * second.getReal() - first.getReal() * second.getImag()) / denominator;
return Complex(realTemp, imagTemp);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.