Thread: iostream overloaded operators

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Hutton, Preston

    iostream overloaded operators

    hi have had a look and searched but i couldnt find anything on iostream overloaded operators

    so i was wondering if anyone on here could explain what this actually is?


  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    The stream operators are designed from the get go to operate on the built in types: int, char, float, etc... For example:

    int foo;
    cin >> foo;  // Read from cin stream into an integer
    float bar = 12.9f;
    cout << bar; // Write to cout stream converting from a float
    Overloading these operators allows you to extend this capability to user defined types (custom class/struct objects).

    #include <string>
    #include <iostream>
    using namespace std;
    struct person
        string first_name;
        string last_name;
        int age;
        person() {}
        person(const string& fname, const string& lname, int yrs) : first_name(fname), last_name(lname), age(yrs) {}
    // Overloaded stream extraction operator
    istream& operator>>(istream& is, person& per)
        return is >> per.first_name >> per.last_name >> per.age;
    // Overloaded stream insertion operator
    ostream& operator<<(ostream& os, const person& per)
        return os << "First Name: " << per.first_name << "\nLast Name : " << per.last_name
                  << "\nAge       : " << per.age;
    int main()
        person h_simpson("Homer","Simpson",40);
        //Output to cout using overloaded stream insertion operator<< for custom object of type person
        cout << h_simpson << endl;
        person temp;
        // Read from cin using overloaded stream extraction operator>> for custom object of type person
        cin >> temp;
        cout << temp << endl;
        return 0;
    Example I/O:
    First Name: Homer
    Last Name : Simpson
    Age       : 40
    Marge Simpson 38
    First Name: Marge
    Last Name : Simpson
    Age       : 38
    Last edited by hk_mp5kpdw; 08-10-2007 at 05:40 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Problem with overloaded operators, templates and inheritance
    By bleakcabal in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2004, 05:07 AM
  3. Polymorphism & Overloaded Operators :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 08:40 PM
  4. overloaded operators
    By ivandn in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2001, 03:52 PM
  5. help with overloaded operators
    By doleman19 in forum C++ Programming
    Replies: 23
    Last Post: 10-23-2001, 02:40 AM