You are going to build a simple phone book that uses two classes, PhoneEntry and
ID: 3687548 • Letter: Y
Question
You are going to build a simple phone book that uses two classes, PhoneEntry and PhoneBook. You will be creating a PhoneEntry.h file, a PhoneEntry.cpp file, a PhoneBook.h file and a PhoneBook.cpp file. Make sure your .h file include “include guards” and make sure you do NOT have any using statements in the .h files. You can have #include statements in your .h file, but not any using statements. That means you will have to fully qualify any included classes. For example, if you need the string class you will need a #include in the header file and you will have to declare your string as std::string. You will also need a driver cpp file (with your main function) that will test all of the functions of your new classes.
The PhoneEntry class contains two private fields, name and number. You need to create accessor functions and mutator functions for both of these fields.
Your PhoneEntry class will need 0 argument constructor and a destructor. Note that your destructor may not do anything, but I want you to code one. Make sure you initialize the data in your object in the constructor.
The PhoneBook class is more complicated. You must create two constructors and a destructor for the PhoneBook class. Make sure you initialize the data in your object in the constructor.
In the private data for your PhoneBook class you will have an array of PhoneEntry objects. You must be able to support N phone entries (type PhoneEntry). If you use the 0 argument constructor the PhoneBook will need to have an array large enough to store 10 phone book entries (N will be 10). The 2 nd constructor will take an int parameter that specifies the maximum number of entries in the phone book (so the parameter value on the constructor is N).
In both constructors you will need to dynamically create the array of phone book entries of the appropriate size. You will use the new [] operator to do this. You also need to make sure you delete this array in the destructor.
In your class do something similar to the following (you will need more that this in your class):
class PhoneBook { // … private: int maximumSize; PhoneEntry *entries; // … public: PhoneBook(); PhoneBook(int maxEntries); ~PhoneBook(); // … };
You also need private data to keep track of how many entries you are actually using in the array. You will need to set this to 0 in your constructor. So, your PhoneBook class will have an array of PhoneEntry objects. You will also need the following public member functions in your PhoneBook class.
void add(string name, string number);
void removeByName(string name);
void removeByNumber(string number);
int getSize() const;
const PhoneEntry& phoneEntryAt(int position) const;
const PhoneEntry& operator[](int position) const;
Function add will fill in the next available PhoneEntry with the passed in name and number. If there are no more available entries the function should just return and not add the name and number to the phone book. The removeByName function will remove the first entry in the phone book it finds that has a name that matches the passed in parameter. If no entry has that name the function does nothing.
You can remove the phone entry in one of two ways:
1. You can simply replace the entry where name is found with the current last entry in the array. You will need to decrement the count of valid entries. For example, if you find name in the entry at index 3 and there are 9 entries used in the array - you would copy the values from the entry with index 8 (the 9th entry) into the entry with index 3 (the 4th entry).
2. You can move entries in the array around to fill in the now (removed) entry. This is more complicated. If, as an example, you find name in the entry with index 3 and you currently using 9 of the 10 entries in the array you will need to copy the entry from index 4 to index 3. You will then copy entry 5 to entry 4, entry 6 to entry 5, entry 7 to entry 6, and entry 8 to entry 7. You will then decrement the number of entries used from 9 to 8. Entries 0, 1 and 2 are not modified in any way. It is your choice which of these two options you want to implement.
The removeByNumber function removes the first entry in the phone book that contains the number specified by the input parameter. Like removeByName the function should return and do nothing if the number is not found.
You must implement the same removal logic (1 or 2 above in removeByName) that you used with the removeByName function. The getSize functions returns the number of entries currently in use. This could be anywhere from 0 to 10. Finally, the phoneEntryAt and operator[] functions return the current entry at the index passed to the function via the position parameter. If position is < 0 you should return back the first entry. If position is > the current size you should return back the last valid entry. If position is valid, return back that entry. Note that there is a bug in this logic if there are no valid entries in the array. For now we will just live with the bug.
Finally, note that phoneEntryAt and operator[] functions return a reference to a PhoneEntry. This is a const reference. The programming using the PhoneEntry returned by the phoneEntryAt and operator[] functions cannot change the PhoneEntry object. You should have the operator[] function call the phoneEntryAt function if you don’t want to duplicate code in both functions. As an alternative you could have phoneEntryAt call operator[]. You need to define the PhoneEntry and PhoneBook classes in a header files and you need to put the implementation of the member functions in source files.
Examples of code using the PhoneBook and PhoneEntry. The following code snippets show the usage of the PhoneBook and PhoneEntry classes.
Create a PhoneBook: PhoneBook book(50); // phone book to store up to 50 entries Add a new entry to the PhoneBook book: book.add("Sarah Jane Smith", "555-555-1212");
List all of the entries in the PhoneBook book using phoneEntryAt:
// iterate though all of the entries for
(int index = 0; index < book.getSize(); index++) { // Create a reference to the returned PhoneEntry const PhoneEntry &item = book.phoneEntryAt(index); // Use the accessors to display the name and number cout << item.getName() << " " << item.getNumber() << endl; } List all of the entries in the PhoneBook book using subscripts: // iterate though all of the entries for (int index = 0; index < book.getSize(); index++) { // Create a reference to the returned PhoneEntry const PhoneEntry &item = book[index]; // Use the accessors to display the name and number cout << item.getName() << " " << item.getNumber() << endl; } Remove an entry by name: book.removeByName("John Smith"); Remove an entry by number: book.removeByNumber("555-555-2211");
Building the main function.
You need to create a main function. The main needs to create a PhoneBook, add entries to the PhoneBook, and remove entries from the PhoneBook. You should also loop through the entries in the PhoneBook by using the getSize and phoneEntryAt functions and you should loop though the entries in the PhoneBook by using the getSize function and subscripts []. You can then display the name and number using the accessor functions in the PhoneEntry class.
Explanation / Answer
#include<iostream>
#include<string.h>
using namespace std;
//PhoneEntry Class represents each entry in the PhoneBook
class PhoneEntry
{
//Private access variables
private:
//Name and number of contact
string myNumber;
string myName;
//Public access specifier
public:
//Setter method to set the number which is a private variable
void setNumber(string num)
{
myNumber=num;
}
//Setter method to set the name which is a private variable
void setName(string name)
{
myName=name;
}
//Getter method to get Number
string getNumber()
{
return myNumber;
}
//Getter method to get Name
string getName()
{
return myName;
}
};
//PhoneBook class represents multiple PhoneEntries
class phoneBook
{
//Private Access Specifier
private:
//Static and constant integer to represent maximum number of entries in the phonebook
static const int MAX_ENTRIES=10;
//Array of PhoneEntry objects to represent each entry in the phonebook
PhoneEntry ph[MAX_ENTRIES];
//Static variable to keep count of number of entries currently in the phonebook
static int currentCount;
//Public Access Specifier
public:
//Method to add new entry in the phoneBook
void add(string name,string number)
{
ph[currentCount].setName(name);
ph[currentCount].setNumber(number);
//Incrementing current counter to represent added entry
currentCount++;
}
//Function that returns reference to the entry at the specified position
const PhoneEntry& phoneEntryAt(int position) const
{
//If position is less than 0 return the first entry in the phonebook
if(position < 0)
return ph[0];
//If position is greter than maximum entries, return the last entry in the phonebook
else if(position > currentCount)
return ph[currentCount];
//If position is valid, then return entry at that position
else
return ph[position];
}
//Method to remove Entry by Name
void removeByName(string name)
{
int i=0,j=0;
for(i=0;i<currentCount;i++)
{
//When entry with a matching name is found
if(ph[i].getName() == name)
{
//Shift all entries one index up
for(j=0;j<currentCount;j++)
{
ph[j].setName(ph[j+1].getName());
ph[j].setNumber(ph[j+1].getNumber());
}
//Decrement current count variable to represent the removed entry
currentCount--;
}
}
}
//Method to remove entry by Number
void removeByNumber(string number)
{
int i=0,j=0;
for(i=0;i<currentCount;i++)
{
//When a matching number is found
if(ph[i].getNumber() == number)
{
//Shift all subsequent entries one index up in the array
for(j=0;j<currentCount;j++)
{
ph[j].setName(ph[j+1].getName());
ph[j].setNumber(ph[j+1].getNumber());
}
//Decrement current count variable to represent the removed entry
currentCount--;
}
}
}
//Method to return the size of the phoneBook
int getSize()
{
return currentCount;
}
};
//Declaration of static variable currentCount
int phoneBook::currentCount;
int main()
{
//Object of phoneBook class
phoneBook book;
//Object of PhoneEntry class
PhoneEntry ph;
int choice=0;
//Character to know if user wants to continue
char menuChoice='y';
//Temporary variables for storing name and number for operations
string name, number;
//Position variable for getEntryAt method
int position;
while(menuChoice == 'y')
{
cout << "1. Add an entry ";
cout << "2. Remove entry by name ";
cout << "3. Remove entry by number ";
cout << "4. Get total count ";
cout << "5. Get entry at position ";
cout << ":: ";
cin >> choice;
switch(choice)
{
case 1:
cout <<"Enter Name and Number: ";
cin >> name >> number;
book.add(name, number);
cout << " Entry Added!";
break;
case 2:
cout << "Enter Name to remove: ";
cin >> name;
book.removeByName(name);
break;
case 3:
cout << "Enter number to remove: ";
cin >> number;
book.removeByNumber(number);
break;
case 4:
cout << "Total Count: " << book.getSize();
break;
case 5:
const PhoneEntry myReturnObj;
cout << "Enter Position: ";
cin >> position;
PhoneEntry temp = book.phoneEntryAt(position);
cout << "Name: " << temp.getName();
cout << "Number: " << temp.getNumber();
break;
}
cout << " Do you want to Continue? ";
cin >> menuChoice;
}
return 0;
}
sample output
1. Add an entry
2. Remove entry by name
3. Remove entry by number
4. Get total count
5. Get entry at position
:: 1
Enter Name and Number: suganthi
7600234561
Entry Added!
Do you want to Continue? y
1. Add an entry
2. Remove entry by name
3. Remove entry by number
4. Get total count
5. Get entry at position
:: 1
Enter Name and Number: suganthi
7600234561
Entry Added!
Do you want to Continue? y
1. Add an entry
2. Remove entry by name
3. Remove entry by number
4. Get total count
5. Get entry at position
:: 4
Total Count: 1
Do you want to Continue?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.