Reference no: EM132157043
Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared.
The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks:
a) Adding two Complex numbers: The real parts are added together and the imaginary parts are added together
b) Subtracting two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.
c) Printing Complex numbers in the form (a, b) where a is the real part and b is the imaginary part.
Here are the files used:
header file:complex.h:
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
public: //not sure if these are correct
void printComplex();
void setComplexNumber(double, double);
double add(double);
double subtract(double);
private: //must add the variables
};
#endif
------
complex.cpp:
#include
using namespace std;
#include "Complex.h"
Complex::Complex(double real, double imaginary)
{
setComplexNumber(real, imaginary);
}
Complex Complex::add(const Complex &right)
{ //Write a statement to return a complex object. Add the real part of right to the real part of this Complex object and add the imaginary part of right to the imaginary part of this Complex object.
}
Complex::subtract(const Complex &right)
{//Same as above but with subtraction
}
void Complex::printComplex()
{
cout << '(' << realPart << "," << imaginaryPart << ')';
}
void Complex::setComplexNumber(double rp, double ip)
{
realPart = rp;
imaginaryPart = ip;
}
-------
main : complextest.cpp:
#include
using namespace std;
#include "Complex.h"
int main()
{
Complex a(1, 7), b(9, 2), c;
a.printComplex();
cout << "+";
b.printComplex();
cout << "=";
c = a.add(b);
c.printComplex();
cout << "\n";
a.setComplexNumber(10, 1);
b.setComplexNumber(11, 5);
a.printComplex();
cout << "-";
b.printComplex();
cout << "=";
c = a.subtract(b);
c.printComplex();
cout << endl;
}