I've read a few books on learning C++, and I have the basic idea of what OOP is all about, but I encountered a slight problem while writing a console based address book.
I'm certainly not trying to take an entirely OO approach to it, since I'm not skilled enough to make that much use of it yet (I've only been programming for about a year off and on), but I do recall something about IS_A and HAS_A relationships, which I think is the key to my question.
If I had to guess, I'd say list HAS_A contact.
I've read up about it, but I didn't quite understand. I was wondering if someone could suggest an efficient solution to the following:
Code:
class contact{
public:
contact(string *pName){
cFirstName=(*pName); //Contacts must at LEAST have a first name
}
protected:
string cFirstName;
string cLastName;
string cEmail;
string cPhoneNumber;
};
class list{
public:
list(string *newListName){
listName=(*newListName);
loaded=true; // If there's an instance, it's been loaded.
}
void unload(){
loaded=false;
return;
}
void view(){
cout<<endl
<<"Contact List: "<<listName<<endl;
for (int i=0;i<(contactList.size());++i){
cout<<(i+1)<<". "<<contactList[i].cLastName<<", "
<<contactList[i].cFirstName<<endl;
}
cout<<" "<<endl; //Instructions for viewing a specific contact in detail goes here
return;
}
protected:
bool loaded;
string listName;
vector<contact> contactList;
};
My problem, is that list::view() iterates through vector<contact> contactList displaying each contact's names in the vector. The problem with this is that the contacts names and other details are protected data, so list can't legally access them.
I could declare list as a friend of contact, but I was hoping to avoid that. Should I make functions that return the names of the contact? Or is there a better way to do this?
Thanks.