57.5k views
1 vote
The Phonebook class must also have the following functions: Constructor with 1 integer parameter for size (numberOfContacts is initially set to 0; allocate memory for myContacts). Copy constructor. Assignment operator. Destructor. int getNumberOfContacts() // Getter int getSize() // Getter contact getContact(int i) //returns contact at index i (you can assume 0 < i < numberOfContacts) bool addContact(string name, string phonenumber, string email) // add a new contact at the end of the phonebook using the received parameters. Returns true if succesful, false if the contact couldn't be added because the phonebook is already full.

User Blueman
by
4.4k points

1 Answer

3 votes

Answer:

See explaination

Step-by-step explanation:

#include <iostream>

using namespace std;

struct contact

{

string name;

string phoneNumber;

string email;

bool available;

};

class Phonebook

{

private :

int numberOfContacts;

int size;

contact *myContacts;

public :

Phonebook(int size)

{

this->size=size;

this->numberOfContacts=0;

myContacts=new contact[size];

}

~Phonebook()

{

delete[] myContacts;

}

int getNumberOfContacts()

{

return numberOfContacts;

}

int getSize()

{

return size;

}

void addContact(string name,string phonenumber,string email)

{

contact c;

c.name=name;

c.phoneNumber=phonenumber;

c.email=email;

c.available=true;

myContacts[numberOfContacts]=c;

numberOfContacts++;

}

void removeContact(string name)

{

for(int i=0;i<numberOfContacts;i++)

{

if(myContacts[i].name.compare(name)==0)

{

this->myContacts[i].available=false;

contact temp=myContacts[numberOfContacts-1];

myContacts[numberOfContacts-1]=myContacts[i];

myContacts[i]=temp;

numberOfContacts--;

}

}

}

Phonebook(const Phonebook& pb)

{

this->myContacts=pb.myContacts;

this->size=pb.size;

this->numberOfContacts=numberOfContacts;

}

Phonebook& operator=(const Phonebook & pb)

{

myContacts=new contact[size];

this->size=pb.size;

this->numberOfContacts=pb.numberOfContacts;

for(int i=0;i<numberOfContacts;i++)

{

this->myContacts[i]=pb.myContacts[i];

}

return *this;

}

void print()

{

for(int i=0;i<numberOfContacts;i++)

{

if(myContacts[i].available==true)

{

cout<<"Name :"<<myContacts[i].name<<endl;

cout<<"Phone Number :"<<myContacts[i].phoneNumber<<endl;

cout<<"Email :"<<myContacts[i].email<<endl;

cout<<endl;

}

}

}

};

int main(){

Phonebook pb1(10);

pb1.addContact("Williams","9845566778","williamatmail.com");

pb1.addContact("James","9856655445","jamesatmail.com");

cout<<"_____ Displaying Phonebook#1 contacts _____"<<endl;

pb1.print();

pb1.removeContact("Williams");

cout<<"_____ After removing a contact Displaying Phonebook#1 _____"<<endl;

pb1.print();

Phonebook pb2(5);

pb2.addContact("Billy","9845554444","billyatmail.com");

pb2.addContact("Jimmy","9834444444","jimmyatmail.com");

cout<<"_____ Displaying Phonebook#2 contacts _____"<<endl;

pb2.print();

pb2=pb1;

cout<<"____ After Assignment Operator Displaying Phonebook#2 contacts____"<<endl;

pb2.print();

return 0;

}

Nb: Replace the at with at symbol.

User Shakeel Ahmed
by
4.7k points