Your class named
Contact should have a constructor with the information you want it to hold, i.e. name, address, etc. You would maintain a list of contacts in an array, and when the user asks to add a contact, you can reallocate the array to add a new space, then call the constructor for the pointer with the arguments, like so:
C++ Code:
class AddressBook
{
private:
Contact **contactList;
unsigned int numberOfContacts;
public:
//this is the constructor for your address book, which maintains a
//list of your contacts. Constructors and destructors would go here.
///////////////////////////////////
//this is the function that you wanted:
//the actual format of the arguments depends on how you have
//your contacts defined, i.e. what your Contact class
//contains.
bool addNewContact(char *name,char *address)
{
++numberOfContacts;
Contact **temp = (Contact **)realloc(contactList,numberOfContacts*sizeof(Contact *));
//this check is only for dummy-proofing
if(temp == NULL)
return false; //this means that we're out of memory.
else
contactList = temp;
//now that we've added a new space, we need to copy the info.
contactList[numberOfContacts - 1] = new Contact(name,address);
//creation of new contact successful.
return true;
}
};
In your program, you need to declare a variable of type
AddressBook. When the user wants to create a new contact...
C++ Code:
AddressBook *addrbk = new AddressBook();
//get user input...
//now user wants to create a new contact.
//get the appropriate information, i.e. name, address, etc....
//and now add the new contact.
addrbk->addNewContact(name,address);
The code I provided for AddressBook is the bare minimum to illustrate my example. Feel free to copy it, but make sure you code constructors, destructors, etc. If you want a somewhat more convoluted (but still relatively easy) method of doing this
without classes, just let me know.