So I'm just having a super fun time with overloaded operators and the friend function. I have an addressbook, and for my assignment I have to use the friend function to overload a few different operators. I think I understand how to type the friend function out to tie it to my class, and how to utilize it in my associated .cpp file, but I'm not all that sure as to how to do what the assignment is asking as far as the actual work these things should be doing. I'll post my code and assignment requirements so you can see where I'm at and hopefully somebody can guide me in what I need to do. I kind of have an idea of what needs to be done, but I don't know what the code is supposed to look like as far as looking into whats in the addressbook already and how its accessed.

Here is my header file :

#ifndef _ADDRESSBOOK
#define _ADDRESSBOOK
#include <vector>
#include <iostream>
using namespace std;

using std::istream;
using std::ostream;

const int MAXADDRESS =25;

struct PERSON
{
 char fName[25];
 char lName[25];
 char Address[100];
};

class addressBook
{
private:
vector<PERSON> people;

 int head;
 int tail;

public:

 addressBook();

 addressBook(const PERSON &p);

 addressBook(const PERSON p[], int size);

 addressBook(char *fName, char *lName, char *address);



 bool addPerson(const PERSON &p);
  bool sortcomp(const PERSON& p1, const PERSON& p2);
 bool getPerson(PERSON &p);
 bool findPerson(char *lastName, PERSON &p);
 bool findPerson(char *lastName, char *firstName, PERSON &p);
 void bubbleSort(int *array,int length);
 void printBook();
 void sort();

friend ostream &operator << (ostream &, addressBook &);
friend istream &operator >> (istream &, addressBook &);

};
#endif

**Here is my addressBook.cpp file : **

#include <iostream>
#include "addressBook.h"
using namespace std;

addressBook::addressBook()
: head(0), tail(-1)
{

}

addressBook::addressBook(const PERSON &p)
: head(0), tail(-1)
{
 addPerson(p);
}

addressBook::addressBook(const PERSON p[], int size)
: head(0), tail(-1)
{
 for(int i = 0; i < size; i++)
 addPerson(p[i]);

}

addressBook::addressBook(char *fName, char *lName, char *address)
: head(0), tail(-1)
{
 PERSON tmp;
 strcpy(tmp.fName, fName);
 strcpy(tmp.lName,lName);
 strcpy(tmp.Address, address);
 addPerson(tmp);
}

bool addressBook::addPerson(const PERSON &p)
{


 people.push_back(p);

 if(tail == -1)
 tail++;
 return true;

}
bool addressBook::getPerson(PERSON &p)
{
 if(tail >=0)
 {
 if(tail >= people.size())
 tail = 0;
 p = people[tail];
 tail++;
 return true;
 }
 return false;
}
bool addressBook::findPerson(char *lastName, PERSON &p)
{
 for(int i = 0; i < people.size(); i++)
 {
 if(!stricmp(people[i].lName, lastName))
 {
 p = people[i];
 return true;
 }
 }
 return false;
}
bool addressBook::findPerson(char *lastName, char *firstName, PERSON &p)
{
 for(int i = 0; i < people.size(); i++)
 {
 if(!stricmp(people[i].lName, lastName) && !stricmp(people[i].fName, firstName))
 {
 p = people[i];
 return true;
 }
 }
 return false;
}


void addressBook::printBook()
{
 for(int i = 0; i < people.size(); i++)
 {

 cout << people[i].fName << "\t" << people[i].lName << "\t" << people[i].Address << endl;
 }
}

 bool addressBook::sortcomp(const PERSON& p1, const PERSON& p2)
{
    int i;
    int result = std::strcmp(p1.lName, p2.lName) ;

    if ( result > 0 )
        return true ;
    if ( result < 0 )
        return false ;
    return std::strcmp(p1.fName, p2.fName) > 0 ;

     for(i=0;i< people.size();i++)
    {
       for ( unsigned i=1; i< people.size(); ++i )
            if ( sortcomp(people[i-1], people[i]) )
            {
                std::swap(people[i-1], people[i]) ;
                result = true ;
            }
            sort();

}
 }

ostream &operator << (ostream &output, addressBook &ab)
    {

        addressBook ad;
        ad.sort();


        return output << ad;

}

**Here is my Main.cpp file : **

#include <iostream>
#include <cstdlib>
#include <conio.h>
#include "addressBook.h"


using namespace std;
int printMenu();
void waitKey();

const int ADDPERSON = 1;
const int GETPERSON = 2;
const int FINDLAST = 3;
const int FINDBOTH = 4;
const int PRINT = 5;
const int NAMESORT = 6;
const int EXIT = 0;


int main()
{
    PERSON p;
    addressBook ad;
    addressBook myBook;
    addressBook newBook;
    addressBook newerbook;



    PERSON me = {"Johnny", "Rocket", "923 go"};


    ad.addPerson(me);


    addressBook newbook("TEST", "TEST2", "1234");



int selection;



bool status;
char lName[50];
char fName[50];


        selection = printMenu();
        while(selection != EXIT )
        {
        switch(selection)
            {

            case ADDPERSON :
                cout << "Enter First Name " << endl;
                cin >> p.fName;
                cout << "Enter last Name " << endl;
                cin >> p.lName;
                cout << "Enter Address " << endl;
                cin >> p.Address;
                status = ad.addPerson(p);
                if(status == false)
                    cout << "Sorry There is no more room in the address book " << endl;
                else
                    cout << "Thanks for your Entry " << endl;

                waitKey();  
                break;
            case GETPERSON :
                status = ad.getPerson(p);
                if(status)
                    cout << p.fName << "\t" << p.lName << " " << p.Address << endl;
                else
                    cout << "Sorry The address book is empty " << endl;

                waitKey();

                break;
            case FINDLAST :
                cout << "Enter a last name " << endl;
                cin >> lName;
                status = ad.findPerson(lName,p);
                if(status)
                        cout << p.fName << "\t" << p.lName << " " << p.Address << endl;
                else
                    cout << "Sorry, Name not found " << endl;

                waitKey();
                break;

            case FINDBOTH :
                cout << "Enter last name " << endl;
                cin >> lName;
                cout << "Enter first name " << endl;
                cin >> fName;
                status = ad.findPerson(lName, fName,p);
                if(status)
                    cout << p.fName << "\t" << p.lName << " " << p.Address << endl;
                else
                    cout << "Sorry, Name not found " << endl;

                waitKey();
                break;

            case NAMESORT :
                ad.sort();
                newbook.sort();
                myBook.sort();
                cout << "Your addressbook has been alphabetically sorted, choose print from the main menu to see the results " << endl;
                waitKey();
                break;

            case PRINT :

                newbook.printBook();
                ad.printBook();
                myBook.printBook();
                waitKey();
                break;
            case EXIT :
                cout << "Thanks for using the address book " << endl;
                exit(0);
        }
            selection = printMenu();
        }
};

int printMenu()
{


int selection;

    system("CLS");
    cout << "1. Add A Person" << endl;
    cout << "2. Get A Person " << endl;
    cout << "3. Find A person By Last Name " << endl;
    cout << "4. Find A person By First and Last Name " << endl;
    cout << "5. Print the address book " << endl;
    cout << "6. Sort by last name " << endl;
    cout << "0. Exit this program " << endl;
    cin >> selection;

    return selection;

};

void waitKey()
{

    cout << "Press a key to continue " << endl;
    while(!kbhit())
        ;

    getch();
    fflush(stdin);

};

And finally, here is my assingment :

Enhance your address book with the following functionality:
1.) Use friend function to overload the stream insertion '<<' operator, so that you can directly print out the contents of the next person in the address book. You should set this up so that after the last person in the address book is printed, then printing will start over from the beginning.
AddressBook ab;
cout << ab; //Should print the first person in the book
cout << ab; //Should print the second person in the book

Overload the '+=' so that a new PERSON can be added to the end of the address book.

PERSON p = {"Bob", "Roberts", "123 Anywhere Rd."};
ab+= p; //Adds Bob to the end of the address book

Overload the '[]' operator so that a person can directly get at the element of the address book they are searching for.
PERSON p;
p = ab[3]; // Assigns the 4th person in the addressBook to p

I think at this point, my biggest problem is that I have no idea how to call into the addressbook and request it to print out 1 name at a time. I think I have the general syntax for the friend function and operator overload worked out, so any help with how to call up the names from problem 1 would be much appreciated, beacuse I can't figure out how to do it, thanks!

Well if << is supposed to print oput the next record untill the end then restart then you can have a member variable in you class that keeps track of what record was printed last. Then in your << operator print the next record and increment the tracking variable. You will also need to check and see if you are at the end of the list and if you are then restart the tracking variable to 0. You will want to do this inside your << operator function.

Well, I managed to get the second part of the assingment to print out, and then I realized I'm not supposed to use the friend function to do it..oi. At least I got it to work though, thats something positive! Here's what I have so far in my addressbook.cpp file :

ostream &operator << (ostream &output, addressBook &ab)
    {
        PERSON me2 = {"CJ", "MZ", "123"};
        ab.addPerson(me2);


        return output;

};

Now I just have to figure out how to switch it over to a regular overloaded operator.

So by member variable, I'm guessing you mean something like printBook();, and then somehow reference it though the friend function? I'm six years between my level 1 and level two classes, (they wouldnt let me retake level 1), so I'm admittedly a bit slow on some stuff. Is there a name for the type of function,(or code), that monitors stuff being printed out so I can look into how its done? Thanks for the help!

Ok, for the second part, can somebody tell me if this looks right? It rints out at the end of the list like its required, but I have to do the function in my .cpp, and the rest in my main.
My addressbook.cpp bit :

addressBook addressBook::operator +=(const PERSON &p)
{
    addressBook ad;

    addPerson(p);

        return ad;
}

Heres my main :

    PERSON me2 = {"CJ", "MZ", "123"};
ad+=me2;

So did I do that the right way?

Edited 3 Years Ago by xanthian23: forgot code

This article has been dead for over six months. Start a new discussion instead.