I understand the concept but i don't know when to use and what my teacher expects when he says overload certain operators.

Dani AI

Generated

Operator overloads belong where an operator's usual meaning cleanly matches the type and makes client code clearer, not as a shortcut to avoid writing a clear function. Echoing : prefer meaningful overloads. For I/O it's common to provide stream operators so custom types plug into iostreams; that idea is implicit in 's comment and the resource mentioned.

The assignment described — read a single-line phrase containing spaces and produce one word per output line — can be solved two ways. If the exercise does not explicitly require overloading, reading a full line with the standard library and then splitting is the simplest, least-surprising design. If the task specifically asks for an overloaded extraction operator, implement a non-member operator>> that uses std::getline to consume the whole input line and then tokenizes it into the object's internal representation. That keeps stream semantics consistent (the newline is consumed) and lets code like stream >> phraseObj; work for both files and cin.

Example implementation (minimal):

#include <string>
#include <vector>
#include <sstream>
#include <istream>

struct Phrase {
    std::vector<std::string> words;
};

std::istream& operator>>(std::istream& is, Phrase& p) {
    p.words.clear();
    std::string line;
    if (!std::getline(is, line)) return is;
    std::istringstream iss(line);
    std::string w;
    while (iss >> w) p.words.push_back(w);
    return is;
}

Notes and cautions: keep operator>> as a non-member that returns the stream, check stream state, and document any nonstandard behavior (for example, reading a whole line instead of a token). Be careful mixing extraction operators and raw getline calls elsewhere (manage leftover newlines or use std::ws/ignore). This approach satisfies an overloading requirement while preserving clear, predictable I/O behavior for future readers of the code.

Recommended Answers

All 7 Replies

Normally overload operators so that you can do mathematical or other operations with c++ classes. for example: cout << MyClass; assuming MyClass is an instance of some c++ class and you want that class to print the value of its class variables to the screen. Or you might want to add them MyClass++; There are lots of things you can do with overloaded operators, those are just two of them.

Which operators to overload depends on the c++ class and what you want it to do.

You overload an operator when it makes sense to do so.

Say that you create a custom string class MyString.

It makes sense to overload the + operator, but not the ~.

i have a project which i am working on. I am supposed to read in a phrase then every time it encounters a space the word is supposed to go to a newline in my output. The phrase can take up only one line. The phrase can be read from a file or from cin. The phrase contains spaces so i am guessing you are supposed to overload input >> to make it take a space containing line when they enter cin>>string word. by overloading with getline()???

Well try it :)

i have tried it just not sure why he wants me to overload the >> operator.

so basically here i want the buffer to continue to read until it hits the \n character?

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.