Problem from texbook

This is a discussion on Problem from texbook within the C++ Programming forums, part of the General Programming Boards category; //Help me //I got 3 errors left //whats wrong with program #include<iostream.h> class Complex { friend ostream &operator<<( ostream &, ...

  1. #1
    Unregistered
    Guest

    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:perator+(const Complex &operand2 ) const

    {
    Complex added;
    added.real = real + operand2.real;
    added.imaginary = imaginary + operand2.imaginary;
    return added;


    }

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

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

    }

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

    return *this;
    }

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

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


    const Complex& Complex:perator*(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:rint() 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;

    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Please use CODE tags.

    B.t.w. real is a private member, make it public or make a function to change the value.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Same for imaginary

  4. #4
    Unregistered
    Guest

    Overloading * operator

    #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:perator+(const Complex &operand2 ) const

    {
    Complex added;
    added.real = real + operand2.real;
    added.imaginary = imaginary + operand2.imaginary;
    return added;


    }

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

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

    }

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

    return *this;
    }

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

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


    Complex Complex:perator*(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:rint() 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;

    }

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Please use CODE TAGS

    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;
    }

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    Complex Complex:&#58;operator*(const Complex &operand2)//Error HERE
    You forgot the const:
    Code:
    Complex Complex:&#58;operator*(const Complex &operand2) const

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21