Operator Overloading
This assignment demonstrates overloaded operators to make usage of the class objects similar to usage of the standard built in types, such as int or double.
Create an address book application. It must use the following AddressBook class. This comprises the contents of the header file AddressBook.h. For full credit on this assignment you must use this header file (it is OK to use the string type instead of character arrays).
const int ENTRY_SZ = 256;
class AddressBook
{
private:
char firstName_[ENTRY_SZ];
char lastName_[ENTRY_SZ];
int streetNum_;
char streetName_[ENTRY_SZ];
char city_[ENTRY_SZ];
char state_[ENTRY_SZ];
int zipCode_;
public:
static int entryCnt_;
static char filePath_[ENTRY_SZ];
void SetFirstName(const char fName[]);
void SetLastName(const char buff[]);
void SetStreetNum(const char buff[]);
void SetStreetNum(int num);
void SetStreetName(const char buff[]);
void SetCity(const char buff[]);
void SetState(const char buff[]);
void SetZipCode(const char buff[]);
void SetZipCode(int zip);
// Copies some properties into out arguments
void GetFirstName(char buff[], int sz) const;
void GetLastName(char buff[], int sz) const;
void AddEntryFromConsole();
void PrintToConsole();
void AppendToFile();
void operator=(const AddressBook& obj);
};
bool operator== (const AddressBook& obj1, const AddressBook& obj2);
Note that the overloaded assignment operator ‘=’ is a member function (of the class AddresssBook) and the overloaded “is equal” operator ‘==’ is not a member function. This is done to demonstrate both methods. Also note that they are each binary operators. That means they take two operands, one on each side of the operator.
Remember that the static class data must be initialized in the .cpp file like this:
int AddressBook::entryCnt_ = 0;
char AddressBook::filePath_ = "";
This application should display a console menu that looks like this:
a) Open an address book file
b) Add a new address book entry
c) Print the contents of current address book
d) Quit
The program must use a fixed array of AdressBook objects and size it to 1000 elements. The code should look like this:
const int ADDR_BOOK_SZ = 1000;
AdressBook addrBook[ADDR_BOOK_SZ];
Input error handling is not required for this assignment. For example, if the user types an incorrect file path, the behavior may remain undefined (I won’t test this case).
When a) is selected, the program must prompt the user to enter a file path for the address book file. It should open the file, read in the 7 lines of data for each entry into the next element of addrBook array. Reading of the file should continue until there are no more entries left in the file.
When calling this option a second time (or more), the address book data from the previous file read should be deleted. If the input file has 4 records in it, and a) is selected a second time with a different input file that has 2 records, the address book array should only contain the 2 records from the last input file.
Make sure to add the following clear statement each time after closing any file (otherwise, some compilers, like mine, will not allow the file to be reopened):
inFile.close();
inFile.clear(std::ios_base::goodbit);
The b) option should prompt the user to enter the 7 lines for a new address book entry contained within the AddEntryFromConsole() method. This method should first check to see if an address book file has been opened yet through selection a). A function in the main file should also verify that the address book entry does not already exist by using the overloaded “==” operator. The overloaded “==” should compare the first and last names of the objects only. If it does not already exist, add that entry to the next element in the addrBook array using the overloaded assignment operator, ‘=’, and also append it to the address book file using the AppendToFile() method.
The c) option should print the current contents of the address book array, which should match the contents of the address book file. It should also print the number of entries currently contained.
Usage Examples:
User entries are bolded:
Please make a choice:
a) Open an address book file
b) Add a new address book entry
c) Print the contents of current address book
d) Quit
a
Enter path to address book file: C:\Miscellaneous\CS162\asgmt01\addressBook.txt
4 records read from the address book file.
Please make a choice:
a) Open an address book file
b) Add a new address book entry
c) Print the contents of current address book
d) Quit
b
Enter the first name: Easter
Enter the last name: Bunny
Enter the street number: 5678
Enter the street name: Jelly Bean Lane
Enter the city: FrostyTown
Enter the state: ID
Enter the zip code: 00001
Please make a choice:
a) Open an address book file
b) Add a new address book entry
c) Print the contents of current address book
d) Quit
b
Enter the first name: Easter
Enter the last name: Bunny
Enter the street number: 44
Enter the street name: rtrt
Enter the city: erer
Enter the state: qq
Enter the zip code: 444
This is a duplicate entry and will not be added to the address book.
Please make a choice:
a) Open an address book file
b) Add a new address book entry
c) Print the contents of current address book
d) Quit
c
Santa
Claus
1234
Snowy Lane
North Pole
High Up
11111
Herman
Munster
1313
Mockingbird Lane
Some City
Some State
66666
Fred
Jones
2323
Elm Street
Springfield
Oklahoma
77123
Larry
Johnson
5678
Bumble Boulevard
Los Angeles
CA
874532
There are 4 address book entries.
Please make a choice:
a) Open an address book file
b) Add a new address book entry
c) Print the contents of current address book
d) Quit
File Organization:
Put the AddressBook class declaration in a header file called AddressBook.h. Put the AddressBook class definition in a source file called AddressBook.cpp. Put the main program and all non-class functions into a main file called main.cpp.
Deliverables:
Upload the AddressBook.cpp, AddressBook.h, and main.cpp files only. Any other files, including zip files, will not be accepted.