I'm trying to create an empty array of objects of the class Contact. Starting with an empty array, I want to create a function in AddrBook.cpp to add objects of Contact to the object array, called addressBook.
Am I initializing the array properly in AddrBook.h?
How do I check to see if an object of Contact exists at a specific index?
AddrBook.cpp
#include "AddrBook.h"
namespace address_book_test
{
const int CAPACITY = 5;
void AddrBook::addContact(Contact& itemToAdd) // Add Contact to the AddrBook (using Contact object)
{
for (int i = 0; i < CAPACITY; i++)
{
if (/*Contact object does not exist at i*/)
{
/*Add Contact object*/
return;
}
}
return;
}
...
}
AddrBook.h
#ifndef ADDR_BOOK_H
#define ADDR_BOOK_H
#include <fstream>
using namespace std;
#include "Contact.h"
namespace address_book_test
{
class AddrBook
{
public:
static const int CAPACITY = 5;
// CONSTRUCTOR
AddrBook() { used = 0; }
// Modification Member Functions
void addContact(Contact& itemToAdd); // Add Contact to the AddrBook (using Contact object)
...
private:
static Contact addressBook[CAPACITY]; // The array used to store Contact objects
int used; // How much of addressBook is used
};
}
#endif
Contact.cpp
#ifndef CONTACT_H
#define CONTACT_H
#include <fstream>
using namespace std;
#include "Address.h"
#include "Name.h"
namespace address_book_test
{
class Contact
{
public:
// Constructor
Contact(string inLastName = "",
string inFirstName = "",
string inStreetAddress = "",
string inCity = "",
string inState = "",
string inZip = "",
string inPhone = "",
string inEmail = "",
string inBirthday = "",
string inPictureFile = "")
{
Name(inLastName, inFirstName);
Address(inStreetAddress, inCity, inState, inZip);
setPhone(inPhone);
setEmail(inEmail);
setBirthday(inBirthday);
setPictureFile(inPictureFile);
}
...
private:
Name fullName;
Address fullAddress;
string phone;
string email;
string birthday;
string pictureFile;
};
}
#endif
CAPACITYnumber of elements. If you require the size to change use anstd::vector.i. The only thing you can do is to overwrite it with a copy.