Hello,
I'm trying to create a simple phonebook that uses two classes, one that holds the entires, the name and phone number and then a phonebook class which is a collection of the entires. I want to use a dynamic array instead of a vector to hold the entires. I would also like the entires to be inserted into the phonebook in an alphabetical order and for the phonebooks to add and subtract each other. So I guess if I want to insert a new entry to a phonebook, I would have to rezise my dynamic array, then move each entry that's already inside back a spot until the next entry is smaller or the letter in this case comes before the one I want to add right now. That is just inserting a new entry, but how would I be able to use that process to add the two entries together; in this case its an overloaded operator+...not insert function...so would I have to call the insert function but also check of that entry is already in the phonebook that I'm adding to?
The samething would be applied for subtracting; create a new dynamic array of size smaller and just copy all of the entires minus the one that is being taken away..that one is simpler I think.
Where would I have to insert the dynamic array inside the class.
This is what I have thus far:
Code:
#include <iostream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;
class PhoneBookEntry{
private:
public:
string name, number;
PhoneBookEntry(){};
};
class PhoneBook{
private:
PhoneBookEntry* phoneEntry;
int size;
bool executedInsert;
public:
PhoneBook(){size = 0;}
PhoneBook(const PhoneBook&);
PhoneBook& operator =(const PhoneBook&);
~PhoneBook() {delete [] phoneEntry;}
void insert(string, string) const;
void changeArray(PhoneBook*&, int&, string);
PhoneBook operator +(const PhoneBook&) const;
PhoneBook operator -(const PhoneBook&) const;
friend ostream& operator <<(ostream&, const PhoneBook&);
};
/////////////////////////////////////////////////////////////////////////////////////////////////
PhoneBook& PhoneBook::operator =(const PhoneBook& rhs){
if(this != &rhs){
delete [] phoneEntry;
phoneEntry = new PhoneBookEntry[size];
for(int i = 0; i< size; i++){
phoneEntry[i] = rhs.phoneEntry[i];
} //for
phoneEntry = rhs.phoneEntry;
return *this;
}
}
////////////////////////////////////////////////////////////////////////////////////
PhoneBook::PhoneBook(const PhoneBook& rhs){
phoneEntry = NULL;
*this=rhs;
}
////////////////////////////////////////////////////////////////////////////////////
void PhoneBook::changeArray(PhoneBook*& pbArray, int& oldSize, string todo){
int newSize;
if(todo == "add"){
newSize = oldSize++;
}
else if(todo == "subtract"){
newSize = oldSize--;
}
PhoneBook* temp = new PhoneBook[newSize];
for(int i = 0; i < oldSize; i++)
temp[i] = pbArray[i];
delete [] pbArray;
pbArray = temp;
temp = NULL;
oldSize = newSize;
} //expandArray
////////////////////////////////////////////////////////////////////////////////////
void PhoneBook::insert(string n, string numb) const{
/*
I think think the dynamic array should be made here,
just entry is being inserted to it, so should it be created
in the private section?
*/
}
////////////////////////////////////////////////////////////////////////////////////
PhoneBook PhoneBook::operator +(const PhoneBook& rhs) const{
PhoneBook temp;
temp = *this + rhs;
return temp;
} //operator+
////////////////////////////////////////////////////////////////////////////////////
PhoneBook PhoneBook::operator -(const PhoneBook& rhs) const{
PhoneBook temp;
temp = *this - rhs;
return temp;
} //operator-
////////////////////////////////////////////////////////////////////////////////////
ostream& operator <<(ostream& os, const PhoneBook& p){
return os << "Name: "
<< p.phoneEntry->name
<< " Phone: "
<< p.phoneEntry->number
<< endl;
} //operator<<
////////////////////////////////////////////////////////////////////////////////////
int main(){
PhoneBook x,y,z;
//x.insert(“John Doe”, “800-255-3417”);
//x.insert(“Thomas”, “448-230-3630”);
//y.insert(John Doe”, “”);
z = x - y;
cout << z; //should display only the Thomas entry object.
}
I'm not sure how successful the code that I have is going to be thus far, the overloaded operators =, +, -, assignment and copy constructor...
so my question is, if someone would be kind enough to demonstrate how to achieve what I need; what I have described ontop..where the dynamic array should be created (PhoneBook *pbArray = new PhoneBook[size]
, more-or-less what the + / - of the phonebooks would look like...
any help would be appreciated...