Question: Q.EDIT THE CODE TO GET THE EXACT OUTPUT To the Contact class you have created , add the following member functions (and implement them): copy
Q.EDIT THE CODE TO GET THE EXACT OUTPUT
To the Contact class you have created , add the following member functions (and implement them):
copy constructor: this constructor should safely make a copy of an existing instance;
copy assignment operator: this operator should set the current instance to another Contact instance, received as a parameter.
void addPhoneNumber(long long phoneNumber): a modifier that adds a new phone number to the contact (this function should resize the array). The phone number must be validated.
Note: When you implement the copy assignment operator, make sure that the Contact instance is not being set to itself.
Using the sample implementation of the w6_at_home.cpp main module shown below, test your code and make sure that it works. Below the source code is the expected output from your program. The output of your program should match exactly the expected one.
| #include #include "Contact.h"
using namespace std; using namespace communication;
int main() { communication::Contact theContact("John Doe", nullptr, 0); theContact.addPhoneNumber(14161234567); theContact.addPhoneNumber(14162345678); theContact.addPhoneNumber(14163456789); theContact.addPhoneNumber(114164567890); theContact.display();
cout << endl << "Testing! Please wait:" << endl;
for (int i = 1; i <= 5000000; i++) { Contact temp = theContact; theContact = temp; theContact = theContact; if (!(i % 10000)) cout << "."; if (!(i % 500000)) cout << endl; } cout << endl; theContact.display();
return 0; } |
| Output: John Doe John Doe (+1) 416 123-4567 (+1) 416 234-5678 (+1) 416 345-6789 (+11) 416 456-7890
Testing! Please wait: .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ..................................................
John Doe (+1) 416 123-4567 (+1) 416 234-5678 (+1) 416 345-6789 (+11) 416 456-7890 |
Contact.h
#ifndef CONTACT_H__ #define CONTACT_H__
namespace communication {
class Contact { char m_name[21]; long long* m_phoneNumbers; int m_noOfPhoneNumbers;
public: // TODO: add the default constructor here Contact(); // // TODO: add the constructor with parameters here Contact(const char* name, const long long* phoneNumbers, const int noOfPhoneNumb // // TODO: add the destructor here ~Contact(); // // TODO: add the display function here bool display() const; // // TODO: add the isEmpty function here bool isEmpty() const;
public: Contact(const Contact& other); void operator=(const Contact& other); void addPhoneNumber(long long phoneNumber); }; } #endif
Contact.cpp
#include
using namespace std;
namespace communication { Contact::Contact(const Contact &other) {
int i =0; snprintf(m_name,sizeof(m_name),"%s", other.m_name);
m_noOfPhoneNumbers = other.m_noOfPhoneNumbers;
if(other.m_phoneNumbers){
m_phoneNumbers = new long long[m_noOfPhoneNumbers]; for(i=0; i < m_noOfPhoneNumbers; i++){
m_phoneNumbers[i] = other.m_phoneNumbers[i]; } } }
Contact::Contact() { m_name[0] = '\0'; m_phoneNumbers = NULL; m_noOfPhoneNumbers = 0; } Contact::Contact(const char *name, const long long *phoneNumbers, const int noOfPhoneNumbers) { m_name[0] = '\0'; m_phoneNumbers = NULL; m_noOfPhoneNumbers = 0; if ((name != NULL) && (*name != '\0') && (strcmp(name, "") != 0)) { strncpy(m_name, name, strlen(name)); m_name[strlen(name)] = '\0'; m_noOfPhoneNumbers = noOfPhoneNumbers; } if (phoneNumbers) { m_phoneNumbers = new long long[m_noOfPhoneNumbers]; for (int i = 0; i < m_noOfPhoneNumbers; i++) { m_phoneNumbers[i] = phoneNumbers[i]; } }
display();
}
Contact::~Contact() { delete[] m_phoneNumbers; }
bool Contact::isEmpty() const { return m_name[0] == '\0' || strcmp(m_name, "") == 0; } bool Contact::display() const { if (isEmpty()) { cout << "Empty contact!" << endl; } else { cout << m_name << endl; for (int i = 0; i < m_noOfPhoneNumbers; i++) { if (10000000000 <= m_phoneNumbers[i] && m_phoneNumbers[i] <= 999999999999) { cout << "(+" << m_phoneNumbers[i] / 10000000000 << ") " << m_phoneNumbers[i] % 10000000000 / 10000000 << " " << m_phoneNumbers[i] % 10000000 / 10000 << "-" << m_phoneNumbers[i] % 10000000 % 10000 << endl; } } } return true;
}
void Contact::operator=(const Contact &other) {
int i =0;
if(this != &other){ snprintf(m_name,sizeof(m_name),"%s", other.m_name);
m_noOfPhoneNumbers = other.m_noOfPhoneNumbers; delete[] m_phoneNumbers;
if(other.m_phoneNumbers){
m_phoneNumbers = new long long[m_noOfPhoneNumbers]; for(i=0; i < m_noOfPhoneNumbers; i++){ m_phoneNumbers[i] = other.m_phoneNumbers[i]; } } }
}
void Contact::addPhoneNumber(long long phoneNumber) {
int newCount = m_noOfPhoneNumbers + 1; int i =0 ;
long long* numbers = new long long[newCount];
for(i=0; i numbers[i] = m_phoneNumbers[i]; } numbers[i] = phoneNumber; delete[] m_phoneNumbers; m_phoneNumbers = numbers; m_noOfPhoneNumbers = newCount; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
