1

I'm in a programming class and need overloading explained to me. Simple question so hopefully I'll get an answer pretty quick. My understanding is that overloading an operator allows it to be used on a class. If that is true, then how would I overload >> to work with a class? I'm working on a small program to test out this idea and i'll post it here

#include <iostream>
#include <cstdlib>
#include "data.h"

using namespace std;

int main() 
{
    data obj;

    cout << "What is the Number?" << endl;
    cin >> obj;
    system("pause");
    return 0;
}

class data
{
    public:
    data operator >> (int);

    private:

};
David G
  • 94,763
  • 41
  • 167
  • 253
jrainey
  • 23
  • 4
  • 1
    show us your code for `class data`. – Walter Nov 01 '13 at 21:48
  • What do you want to do with `data` and what are you using `>>` for? – David G Nov 01 '13 at 21:51
  • I'm just trying to understand overloading. I'm trying to read into a variable that I make in a class by using overloading to do so. It's part of a project in which I have to use overloading to read in, output, compare, and ++/-- a day. It's just to prove I know what overloading is – jrainey Nov 01 '13 at 21:54
  • @jrainey forget about operator overloading for minute. Do you understand how just "overloading" as a *concept* works? Ex: `void foo(float); void foo(const MyObjectClass&); void foo(long);`? – WhozCraig Nov 01 '13 at 22:04
  • Don't mean to sound like a jerk, but why do you not feel comfortable asking your lecturer? If you'd spent the same amount of time preping a way to ask them the question you asked here, you'd not only have your answer but probably gained brownie points with the lecturer to boot. – kfsone Nov 01 '13 at 22:14

4 Answers4

1

case1: no need to access private data

data.h.

class data {
  public:
    int i;
};

std::ostream& operator>> (std::istream&, data&); // better make operator >>
                                                 // a nonmember function 
                                                 // if it doesn't need access
                                                 // to private data

data.cpp

#include "data.h"

std::istream& operator>> (std::istream& is, data& d) {
  is>>d.i;    // here we do some logic, we do what it means to do >> on
  return is;  // an instance of your data class and return reference to istream
}

case2: there is a need to access private data

data.h.

class data {
  private:
    int i;
  friend std::ostream& operator>> (std::istream&, data&);
};

data.cpp

#include "data.h"

std::istream& operator>> (std::istream& is, data& d) {
  is>>d.i;    // here we do some logic, we do what it means to do >> on
  return is;  // an instance of your data class and return reference to istream
}
4pie0
  • 29,204
  • 9
  • 82
  • 118
1

This page tells you mostly everything you need to know about operator overloading.

In short, nearly every operator in C++ can be overloaded for user-defined types. Some operators, like +, -, or >> must be defined outside of a class since they are free-standing, whereas others like copy assignment (=), must be defined within.

For your case, overloading the >> operator can be done in the following manner:

istream& operator>>(istream& in, data& d){
    // Code here
    return in;
}

Where it says "Code here", place the code you need to read into the data object.

For example, let us pretend that we were reading into a Point object with an x coordinate and a y coordinate. It is formatted in the stream like so: "(x,y)". The operator overload might look like this:

istream& operator>>(istream& in, Point& p){
    char c;
    in >> c;
    if(c != '('){
        in.clear(ios_base::failbit);
        return in;
    }
    in >> p.x >> c >> p.y >> c;
    return in;
}

This is just an example with minimal format checking, but hopefully it is enough to get you started.

Note that if members in your class are private, then you should friend the istream operator in the class definition:

class data{
    ...
public:
    friend istream& operator>>(istream&, data&);
}
Community
  • 1
  • 1
Ardeol
  • 663
  • 1
  • 5
  • 9
1

If you want to bolster your understanding of what operator overloading is, consider that essentially all operators on objects (such as "+", "++", "==", "!=", etc) are member functions.

Challenge yourself to recognize Obj a, b; a = b; as Obj a; Obj b; a.operator=(b);.

Overloading is purely providing a non-default implementation.

Here is a [terrible] overload of the cast-to-const-char* operator:

class BadWolf {
    const char* m_text;
public:
    BadWolf(const char* text) : m_text(text) {}

    // if you really want the original text and ask for it with this function...
    const char* tardisTranslation() const { return m_text; }

    // but if you try to use me as a const char*...
    operator const char* () const { return "bad wolf"; }
};

int main(int argc, const char* argv[]) {
    BadWolf message("hello, sweetie");

    std::cout << "Message reads: " << (const char*)message << std::endl;
    std::cout << "Tardis translation: " << message.tardisTranslaction() << std::endl;

    return 0;
}
kfsone
  • 23,617
  • 2
  • 42
  • 74
0

I think this is what you want.

class data
{
    friend istream& operator>>( istream&, data& );

    private:
        int data;
};

istream& operator>>( istream& in, data& d )
{
    return in >> d.data;
}
clcto
  • 9,530
  • 20
  • 42