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 (i = 0; i < aFriendList.numOfFriends; i++)
{
// 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 &inStream, Friends &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
P = new Friends::FriendNode;
// get the name
inStream.getline(P->name, Friends::MAX_NAME_SIZE);
// Strip leading "white space"
inStream >> ws;
// get the telephone number
inStream.getline(P->telephone, Friends::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 = 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 &inStream, Friends &aFriendList)
{
Friends::friendPtr P = new Friends::FriendNode;
cout << "Friend's name : ";
inStream.getline(P->name, Friends::MAX_NAME_SIZE - 1);
cout << "Friend's 'phone number: ";
inStream.getline(P->telephone, Friends::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 = 31, MAX_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.