In: Computer Science
This class should include .cpp file, .h file and driver.cpp (using the language c++)!
Overview of complex Class
The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features.
Constructor
Only one constructor with default value for Real = 0.0, Imaginary = 0.0; But it should construct the complex numbers with, 1) no argument, 2) 1 argument, 3) 2 arguments. Please refer the rat2.h file and rat2.cpp.
Data members
The complex has two members which should be Real values. For example, x+yi, Real = x, Imaginary = y.
Member functions : Provide the following four member functions:
getReal
returns the real part of the complex.
getImaginary
returns the imaginary part of this complex number.
setReal
sets the real part of the complex.
setImaginary
sets the imaginary part of this complex number.
Math operators
The class must implement binary arithmetic operations such as addition, subtraction, multiplication, and division. You should be able to use operators (+, -, *, /).
Addtion (+)
Add two objects. For example, (a+bi) + (c+di) = (a+c) + (b+d)i
Subtraction(-)
Perform complex minus operation
Multiplication(*)
Multiply the left-hand-side object by the right-hand-side and return complex object.
Division(/)
Divide the left-hand-side object by the right-hand-side object. You have to know the division of complex numbers. Divide by zero error should be handled in this method. That is, print an error message, and return the original one. Since it is an exception error, should not affect the following tasks. (Same for /= method)
Conjugate
The complex conjugate of the complex number z = x + yi is defined to be x − yi. This function returns the conjugate of the input complex.
Comparison
The class must implement ==. !=.
Assignment
The class must implement +=, -=, *= and /=. (Divide by zero error should be handled in /= method.)
Stream I/O
The class must implement the << and >> operators:
Input
Take two values as a real and imaginary value.
Output
The format will be: X+Yi, where X and Y are a Real and Imaginary value respectively. Of course, if either X or Y is 0, it should not be displayed. However, if both X and Y are 0, the output should be 0. Also note that if X is 1, it should be printed out as 1, and that if Y is 1, its output should be i. Wrong examples: 1+0 i, 0+2i, 1i, etc..In the case of Y be negative, it should not have “+” between the two. For example, print 2-3i, instead of 2+-3i.
The code is written on Codeblocks. You can create your own main function
In the code written the sample main function is given and all the member function are commented properly.
ouput snippets is attached.code for the program is given below
Please paste it in some c++ support eidtor to see all the code flow for better understanding
#include <iostream>
using namespace std;
class Complex{
private:
float real;//real part of complex no
float img;//imaginary part of complex no
public:
Complex(){ //default constructor
real=0;
img=0;
}
Complex(double r){ //constructor with one argument with sets only
the real value
real=r;
}
Complex(double r,double i){ //constructor with tow arguments
real=r;
img=i;
}
double getReal(){ //getter function to get the real value
return real;
}
double getImaginary(){ //getter function to get the imaginary
value
return img;
}
void setReal(double r){ //setter function to set the real
value;
real=r;
}
void setImaginary(double i){ //setter function to set the imaginary
value
img=i;
}
Complex conjugate(Complex c){ //function to calculate the conjugate
of the complex no
Complex res;
res.real=c.real;
res.img=c.img;
return res;
}
friend Complex operator +(Complex c1,Complex c2){ //function to
calculate the sum on 2 complex no using +
Complex res;
res.real=c1.real+c2.real;
res.img=c1.img+c2.img;
return res;
}
friend Complex operator -(Complex c1, Complex c2) { //function to
calculate the difference on 2 complex no using -
Complex res;
res.real = c1.real - c2.real;
res.img = c1.img - c2.img;
return res;
}
friend Complex operator *(Complex c1,Complex c2){ //function to
calculate the multiplciation on 2 complex no using *
Complex res;
res.real=c1.real*c2.real-c1.img*c2.img;
res.img=c1.real*c2.img+c2.real*c1.img;
return res;
}
friend Complex operator /(Complex c1,Complex c2){ //function to
calculate the division on 2 complex no using /
Complex c3=c2.conjugate(c2);
Complex n=c1*c3;
double d=c2.real*c2.real+c2.img*c2.img;
Complex res;
if(d!=0){
res.real=n.real/d;
res.img=n.img/d;
return res; // result complex no returned if denominator is not
zero
}
else{
cout<<"Denominator should not be zero";
}
return c1; //if denominator is zero it will return the
numerator
}
friend bool operator ==(Complex c1,Complex c2){ //function to check
if complex no are equal using ==
if(c1.real==c2.real&&c1.img==c2.img)return true;
return false;
}
friend bool operator !=(Complex c1,Complex c2){ //function to check
if complex no are not equal using !=
if(c1.real==c2.real&&c1.img==c2.img)return false;
return true;
}
friend Complex operator +=(Complex c1,Complex c2){ //function to
calculate sum and assign to first complex no
Complex res=c1+c2;
return res;
}
friend Complex operator -=(Complex c1,Complex c2){ //function to
calculate difference and assign to first complex no
Complex res=c1-c2;
return res;
}
friend Complex operator *=(Complex c1,Complex c2){ //function to
calculate multipllication and assign to first complex no
Complex res=c1*c2;
return res;
}
friend Complex operator /=(Complex c1,Complex c2){ //function to
calculate division and assign to first complex no
Complex res=c1/c2;
return res;
}
friend ostream &operator<<( ostream &output, const
Complex &c ) { //output function to output complex no using
<<
if(c.real==0&&c.img==0){
output<<0;
}
else if(c.real>0){
output<<c.real;
}
if(c.img>0){
if(c.real==0.0&&c.img==1){
output<<"i";
}
else if(c.real==0.0&&c.img!=1){
output<< c.img<<"i";
}
else{
output<<"+"<<c.img<<"i";
}
}
else {
if(c.img==-1){
output<<"-i";
}
else output<<c.img<<"i";
}
return output;
}
friend istream &operator>>( istream &input, Complex
&c ) { //input function to take input on complex no using
>>
input >>c.real>>c.img;
return input;
}
};
int main(){ // main code
Complex c1,c2(4,5);
cin>>c1;
Complex c3;
c3=c1+c2;
cout<<c3<<endl;
c3=c1*c2;
cout<<c3<<endl;
c3=c1/c2;
cout<<c3<<endl;
if(c1==c2)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
c1+=c2;
cout<<c1;
cout<<endl;
cout<<c1.getReal()<<endl;
cout<<c1.getImaginary();
c1.setReal(4.5);
c1.setImaginary(5.4);
c1=c1.conjugate(c1);
cout<<c1;
return 0;
}