Professional Documents
Culture Documents
string get_addr();
string get_phone();
string get_relation();
void set_name(string a_name);
void set_addr(string addr);
void set_phone(string a_phone);
void set_relation(string a_relation);
private:
string name;
string address;
string phone;
string relation;
};
All member functions explain themselves well by the name of the function. The definition of the class
ContactBook should be put in the file ContactBook.h, and that of Person should be put in the file
Person.h. Your first task is to implement all the member functions of ContactBook and Person. Your
implementation of ContactBook's member function should be put in the file ContactBook.cpp, and those
of Person should be put in the file Person.cpp.
Your second task is the implement the main function. It should first read data from a text file called
contact.txt (which is in the same directory as your program).
Following shows an example of the content of the file.
Mell 51232345 Central friend
Jerry 56789876 Kennedy_Town friend
Bill 23456789 North_Point family
Eric 78949876 Jordan partner
Format: Each line of the file contains 4 fields separated by a single white space. The first field is the
name of the contact. The second field is the phone number. The third field is the address of the contact.
Note that the words in an address are joined by the character `_'. The last field is the relationship with
the contact. There is no length restriction on the length of each field. Normally, in our test cases, the
length wont exceed 30.
The number of contacts in contact.txt is no more than 50. All names are unique in the contact book.
In the main function, your program should declare an object variable of class ContactBook, and add
each contact read from contact.txt into this variable. Then, it should print a menu shown as below and
repeatedly ask the user to choose an option until the user selects ``Exits.
For each option, you need to perform the corresponding task as described below. All task specifications
are given based upon the content of contact.txt below.
1. Add a contact: void add(string name, string addr, string phone, string relation)
First, your program should let the user input the name, phone number, address and the
relationship of a new contact into your contact. (1) When adding a contact, you should check
whether the contact is already in the address book, and you should not add it again if it is
already in the book. For example, if the user wants to add Bill into the contact, you should tell
the user that Bill has already in the contact book. (2) Also, you should be aware of the number
of contacts should be controlled within 0-50. If a contact to be added is the 51th one, you
should not add it; instead, you should print the message "Too many contacts!" (3) The new
contact should be appended to the end of the existing contact list, which means that if Eric is
going to be added to the contact list shown above, it should be stored after Bill. An example is
shown here.
In this example, the user inputs Central and leader, then the relationship of Tom, Jeremy and
Morry will all be changed to leader, since their address contains the substring Central. Note
that there is no break line after wan; it breaks a line just due to the limited size of the
command window. Also, you may assume that there is no white space in the substring.
After the user selects Exit, the contact list should be well managed and updated after a sequence of
options. Then you are asked to write the new contact book into the file named new_contact.txt with
the same format as the input file (Each contact one line, with a single white space as the separator
between the fields). --- void write_to_file(const char* filename);
This last step is very important because your program will be judged based on the output file.
Sample run:
In the sample run, the input file is the same as the one we use to describe tasks shown as the second
text block at the first page.
The following is a screen shot of a sequence of options:
Important Notes:
I.
Your program must follow the formats of the sample input / output strictly.
II.
III.
IV.
The .cpp file that contains the main function must be named main.cpp. The header file of
Person class should be named Person.h. The header file of ContactBook class should be
named ContactBook.h. The implementation of these two files should be named Person.cpp
and ContactBook.cpp respectively. You must strictly follow the name of files above.
V.
Please make sure that your source code could be compiled in Code::Blocks environment
before submission.
Handin:
Submit your program electronically using the Moodle system to Assignment 5 under
Assignments section. Late submission will NOT be accepted.