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;
}