**I mostly need help with the parts before the main function Implement a class t
ID: 3555075 • Letter: #
Question
**I mostly need help with the parts before the main function
Implement a class that allows addition, multiplication, etc. for complex numbers.
Addition: c1 + c2 = (R(c1) + R(c2)) + (I(c1) + I(c2))i
Subtraction:c1 + c2 = (R(c1) + R(c2)) + (I(c1) + I(c2))i
Multiplication: c1 + c2 = (R(c1) + R(c2)) + (I(c1) + I(c2))i
Division: (c1/c2)=[(R(c1)R(c2)+I(c1)I(c2))/(R(c2)^2+I(c2)^2)] + [(I(c1)R(c2)-R(c1)I(c2))/(R(c2)^2+I(c2)^2)]i
The template was given:
#include<iostream>
#include<cmath>
using namespace std;
//Defination of the class Complex
class Complex
{
//private member variables
private:
double real;
double imag;
//public functions
public:
Complex(void);
Complex(double,double);
void SetReal(double);
void SetImag(double);
double GetReal(void);
double GetImag(void);
void ComplexAdd(Complex,Complex);
void ComplexSub(Complex,Complex);
void ComplexMul(Complex,Complex);
void ComplexDiv(Complex, Complex);
};
Complex::Complex()
{
???
???
}
Complex::Complex(double a,double b)
{
???
???
}
void Complex::SetReal(double a)
{
???
}
void Complex::SetImag(double a)
{
???
}
double Complex::GetReal()
{
???
}
double Complex::GetImag()
{
???
}
void Complex::ComplexAdd(Complex c1,Complex c2)
{
???
???
}
void Complex::ComplexSub(Complex c1,Complex c2)
{
???
???
}
void Complex::ComplexMul(Complex c1,Complex c2)
{
???
???
}
void Complex::ComplexDiv(Complex c1,Complex c2)
{
???
???
}
int main()
{
//Declare two complex objects
???
//Declare 4 double variables to store the real and imainary part of two complex objects
???
//Prompt user to enter the real and imaginary part of the two complex number
???
???
//Set the real and imaginary part of the two complex number
???
???
//Declare a complex objects that will store the arithmetic operation of the two complex objects already defined
???
//Add the two complex objects and display
???
???
//Subtract the two complex objects and display
???
???
//Multiply the two complex objects and display
???
???
//Divide the two complex objects and display
???
???
system("pause");
return 0;
}
Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
//Defination of the class Complex
class Complex
{
//private member variables
private:
double real;
double imag;
//public functions
public:
Complex(void);
Complex(double,double);
void SetReal(double);
void SetImag(double);
double GetReal(void);
double GetImag(void);
void ComplexAdd(Complex,Complex);
void ComplexSub(Complex,Complex);
void ComplexMul(Complex,Complex);
void ComplexDiv(Complex, Complex);
};
Complex::Complex()
{
real=0;imag=0;
}
Complex::Complex(double a,double b)
{
real=a;imag=b;
}
void Complex::SetReal(double a)
{
real=a;
}
void Complex::SetImag(double a)
{
imag=a;
}
double Complex::GetReal()
{
return real;
}
double Complex::GetImag()
{
return imag;
}
void Complex::ComplexAdd(Complex c1,Complex c2)
{
real=(c1.GetReal()+c2.GetReal());
imag=(c1.GetImag()+c2.GetImag());
}
void Complex::ComplexSub(Complex c1,Complex c2)
{
real=(c1.GetReal()-c2.GetReal());
imag=(c1.GetImag()-c2.GetImag());
}
void Complex::ComplexMul(Complex c1,Complex c2)
{
real=((c1.GetReal()*c2.GetReal())-(c1.GetImag()*c2.GetImag()));
imag=((c1.GetReal()*c2.GetImag())+(c1.GetImag()*c2.GetReal()));
}
void Complex::ComplexDiv(Complex c1,Complex c2)
{
real=(((c1.GetReal()*c2.GetReal())+(c1.GetImag()*c2.GetImag()))/((c2.GetReal()*c2.GetReal())+(c2.GetImag()*c2.GetImag())));
imag=(((c2.GetReal()*c1.GetImag())-(c1.GetReal()*c2.GetImag()))/((c2.GetReal()*c2.GetReal())+(c2.GetImag()*c2.GetImag())));
}
int main()
{
Complex a,b;
double ra,ia,rb,ib;
cout<<"Enter real and imaginary parts of first complex number"<<endl;
cin>>ra>>ia;
cout<<"Enter real and imaginary parts of second complex number"<<endl;
cin>>rb>>ib;
a.SetReal(ra);
a.SetImag(ia);
b.SetReal(rb);
b.SetImag(ib);
Complex add,sub,mul,div;
add.ComplexAdd(a,b);
cout<<"a+b="<<add.GetReal()<<"+"<<add.GetImag()<<"i"<<endl;
sub.ComplexSub(a,b);
cout<<"a-b="<<sub.GetReal()<<"+"<<sub.GetImag()<<"i"<<endl;
mul.ComplexMul(a,b);
cout<<"a*b="<<mul.GetReal()<<"+"<<mul.GetImag()<<"i"<<endl;
div.ComplexDiv(a,b);
cout<<"a/b="<<div.GetReal()<<"+"<<div.GetImag()<<"i"<<endl;
system("pause");
return 0;
}
Sample output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.