Question: Has-a relationships, where one class uses another class as a member variable, are very commonplace in object-oriented programs. Often has-a relationships extend deeply into multiple
Has-a relationships, where one class uses another class as a member variable, are very commonplace in object-oriented programs. Often has-a relationships extend deeply into multiple layers of classes that have classes in this kind of a relationship. Now that you have had the opportunity to learn about has-a relationships between objects it is time to demonstrate your knowledge by creating a program that utilizes this type of relationship.
Modify the Vector address book program you created in the previous module by creating a Person Class, an Employer Class and a Personal Info Class. Use the Employer and PersonalInfo classes as member variables of a Person class. Modify the Vector class to hold People objects instead of Record objects. Modify the menu interface and input algorithms to accept data for the Employer and Personal Info classes via functions in the Person class. Modify the Output to print all the information in each Person object, associated Employer object, and associated Personal Info object. Make sure to separate the implementation of your classes from the header files.
This is what I have:
#include
using namespace std; class contact { //Declare all variables long ph; char name[20],add[20],email[30]; // Create a contact module to add information about the individual. public:
void create_contact() { cout<<"Phone: "; cin>>ph;
cout<<"Name: "; cin.ignore(); cin>>name;
cout<<"Address: "; cin.ignore(); cin>>add;
cout<<"Email address: "; cin.ignore(); cin>>email; cout<<" "; } // DIsplay customer information void show_contact() { cout< void operator = (contact a){ ph = a.ph; strcpy(name,a.name); strcpy(add, a.add); strcpy(email,a.email); } char* getName() { return name; } char* getAddress() { return add; } char* getEmail() { return email; } }; fstream fp; contact cont; //Save customer information to the array void write_contacts(vector void show_all_contacts(vector void show_all_contacts() { //system("clear"); cout<<" \t\t-------------------------------- \t\t\tLIST OF CUSTOMERS \t\t-------------------------------- "; fp.open("contactBook.dat",ios::in); while(fp.read((char*)&cont,sizeof(contact))) { cont.show_contact(); cout< void display_contact(int num) { bool found; int ch; found=false; fp.open("contactBook.dat",ios::in); while(fp.read((char*)&cont,sizeof(contact))) { if(cont.getPhone()==num) { system("clear"); cont.show_contact(); found=true; } } fp.close(); if(found == false) { cout<<" No results found. Please try again.";} getchar(); } void display_contact(vector for (int i = 0; i } void edit_contact(vector for(int i = 0; i < data.size(); i++) { if(data[i].getPhone()==num) { data[i].show_contact(); cout<<" Please enter the new customer information: "< //Edit the customer information to update/correct information void edit_contact() { int num; bool found=false; system("clear"); cout<<"Edit customer ------------------------------- \tEnter the number of the customer that you wish to change:"; cin>>num; fp.open("contactBook.dat",ios::in|ios::out); while(fp.read((char*)&cont,sizeof(contact)) && found==false) { if(cont.getPhone()==num) { cont.show_contact(); cout<<" Please enter the new customer information: "< void delete_contact(vector int num; system("clear"); cout< for (int i = 0 ; i //Delete a customer from the array void delete_contact() { int num; system("clear"); cout< void read_contact(vector fp.open("contactBook.dat",ios::in); if (fp != NULL){ cout << "Error opening file "; return; } int count = 0; contact cont; while(fp.read((char*)&cont,sizeof(contact))) data.push_back(cont); } void add_contact(vector contact cont; cont.create_contact(); data.push_back(cont); cout< } //Main module for the menu int main(int argc, char *argv[]) { vector cout<<"\t\t\t\t-\t-"; cout<<"\t\t\t\t--\t--"; cout<<"\t\t\t\t---\t---"; cout<<"\t\t\t\t----\t----"; cout<<"\t\t\t\t-----\t-----"; cout<<"\t\t\t\t------\t------"; cout<<"\t\t\t\t-------\t-------"; cout<<"\t\t\t\t------\t-------"; cout<<"\t\t\t\t------\t------"; cout<<"\t\t\t\t-----\t-----"; cout<<"\t\t\t\t----\t----"; cout<<"\t\t\t\t---\t---"; cout<<"\t\t\t\t--\t--"; cout<<"\t\t\t\t-\t-"; read_contact(data); for(;;) { int ch; cout<<" \t ---- Welcome to the Contact Management System ----"; cout<<" \t\t\tMAIN MENU \t\t---------------------- \t\t[1] Add a new customer \t\t[2] List all customers \t\t[3] Search for a customer \t\t[4] Edit a customer \t\t[5] Delete a customer \t\t[0] Exit \t\t------------------ \t\t"; cout<<"Enter your choice:"; cin>>ch; switch(ch) { case 0: cout<<" \t\tHave a great day!"; write_contacts(data); exit(0); break; break; break; case 1:add_contact(data); break; case 2:show_all_contacts(data); break; case 3: int num; system("clear"); cout<<" \tPhone: "; cin>>num; display_contact(data,num); break; case 4:edit_contact(data); break; case 5:delete_contact(data); break; default: break; } int opt; cout<<" Enter your choice: \t[1] Main Menu\t\t[0] Exit "; cin>>opt; switch (opt) { case 1: system("clear"); continue; case 0: write_contacts(data); exit(0); } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
