Thread: Can't figure out a line of code...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Can't figure out a line of code...

    My teacher gave the class a address book program to study and run. I'm unclear about this line below....

    istream & operator >> (istream &stream, Pessoa &p)

    There are brackets with code below it but I've never seen a function like this. What is the term "operator" and >> doing here exactly?

    I'm in Brazil so it's in portuguese, sorry about that.
    The whole program is below and runs fine.





    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream.h>
    #include <string.h>
    
    using namespace std;
    
    class Pessoa
    {
          public :
                 char ultimonome[11];
                 char primeironome[11];
                 char endereco[16];
                 char cidade[16];
                 char estado[3];
                 char CEP[10];
                 
                 Pessoa(); //construtor
                 void escrevebuffer(ostream&,Pessoa&);
                 };
                 
          Pessoa::Pessoa()
          {
           ultimonome[0]=0;
           primeironome[0]=0;
           endereco[0]=0;
           cidade[0]=0;
           estado[0]=0;
           CEP[0]=0;
          }
          
    istream & operator >> (istream &stream, Pessoa &p)
    {
            cout<<"\nEntre com o ultimo nome, ou digite <enter> para finalizar :"<<flush;
            stream.getline(p.ultimonome,30);
            if(strlen(p.ultimonome)==0)
            return stream;
            cout<<"\nEntre com o primeiro nome : "<<flush;
            stream.getline(p.primeironome,30);
            cout<<"\nEntre com o endereco : "<<flush;
            stream.getline(p.ultimonome,30);
            cout<<"\nEntre com a cidade : "<<flush;
            stream.getline(p.cidade,30);
            cout<<"\nEntre com o estado : "<<flush;
            stream.getline(p.estado,15);
            cout<<"\nEntre com o CEP : "<<flush;
            stream.getline(p.CEP,10);
            return stream;
    }
    
    const int tambuffer=200;
    void Pessoa::escrevebuffer(ostream &stream, Pessoa &p)
    {
         char buffer[tambuffer];
         strcpy(buffer,p.ultimonome);
         strcat(buffer,"|");
         strcpy(buffer,p.primeironome);
         strcat(buffer,"|");
         strcpy(buffer,p.endereco);
         strcat(buffer,"|");
         strcpy(buffer,p.cidade);
         strcat(buffer,"|");
         strcpy(buffer,p.estado);
         strcat(buffer,"|");
         strcpy(buffer,p.CEP);
         strcat(buffer,"|");
         int tam=strlen(buffer);
         stream<<tam;
         stream.write((char*)&tam,sizeof(tam));
         stream.write(buffer,tam);
    }
    
    int main(void)
    {
        char nomearq[20];
        Pessoa p;
        cout<<"\Entre com o nome do arquivo: "<<flush;
        cin.getline(nomearq,19);
        ofstream stream(nomearq,ios::out);
        if(stream.fail())
         {
          cout<<"\nErro na abertura";
          return 0;
         }
        while(1)
        {
         cin>>p;
         if(strlen(p.ultimonome)==0)
          break;
         p.escrevebuffer(stream,p);
        }
        return 0;
    }

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Imposing Line Numbers automatically in the C code
    By cavestine in forum C Programming
    Replies: 14
    Last Post: 10-15-2007, 12:41 AM
  2. What does this line of code mean?
    By diagonalArg in forum C++ Programming
    Replies: 3
    Last Post: 07-28-2005, 12:59 PM
  3. Need the missing line of code
    By mayhem in forum C Programming
    Replies: 3
    Last Post: 06-20-2005, 04:21 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM