help me to get the following output: (you need to modify contact.cpp & contact.h
ID: 3840992 • Letter: H
Question
help me to get the following output: (you need to modify contact.cpp & contact.h)
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
main.cpp
#include <iostream>
#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;
}
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
Contact.cpp
#include <iostream>
#include <cstring>
#include "Contact.h"
using namespace std;
namespace communication {
Contact::Contact(const Contact &other) {}
Contact::Contact() {
m_name[0] = '';
m_phoneNumbers = NULL;
m_noOfPhoneNumbers = 0;
}
Contact::Contact(const char *name, const long long *phoneNumbers, const int noOfPhoneNumbers) {
m_name[0] = '';
m_phoneNumbers = NULL;
m_noOfPhoneNumbers = 0;
if (name != '' && strcmp(name, "") != 0) {
strncpy(m_name, name, 20);
m_name[20] = '';
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] == '' || 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) {
}*/
}
Explanation / Answer
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 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
void operator=(const Contact& other); // Copy Assignment Operator
void addPhoneNumber(long long phoneNumber); // Modifier
};
}
contact.cpp :
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] = '';
m_phoneNumbers = NULL;
m_noOfPhoneNumbers = 0;
}
Contact::Contact(const char *name, const long long *phoneNumbers, const int noOfPhoneNumbers) {
m_name[0] = '';
m_phoneNumbers = NULL;
m_noOfPhoneNumbers = 0;
if ((name != NULL) && (*name != '') && (strcmp(name, "") != 0)) {
strncpy(m_name, name, strlen(name));
m_name[strlen(name)] = '';
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] == '' || 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<m_noOfPhoneNumbers ;i++){
numbers[i] = m_phoneNumbers[i];
}
numbers[i] = phoneNumber;
delete[] m_phoneNumbers;
m_phoneNumbers = numbers;
m_noOfPhoneNumbers = newCount;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.