Question
C++ LAB Contact.cpp #include #include #include Contact.h using namespace std; namespace communication { Contact::Contact(const Contact &other) {} Contact::Contact() { m_name[0] = '0'; m_phoneNumbers = NULL;
C++ LAB
Contact.cpp
#include
using namespace std;
namespace communication { Contact::Contact(const Contact &other) {}
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 != '\0' && strcmp(name, "") != 0) { strncpy(m_name, name, 20); m_name[20] = '\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]; } }
}
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;
} /* Contact &Contact::operator=(const Contact &other) { if (this != &other) { m_name[0] = other.m_name[0]; m_phoneNumbers = other.m_phoneNumbers;
delete[] m_phoneNumbers;
if (other.m_phoneNumbers != NULL) { m_phoneNumbers = new long long[m_noOfPhoneNumbers]; for (int i = 0; i < m_noOfPhoneNumbers; i++) m_phoneNumbers[i] = other.m_phoneNumbers[i]; } else { m_phoneNumbers = NULL; } } return *this;
}
void Contact::addPhoneNumber(long long phoneNumber) { }*/
}
Contact.h
#ifndef CONTACT_H__ #define CONTACT_H__
// TODO: add namespace here 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 noOfPhoneNumbers); // 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); // Copy Constructor Contact& operator=(const Contact& other); // Copy Assignment Operator void addPhoneNumber(long long phoneNumber); // Modifier };
} #endif
QUESTION:
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 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started