Define a class for complex numbers. A complex number is a number of the form a +
ID: 3530281 • Letter: D
Question
Define a class for complex numbers. A complex number is a number of the form a + b*i where for our purposes, a and b are numbers of type double, and i is a number that represents the quantity sqrt(-1). Represent a complex number as two values of type double. Name the member variables real and imaginary. (The variable for the number that is multiplied by i is the one called imaginary.) Call the class Complex. Include a constructor with two parameters of type double that can be used to set the member variables of an object to any values. Include a constructor that has only a single parameter of type double; call this parameter realPart and define the constructor so that the object will be initialized to realPart + 0*i. Include a default constructor that initializes an object to 0(that is, to 0 + 0*i). Overload all the following operators so that they correctly apply to the type Complex: ==, +, -, *, >>, and <<. You should also write a test program to test your class. In the interface file, you should define a constant i as follows: const Complex i(0,1);Explanation / Answer
#include <cmath> #include <iostream> #include <iomanip> using namespace std; class complex { private: double real; // Real Part double imag; // Imaginary Part public: complex(double,double); complex(const complex&); complex operator +(complex); complex operator -(complex); complex operator *(complex); complex operator /(complex); complex getconjugate(); complex getreciprocal(); float getmodulus(); void setdata(float,float); void getdata(); float getreal(); float getimaginary(); bool operator ==(complex); void operator =(complex); friend ostream& operator <<(ostream &s,complex &c); }; // CONSTRUCTOR complex::complex(float r=0.0f,float im=0.0f) { real=r; imag=im; } // COPY CONSTRUCTOR complex::complex(const complex &c) { this->real=c.real; this->imag=c.imag; }
void complex::operator =(complex c) { real=c.real; imag=c.imag; } complex complex::operator +(complex c) { complex tmp; tmp.real=this->real+c.real; tmp.imag=this->imag+c.imag; return tmp; } complex complex::operator -(complex c) { complex tmp; tmp.real=this->real - c.real; tmp.imag=this->imag - c.imag; return tmp; } complex complex::operator *(complex c) { complex tmp; tmp.real=(real*c.real)-(imag*c.imag); tmp.imag=(real*c.imag)+(imag*c.real); return tmp; } complex complex::operator /(complex c) { float div=(c.real*c.real) + (c.imag*c.imag); complex tmp; tmp.real=(real*c.real)+(imag*c.imag); tmp.real/=div; tmp.imag=(imag*c.real)-(real*c.imag); tmp.imag/=div; return tmp; } complex complex::getconjugate() { complex tmp; tmp.real=this->real; tmp.imag=this->imag * -1; return tmp; } complex complex::getreciprocal() { complex t; t.real=real; t.imag=imag * -1; float div; div=(real*real)+(imag*imag); t.real/=div; t.imag/=div; return t; } float complex::getmodulus() { float z; z=(real*real)+(imag*imag); z=sqrt(z); return z; } void complex::setdata(float r,float i) { real=r; imag=i; } void complex::getdata() { cout<<"Enter Real:"; cin>>this->real; cout<<"Enter Imaginary:"; cin>>this->imag; } float complex::getreal() { return real; } float complex::getimaginary() { return imag; } bool complex::operator ==(complex c) { return (real==c.real)&&(imag==c.imag) ? 1 : 0; } ostream& operator <<(ostream &s,complex &c) { s<<"Real Part = "<<c.real<<endl <<"Imaginary Part = "<<c.imag<<endl; s<<"z = "<<c.real<<setiosflags(ios::showpos) <<c.imag<<"i"<<endl<<resetiosflags(ios::showpos); return s; } int main() { complex a(10.0f,-2.f); // Calls Constructor cout<<a<<endl; // Calls the overloaded << operator complex b(-2); // Calls Constructor complex c=b; // Calls Copy Constructor c=a; // calls overloaded = operator b.getdata(); // Calls Getdata() c.getdata(); if(b==c) // calls overloaded == operator cout<<"b == c"; else cout<<"b != c"; cout<<endl<<c.getmodulus()<<endl; // calls getmodulus function() complex d; d=a+b; // Calls overloaded + LINE 176 cout<<d<<endl; d=a-b; // Calls overloaded - LINE 178 cout<<d<<endl; d=a*b; // calls overloaded * LINE 180 cout<<d<<endl; d=a/b; // calls overloaded / LINE 182 cout<<d<<endl; return 0; }
this might help to construct your program! complex::complex(const complex &c) { this->real=c.real; this->imag=c.imag; }
#include <cmath> #include <iostream> #include <iomanip> using namespace std; class complex { private: double real; // Real Part double imag; // Imaginary Part public: complex(double,double); complex(const complex&); complex operator +(complex); complex operator -(complex); complex operator *(complex); complex operator /(complex); complex getconjugate(); complex getreciprocal(); float getmodulus(); void setdata(float,float); void getdata(); float getreal(); float getimaginary(); bool operator ==(complex); void operator =(complex); friend ostream& operator <<(ostream &s,complex &c); }; // CONSTRUCTOR complex::complex(float r=0.0f,float im=0.0f) { real=r; imag=im; } // COPY CONSTRUCTOR complex::complex(const complex &c) { this->real=c.real; this->imag=c.imag; }
void complex::operator =(complex c) { real=c.real; imag=c.imag; } complex complex::operator +(complex c) { complex tmp; tmp.real=this->real+c.real; tmp.imag=this->imag+c.imag; return tmp; } complex complex::operator -(complex c) { complex tmp; tmp.real=this->real - c.real; tmp.imag=this->imag - c.imag; return tmp; } complex complex::operator *(complex c) { complex tmp; tmp.real=(real*c.real)-(imag*c.imag); tmp.imag=(real*c.imag)+(imag*c.real); return tmp; } complex complex::operator /(complex c) { float div=(c.real*c.real) + (c.imag*c.imag); complex tmp; tmp.real=(real*c.real)+(imag*c.imag); tmp.real/=div; tmp.imag=(imag*c.real)-(real*c.imag); tmp.imag/=div; return tmp; } complex complex::getconjugate() { complex tmp; tmp.real=this->real; tmp.imag=this->imag * -1; return tmp; } complex complex::getreciprocal() { complex t; t.real=real; t.imag=imag * -1; float div; div=(real*real)+(imag*imag); t.real/=div; t.imag/=div; return t; } float complex::getmodulus() { float z; z=(real*real)+(imag*imag); z=sqrt(z); return z; } void complex::setdata(float r,float i) { real=r; imag=i; } void complex::getdata() { cout<<"Enter Real:"; cin>>this->real; cout<<"Enter Imaginary:"; cin>>this->imag; } float complex::getreal() { return real; } float complex::getimaginary() { return imag; } bool complex::operator ==(complex c) { return (real==c.real)&&(imag==c.imag) ? 1 : 0; } ostream& operator <<(ostream &s,complex &c) { s<<"Real Part = "<<c.real<<endl <<"Imaginary Part = "<<c.imag<<endl; s<<"z = "<<c.real<<setiosflags(ios::showpos) <<c.imag<<"i"<<endl<<resetiosflags(ios::showpos); return s; } int main() { complex a(10.0f,-2.f); // Calls Constructor cout<<a<<endl; // Calls the overloaded << operator complex b(-2); // Calls Constructor complex c=b; // Calls Copy Constructor c=a; // calls overloaded = operator b.getdata(); // Calls Getdata() c.getdata(); if(b==c) // calls overloaded == operator cout<<"b == c"; else cout<<"b != c"; cout<<endl<<c.getmodulus()<<endl; // calls getmodulus function() complex d; d=a+b; // Calls overloaded + LINE 176 cout<<d<<endl; d=a-b; // Calls overloaded - LINE 178 cout<<d<<endl; d=a*b; // calls overloaded * LINE 180 cout<<d<<endl; d=a/b; // calls overloaded / LINE 182 cout<<d<<endl; return 0; }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.