In: Computer Science
PLEASE WRITE THIS IN C++
Write a ComplexNumber class that has two member variables realNum and complexNum of type double. Your class shall have following member functions
Complex();
Complex(double, double);
Complex add(Complex num1, Complex num2);
Complex subtract(Complex num1, Complex num2);
string printComplexNumber();
Write a driver program to test your code. Please make sure your code is separated into Complex.h Containing definition of the class Complex.cpp Containing the implementation ComplexTestProgram.cpp Containing main program Use the following directive in class definition to prevent duplicate inclusions . #ifndef COMPLEX_H #define COMPLEX_H ... #endif
Your Output should be like
Enter real and imaginary part of first complex number
5
6
Enter real and imaginary part of second complex number
4
9
Sum of two complex numbers is 9+15i
Enter real and imaginary part of first complex number
-7
6
Enter real and imaginary part of second complex number
9
-3
Sum of two complex numbers is 2+3i
Enter real and imaginary part of first complex number
-6
-6
Enter real and imaginary part of second complex number
-3
-8
Sum of two complex numbers is -9+-14i
PROGRAM:
#include <iostream>
using namespace std;
class ComplexNumber
{
private:
double realNum;
double complexNum;
public:
ComplexNumber();
ComplexNumber(double, double);
~ComplexNumber();
double getRealNum();
double getComplexNum();
void setRealNum(double realNum);
void setComplexNum(double complexNum);
void print(ComplexNumber);
ComplexNumber operator+(ComplexNumber);
ComplexNumber operator-(ComplexNumber);
};
ComplexNumber::ComplexNumber()
{
}
ComplexNumber::ComplexNumber(double realNum, double
complexNum)
{
this->realNum = realNum;
this->complexNum = complexNum;
}
ComplexNumber::~ComplexNumber()
{
}
double ComplexNumber::getRealNum()
{
return realNum;
}
double ComplexNumber::getComplexNum()
{
return complexNum;
}
void ComplexNumber::setRealNum(double realNum)
{
this->realNum = realNum;
}
void ComplexNumber::setComplexNum(double complexNum)
{
this->complexNum = complexNum;
}
void ComplexNumber::print(ComplexNumber c)
{
cout << c.getRealNum() << "/" <<
c.getComplexNum();
}
ComplexNumber ComplexNumber::operator+(ComplexNumber c)
{
ComplexNumber a;
a.realNum = realNum + c.realNum;
a.complexNum = complexNum + c.complexNum;
return a;
}
ComplexNumber ComplexNumber::operator-(ComplexNumber c)
{
ComplexNumber a;
a.realNum = realNum - c.realNum;
a.complexNum = complexNum - c.complexNum;
return a;
}
int main()
{
// Declaring variables
double real, complex;
// Getting the first complex number entered by the user
cout << "\nEnter The First Complex Number:" <<
endl;
cout << "Enter Real Part:";
cin >> real;
cout << "Enter Complex Part:";
cin >> complex;
// creating an instance of ComplexNumber class
ComplexNumber c1(real, complex);
// Getting the second complex number entered by the user
cout << "\nEnter The Second Complex Number:" <<
endl;
cout << "Enter Real Part:";
cin >> real;
cout << "Enter Complex Part:";
cin >> complex;
// creating an instance of ComplexNumber class
ComplexNumber c2(real, complex);
// Performing addition
ComplexNumber c3 = c1 + c2;
c1.print(c1);
cout << "+";
c2.print(c2);
cout << "=";
c3.print(c3);
cout << endl;
// Performing subtraction
ComplexNumber c4 = c1 - c2;
c1.print(c1);
cout << "-";
c2.print(c2);
cout << "=";
c4.print(c4);
return 0;
}