Ugh, this coding is killing me

This is a discussion on Ugh, this coding is killing me within the C++ Programming forums, part of the General Programming Boards category; Hello again. I need help with operation overloading with the >> input arrows. The previous threads did not help me ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Location
    Illinois
    Posts
    6

    Need help with operation overloading of >>

    Hello again. I need help with operation overloading with the >> input arrows. The previous threads did not help me much. I'm working with computing complex numbers. Can anybody tell me what I'm doing wrong in trying to redefine my input arrows?

    HEADER FILE:
    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    namespace Evan
    {
        class Complex
        {
        private:
            double real;
            double imag;
        public:
            Complex();
            /*post: default constructor */
            Complex(double notReal, double notImag);
            /*post: non-default constructor */
            Complex operator+(const Complex& other) const;
            /*post: overloaded input */
            friend istream& operator>>(Complex c1, Complex c2);
            double getReal() const;
            /*post: gets the real numbers*/
            double getImag() const;
            /*post: gets the imaginary numbers*/
            void setReal(double notReal);
            /*post: sets the real numbers*/
            void setImag(double notImag);
            /*post: sets the imaginary numbers */
            double findMagnitude();
            /*post: finds the magnitude of the complex number */
        };
    }
    
    #endif
    IMPLEMENTATION FILE:
    Code:
    #include "Complex.h"
    
    namespace Evan
    {
        istream& operator>>(Complex& c1, Complex& c2)
        {
            double notReal, notImag;
            cin >> notReal >> notImag;
            c1.setReal(notReal);
            c1.setImag(notImag);
            c2.setReal(notReal);
            c2.setImag(notImag);
            return cin;
        }
    
        Complex Complex::operator+(const Complex& other) const
        {
            Complex result;
            result.real = real + other.real;
            result.imag = imag + other.imag;
            return result;
        }
    
        Complex::Complex()
        {
            real = 0;
            imag = 0;
        }
    
        Complex::Complex(double notReal, double notImag)
        {
            real = notReal;
            imag = notImag;
        }
    
        double Complex::getReal() const
        {
            return real;
        }
    
        double Complex::getImag() const
        {
            return imag;
        }
    
        void Complex::setReal(double notReal)
        {
            real = notReal;
        }
    
        void Complex::setImag(double notImag)
        {
            imag = notImag;
        }
    
        double Complex::findMagnitude()
        {
            return sqrt(real*real + imag*imag);
        }
    }
    MAIN APPLICATION FILE:
    Code:
    #include "Complex.h"
    
    using namespace Evan;
    
    int main()
    {
        double notReal, notImag;
        Complex c1, c2;
        cout << "Welcome to my crappy program." << endl;
        cout << "Enter the real and imaginary numbers separated by a space for x1: ";
        cin >> c1;
        c1.setReal(notReal);
        c1.setImag(notImag);
        cout << "Enter the real and imaginary numbers separated by a space for x2: ";
        cin >> c2;
        c2.setReal(notReal);
        c2.setImag(notImag);
        return 0;
    }
    Last edited by Evan; 02-13-2005 at 04:48 PM. Reason: New title

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    An example of the operator overload of the input istream operator.
    Code:
    // declaration in class file
    friend istream &operator>>( istream &, Tester & );
    
    // member defintion in cpp file
    // i and j are two integer variables of the class Tester
    istream &operator>>( istream &input, Tester &t )
    {
       cout << "Enter two numbers: ";
       input >> t.i >> t.j;
       return input;
    }
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Location
    Illinois
    Posts
    6
    Quote Originally Posted by bigtamscot
    An example of the operator overload of the input istream operator.
    Code:
    // declaration in class file
    friend istream &operator>>( istream &, Tester & );
    
    // member defintion in cpp file
    // i and j are two integer variables of the class Tester
    istream &operator>>( istream &input, Tester &t )
    {
       cout << "Enter two numbers: ";
       input >> t.i >> t.j;
       return input;
    }
    I need to know how to do this for 2 different sets of numbers, though. Will this work for both sets?

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Yes if you set it up right.

    Code:
    // in complex.h
    friend istream& operator>>(istream &input, Complex &c);
    
    // in your cpp file
    istream& operator>>(istream &input, Complex &c)
        {
            // since this method is a friend of Complex
            // it can set the private members directly
            // using the dot access
            input >> c.real >> c.imag;
    
            return input;
        }
    
    // now all that is needed is to call it in main for your two instances of Complex.
    
    // get first set of complex numbers
    cin >> c1;
    
    // get second set
    cin >> c2;
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Location
    Illinois
    Posts
    6
    Quote Originally Posted by bigtamscot
    Yes if you set it up right.
    Works great now. Thank you very much for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 07:09 AM
  3. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 01:26 AM
  4. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM

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