# Problem from texbook

• 07-25-2002
Unregistered
Problem from texbook
//Help me
//I got 3 errors left
//whats wrong with program

#include<iostream.h>

class Complex {

friend ostream &operator<<( ostream &, Complex & );
friend istream &operator>>(istream &, Complex & );

public:
Complex ( double = 0.0, double = 0.0 );
Complex operator+(const Complex & ) const;
Complex operator-(const Complex & ) const;
Complex operator*(const Complex & ) const;
const Complex &operator=(const Complex & );
int operator==(const Complex&) const;
int operator!=(const Complex&) const;

//void print() const;

private:
double real;
double imaginary;

};

Complex::Complex( double r, double i )

: real(r), imaginary(i) { }

Complex Complex::operator+(const Complex &operand2 ) const

{

}

Complex Complex::operator-( const Complex &operand2 ) const
{

Complex subtrac;
subtrac.real = real - operand2.real;
subtrac.imaginary = imaginary - operand2.imaginary;
return subtrac;

}

const Complex& Complex::operator=(const Complex &right )
{
real = right.real;
imaginary = right.imaginary;

return *this;
}

int Complex::operator==(const Complex &right) const
{ return right.real == real && right.imaginary == imaginary ? 1 : 0; }

int Complex::operator!=(const Complex &right) const
{ return !(*this == right); }

const Complex& Complex::operator*(const Complex &operand2)
{
Complex mult;
mult.real = real * operand2.real + imaginary * operand2.imaginary;
mult.imaginary = real * operand2.imaginary + imaginary * operand2.real;
return mult;

}

//void Complex::print() const
//{ cout<< '(' <<real <<"," <<imaginary<<')'; }

ostream& operator<<( ostream &output, const Complex &number)
{
output<<number.real;
//output<<ignore(2);
output<<number.imaginary;
}

istream& operator>>( istream &input, Complex &number)
{
input>>number.real;//ERROR HERE CANNOT ACCESS PRIVATE MEMBER REAL
input.ignore(2);
input >> number.imaginary;
input.ignore(2);
return input;
}

int main()

{
Complex x,y(4.3, 8.2), z(3.3, 1.1 ),cmp;

cout << "Enter a complex number in the form: a + bi\n? ";
cin >> cmp;

cout << "x: " << x << "\ny: " << y << "\nz: " << z << "\ncmp: "
<< cmp << endl;

cout<< "x: ";
//x.print();
cout<< "\ny: ";
//y.print();
cout<<"\nz: ";
//z.print();

x+y=z;
cout<<"\n\nx = y + z:\n";
//x.print();
cout<<" = ";
//y.print();
cout<<" +";
//z.print();

x = y - z;
cout<< "\n\nx = y - z:\n";
//x.print();
cout <<" = ";
//y.print();
cout<<" - ";
//z.print();
cout <<endl;

if (x != cmp)
cout << x << " != " << cmp << endl;

cout << endl;

x = cmp;

if (x == cmp)
cout << x << " == " << cmp << endl;

return 0;

}
• 07-25-2002
Monster

B.t.w. real is a private member, make it public or make a function to change the value.
• 07-25-2002
Monster
Same for imaginary
• 07-26-2002
Unregistered
#include<iostream.h>

class Complex {

friend ostream &operator<<( ostream &, Complex & );
friend istream &operator>>(istream &, Complex & );

public:
Complex ( double = 0.0, double = 0.0 );
Complex operator+(const Complex & ) const;
Complex operator-(const Complex & ) const;
Complex operator*(const Complex & ) const;//I am gettin an error here
const Complex &operator=(const Complex & );
int operator==(const Complex&) const;
int operator!=(const Complex&) const;
double getre() { return real; }
double getim() {return imaginary; }

//void print() const;

private:
double real;
double imaginary;

};

//Complex::Complex( double r, double i )

//: real(r), imaginary(i) { }

Complex::Complex(double r, double i)
{
real = r;
imaginary = i;
}

Complex Complex::operator+(const Complex &operand2 ) const

{

}

Complex Complex::operator-( const Complex &operand2 ) const
{

Complex subtrac;
subtrac.real = real - operand2.real;
subtrac.imaginary = imaginary - operand2.imaginary;
return subtrac;

}

const Complex& Complex::operator=(const Complex &right )
{
real = right.real;
imaginary = right.imaginary;

return *this;
}

int Complex::operator==(const Complex &right) const
{ return right.real == real && right.imaginary == imaginary ? 1 : 0; }

int Complex::operator!=(const Complex &right) const
{ return !(*this == right); }

Complex Complex::operator*(const Complex &operand2)//Error HERE
{
//Complex mult;
// mult.real = real * operand2.real + imaginary * operand2.imaginary;
//mult.imaginary = real * operand2.imaginary + imaginary * operand2.real;
//return mult;

}

//void Complex::print() const
//{ cout<< '(' <<real <<"," <<imaginary<<')'; }

ostream& operator<<( ostream &output, const Complex &number)
{
output<<number.real;
//output<<ignore(2);
output<<number.imaginary;
}

istream& operator>>( istream &input, Complex &number)
{
input>>number.real;//ERROR HERE CANNOT ACCESS PRIVATE MEMBER REAL
input.ignore(2);
input >> number.imaginary;
input.ignore(2);
return input;
}

int main()

{
Complex x,y(4.3, 8.2), z(3.3, 1.1 ),cmp;

cout << "Enter a complex number in the form: a + bi\n? ";
cin >> cmp;

cout << "x: " << x << "\ny: " << y << "\nz: " << z << "\ncmp: "
<< cmp << endl;

cout<< "x: ";
//x.print();
cout<< "\ny: ";
//y.print();
cout<<"\nz: ";
//z.print();

x+y=z;
cout<<"\n\nx = y + z:\n";
//x.print();
cout<<" = ";
//y.print();
cout<<" +";
//z.print();

x = y - z;
cout<< "\n\nx = y - z:\n";
//x.print();
cout <<" = ";
//y.print();
cout<<" - ";
//z.print();
cout <<endl;

if (x != cmp)
cout << x << " != " << cmp << endl;

cout << endl;

x = cmp;

if (x == cmp)
cout << x << " == " << cmp << endl;

return 0;

}
• 07-26-2002
Monster

Without code tags your code looks a bit messy (no spacing and all kind of smilies in your code).
When you start the code first type &#91;CODE&#93;
At the end of the code type: &#91;/CODE&#93;

You can then press the Preview button to see the result before posting it. It should be something like this:
Code:

```int main() {   printf("Hello world!\n");   return 0; }```
• 07-26-2002
Monster
Code:

`Complex Complex:&#58;operator*(const Complex &operand2)//Error HERE`
You forgot the const:
Code:

`Complex Complex:&#58;operator*(const Complex &operand2) const`