How to code a Phonebook using classes in C++ using these directions Building the
ID: 3705801 • Letter: H
Question
How to code a Phonebook using classes in C++ using these directions
Building the PhoneBook and PhoneEntry classes
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 <string> 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.
PhoneEntry class
The PhoneEntry class contains two private fields, name and number. You need to create accessor
functions and mutator functions for both of these fields. You will also need a copy constructor and an
assignment operator. You will also need an equal’s operator. These are all going to be member functions
in class PhoneEntry.
The signatures of these member functions (these are all defined in class PhoneEntry) must be:
std::string getName() const;
std::string getNumber() const;
void setName(std::string newName);
void setNumber(std::string newNumber);
const PhoneEntry& operator=(const PhoneEntry &rhs);
bool operator==(const PhoneEntry &rhs) const;
The assignment operator must copy the name and number values from the right hand side (rhs) to the
left hand side (the this pointer). The operator== will return true only if both phone entries
contain the same values for name, and the same values for number.
You also need to write the standalone function for operator<< to output the contents of a phone
entry to the output stream. This is NOT defined in class PhoneEntry, but must be included in the
PhoneEntry.h file and implemented in the PhoneEntry.cpp file.
std::ostream& operator<<(std::ostream &out, const PhoneEntry &rhs);
The output from operator<< must contain both the name and the number.
Your PhoneEntry class will need 0 argument constructor, a two argument constructor, a copy
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 constructors.
PhoneEntry();
PhoneEntry(const PhoneEntry &other);
PhoneEntry(std::string newName, std::string newNumber);
~PhoneEntry();
Note that you MUST not inline all of these functions. You can only inline a member function if it is 1 or 2
lines of code.
PhoneBook class
The PhoneBook class is more complicated. You must create two constructors, a copy constructor, and a
destructor for the PhoneBook class. Make sure you initialize the data in your object in the constructors.
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 2nd constructor will take an unsigned 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:
unsigned int maximumSize;
PhoneEntry *entries;
// …
public:
PhoneBook();
PhoneBook(unsigned int maxEntries);
PhoneBook(const PhoneBook &otherBook);
~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);
void remove(const PhoneEntry &entry);
unsigned int getSize() const;
const PhoneEntry& phoneEntryAt(unsigned int position) const;
const PhoneEntry& operator[](unsigned int position) const;
const PhoneBook& operator=(const PhoneBook &rhs);
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 remove function removes the first entry in the phone book where the name and number match
the value of the passing in PhoneEntry. Use the PhoneEntry’s == operator to do this comparison. You
must implement the same removal logic used in the other remove functions. You may want to have a
private member function that does this and is called by the three remove functions.
The getSize functions returns the number of entries currently in use. This could be anywhere from 0
to the max size specified at creation time.
Finally, the phoneEntryAt and operator[] functions return the current entry at the index passed
to the function via the position parameter. 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[].
The copy constructor and assignment operators need to make sure the object being copied from and
being copied to are not the same object. You also have to make sure the left hand side of an assignment
does not create a memory leak. You have to delete the left hand side array, create a new one, and copy
the values from the right hand side to the left hand side. The only time you can reuse the left hand side
array is if the maximum number of elements is exactly the same as the right hand side.
Again you can only inline functions that are 1 or 2 lines of code.
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. Again, you must not inline all member
functions. You should only inline a function if it is 1 or 2 lines of code.
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 (unsigned int index = 0; index < book.getSize(); index++)
{
// Create a reference to the returned PhoneEntry
const PhoneEntry &item = book.phoneEntryAt(index);
// Use the << operator display the name and number
cout << item << 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 << operator to display the name and number
cout << item << endl;
}
Remove an entry by name:
book.removeByName("John Smith");
Remove an entry by number:
book.removeByNumber("555-555-2211");
Remove an entry by PhoneEntry:
book.remove(PhoneEntry("John Smith", "555-555-2211"));
or
PhoneBook anEntry("John Smith", "555-555-2211");
book.remove(anEntry);
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 << operator you created.
You need to make sure you are using the copy constructor and the assignment operator for the
PhoneBook and PhoneEntry classes.
Be very careful that you do not have any memory leaks in your program.
Put your main function in a file called Assignment4.cpp.
Explanation / Answer
here is your files : --------->>>>>>>
PhoneEntry.h: ------->>>>>
#ifndef __PHONE_ENTRY_H
#define __PHONE_ENTRY_H
#include<iostream>
#include<string>
class PhoneEntry{
std::string name;
std::string number;
public:
PhoneEntry();
PhoneEntry(std::string n,std::string ph);
PhoneEntry(const PhoneEntry &oth);
~PhoneEntry(){}
const PhoneEntry& operator=(const PhoneEntry &oth);
bool operator==(const PhoneEntry &oth)const;
std::string getName()const{return name;}
std::string getPhone()const{return number;}
std::string setName(std::string n){name = n;}
std::string setNumber(std::string ph){if(ph.length() >= 10){number = ph;}}
friend std::ostream& operator<<(std::ostream &out,const PhoneEntry &oth);
};
#endif
PhoneEntry.cpp : ------->>>>>>>
#include "PhoneEntry.h"
PhoneEntry::PhoneEntry():name(""),number(""){
}
PhoneEntry::PhoneEntry(std::string n,std::string ph):name(n),number(ph){
}
PhoneEntry::PhoneEntry(const PhoneEntry &oth):name(oth.getName()),number(oth.getPhone()){
}
const PhoneEntry& PhoneEntry::operator=(const PhoneEntry &oth){
if(this == &oth){
return *this;
}
name = oth.getName();
number = oth.getPhone();
return *this;
}
bool PhoneEntry::operator==(const PhoneEntry &oth)const{
if(this == &oth){
return true;
}
if(name == oth.name && number == oth.number){
return true;
}
return false;
}
std::ostream& operator<<(std::ostream &out,const PhoneEntry &oth){
out<<"Name = "<<oth.getName()<<" Number = "<<oth.getPhone();
return out;
}
PhoneBook.h : -------->>>>>>>>
#ifndef __PHONE__BOOK__H
#define __PHONE__BOOK__H
#include "PhoneEntry.cpp"
class PhoneBook
{
// …
private:
unsigned int maximumSize;
PhoneEntry *entries;
unsigned int size;
// …
public:
PhoneBook();
PhoneBook(unsigned int maxEntries);
PhoneBook(const PhoneBook &otherBook);
~PhoneBook();
// …
void add(std::string name,std::string number);
void removeByName(std::string name);
void removeByNumber(std::string number);
void remove(const PhoneEntry &entry);
unsigned int getSize() const{return size;};
const PhoneEntry& phoneEntryAt(unsigned int position) const;
const PhoneEntry& operator[](unsigned int position) const;
const PhoneBook& operator=(const PhoneBook &rhs);
};
#endif
PhoneBook.cpp : --------->>>>>>>
#include "PhoneBook.h"
PhoneBook::PhoneBook(){
entries = new PhoneEntry[10];
maximumSize = 10;
size = 0;
}
PhoneBook::PhoneBook(unsigned int s){
entries = new PhoneEntry[s];
maximumSize = s;
size = 0;
}
PhoneBook::PhoneBook(const PhoneBook &otherBook){
*this = otherBook;
}
const PhoneBook& PhoneBook::operator=(const PhoneBook &rhs){
if(this == &rhs){
return *this;
}
maximumSize = rhs.maximumSize;
delete[] entries;
entries = new PhoneEntry[maximumSize];
size = rhs.size;
for(int i = 0;i<size;i++){
entries[i] = rhs.entries[i];
}
return *this;
}
PhoneBook::~PhoneBook(){
delete[] entries;
size = 0;
maximumSize = 0;
}
const PhoneEntry& PhoneBook::operator[](unsigned int position)const{
if(position >= 0 && position < size){
return entries[position];
}
}
const PhoneEntry& PhoneBook::phoneEntryAt(unsigned int position)const{
return (*this)[position];
}
void PhoneBook::add(std::string name,std::string number){
if(size >= maximumSize){
std::cout<<"Entry Full";
return;
}
entries[size++] = PhoneEntry(name,number);
}
void PhoneBook::remove(const PhoneEntry &entry){
for(int i = 0;i<size;i++){
if(entry == entries[i]){
for(int j = i;j<size-1;j++){
entries[j] = entries[j+1];
}
size--;
return;
}
}
}
void PhoneBook::removeByName(std::string name){
for(int i = 0;i<size;i++){
if(name == entries[i].getName()){
remove(entries[i]);
}
}
}
void PhoneBook::removeByNumber(std::string number){
for(int i = 0;i<size;i++){
if(number == entries[i].getPhone()){
remove(entries[i]);
}
}
}
Assignment4.cpp : ---------->>>>>>>
#include "PhoneBook.cpp"
using namespace std;
int main(){
PhoneBook book(50);
book.add("Dhananjay","1234567890");
book.add("Rahul","9812345678");
book.add("deepika","9801234567");
book.add("neha","9871234567");
std::cout<<endl;
for(int i = 0;i<book.getSize();i++){
std::cout<<book[i]<<endl;
}
std::cout<<endl<<endl;
book.remove(PhoneEntry("rahul","9812345678"));
for(int i = 0;i<book.getSize();i++){
std::cout<<book.phoneEntryAt(i)<<endl;
}
std::cout<<endl<<endl;
book.removeByName("neha");
book.removeByNumber("9801234567");
for(int i = 0;i<book.getSize();i++){
std::cout<<book.phoneEntryAt(i)<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.