can anyone help me with this program
write in c++ a defintion of a complex class for complex numbers.Each object of this class will represent a complex number x+y*j storing the real part x and the imaginary part y as float in private section. Include a constructor, a destructor, access functions,assignemnt functions, a nom() functions that returns magnitude of the complex number, an isequal to (complex) function and arithmetic functions plus (complex),minus(complex), times(complex), divide by(complex) and print () function.

It seems like you have just posted your homework assignment. Please give a try and check back here if you have any problems.

>>can anyone help me
Can we? Yes. Will we is another question. We are not going to do your homework for you. Use google and you will probably find what you are looking for (the code).

it's not a homework ! I had a quiz last monday and most of students couldn't solve it , and i think we will have such a question in the final exam that's why i am looking for a help here

http://en.wikipedia.org/wiki/Complex_number

that's an explanation of the math involved. Now use the instructions provided as a guideline for the declarations of the class variables and methods, and translate the math into C++. Show us some of your work and somebody will likely help you understand errors, answer specific questions, etc.

``````#include <cmath>
#include <iostream>
#include <iomanip.h>

using namespace std;

class complex
{
private:
float real;               // Real Part
float imag;      //  Imaginary Part

public:
complex(float,float);
complex(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(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;
cout<<d<<endl;
cout<<d<<endl;
cout<<d<<endl;
cout<<d<<endl;

return 0;
}``````

0. Where is `};` construct closed the class definition?
1. In practice the complex type based on the float type provides too low precision for serious (it's the same as correct ;)) math and engineering calculations. Well, it's a short-sighted requirements issue...
2. Rignt operator signatures looks like:

``complex complex::operator +(const complex& c) const;``

With current signatures your class create temporary right operand complex object on every op...
3. Overloaded output operator<< can't be this class member. No need to declare it as a friend: you have all public getters to implement this operator as usually.
4. Look at `std::operator<<` for real numbers. Have you ever seen that `cout << i << '\t' << j << endl;` statement prints something like

``````Integer = 1
Integer = 2``````

?
There is common standard text representation of complex numbers: `(rpart,ipart)` . Don't invent wheels. Avoid line adjustment control in overloaded output operators.
5. Define `complex::operator !=` . Nobody generates it automatically.
That's all for now...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.