Results 1 to 2 of 2

Thread: How would u use this overloaded operator?

  1. #1

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Question How would u use this overloaded operator?

    Hello everyone!
    I was looking over this example of loading an insertion operator and a extraction operator, for file output and default console output, and i don't understand how you would use it! Here is the Code:
    PHP Code:
    // Overload the insertion operator for file output.
    fstream &operator<<(fstream &outStream, const Friends &aFriendList)
    {
       
    int i//index counter

       
    Friends::friendPtr P aFriendList.start;
       for (
    0aFriendList.numOfFriendsi++)
       {
          
    // save the friend data, one field on each line
          
    outStream <<  P->name      << endl
                    
    <<  P->telephone << endl
                    
    <<  P->sex       << endl
                    
    <<  P->age       << endl;
       }
       return 
    outStream;
    }

    //================================================================
     
    // Overload the extraction operator for file input.
    fstream &operator>>(fstream &inStreamFriends &aFriendList)
    {
       
    Friends::friendPtr  P;

       
    inStream >> ws;
       
    inStream.peek(); // Look ahead to see if it is the end of file
       
    while(!inStream.eof()) //next char is not EOF
       
    {
          
    // create a new task node
          
    = new Friends::FriendNode;

          
    // get the name
          
    inStream.getline(P->nameFriends::MAX_NAME_SIZE);
          
    // Strip leading "white space"
          
    inStream >> ws;
          
    // get the telephone number
          
    inStream.getline(P->telephoneFriends::MAX_PHONE_SIZE);
          
    // Strip leading "white space"
          
    inStream >> ws;
          
    // get the sex
          
    inStream >> P->sex;
          
    // Strip leading "white space"
          
    inStream >> ws;
          
    // get the age
          
    inStream >> P->age;
          
    // Strip leading "white space"
          
    inStream >> ws;

          
    // insert the task node into the list
          
    aFriendList.InsertFriend(P);
          
    inStream.peek(); // Look ahead to see if it is the end of file

       
    }
       return 
    inStream;
    }



    // Overload the insertion operator for default screen output.
    ostream &operator<<(ostream &outStream, const Friends &aFriendList)
    {
       
    int friendCounter  aFriendList.numOfFriends,
       
    displayCounter 0;

       
    Friends::friendPtr P aFriendList.start;

       if(
    friendCounter)
       {
          while(
    friendCounter)
          {
             
    outStream << "Friend's name   : " << P->name      << endl
                       
    << "Telephone number: " << P->telephone << endl
                       
    << "            Sex : " << P->sex       << endl
                       
    << "            Age : " << P->age       << endl 
                       
    << endl;

             
    P->next;
             
    friendCounter--;
             
    displayCounter++;
             
    displayCounter %= 4;
             if(!
    displayCounter && friendCounter)  // Only display 4 nodes to the screen at a time
             
    {
                
    outStream << "More to come.";
                
    pause();
                
    clearScreen();
             }
          }
          
    outStream << "All finished.";
          
    pause();
          
    clearScreen();
       }
       else
       {
          
    cout << "No friends to display ... " << endl;
          
    pause();
          
    clearScreen();
       }
       return 
    outStream;
    }

    //================================================================
     
    // Overload the extraction operator for default keyboard input.
    istream &operator>>(istream &inStreamFriends &aFriendList)
    {
       
    Friends::friendPtr P = new Friends::FriendNode;

       
    cout << "Friend's name         : ";
       
    inStream.getline(P->nameFriends::MAX_NAME_SIZE 1);
       
    cout << "Friend's 'phone number: ";
       
    inStream.getline(P->telephoneFriends::MAX_PHONE_SIZE 1);
       do
       {
          
    cout << "Friend's sex    (M/F) : ";
          
    inStream >> P->sex;
          
    P->sex toupper(P->sex);
       }
       while ((
    P->sex != 'M') && (P->sex != 'F'));
       
    cout << "Friend's age          : ";
       
    inStream >> P->age;
       
    inStream.ignore(1);
       
    aFriendList.InsertFriend(P);
       return 
    inStream;


    Here is the start of the class incase your wondering what Friend is.

    PHP Code:
    class Friends{

    public:  
    // Allow users access to these constants

       
    enum {MAX_NAME_SIZE 31MAX_PHONE_SIZE 15};

    private: 
    // Declare private data members


       
    struct FriendNode{

          
    char name[MAX_NAME_SIZE 1];

          
    char telephone[MAX_PHONE_SIZE 1];

          
    char sex;

          
    unsigned age;

          
    FriendNode *next;  // A pointer to the next client in the list
       
    };


       
    typedef FriendNode *friendPtr;  // To simplify declarations of pointers


       
    int numOfFriends;  // The current number of tasks in the list.

     
      
    friendPtr start;  // Pointer to the start of the list. 

    If you could give me an example so I can start using this class properly that would be great! If i didn't supply enough code, please tell me and I will post up more.
    Last edited by struntz; Jun 27th, 2002 at 12:52 AM.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Those operators are there so you can use it with cout and the other iostream objects. Like this:
    Friends fr;
    fstream fin("data.dat", ....);
    fin >> fr; // reads in all necessary information from a file
    cout << fr; // output to console
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width