Respuesta :
Answer:
Explanation:
============== main.cpp =======================
#include <iostream>
using namespace std;
#include <iostream>
#include <vector>
#include "Contact.h"
using namespace std;
int main() {
const int NUM_OF_CONTACTS = 3;
vector<Contact *> contacts;
for (int i = 0; i < NUM_OF_CONTACTS; i++) {
string name, phoneNumber;
cout << "Enter a name: ";
cin >> name;
cout << "Enter a phoneNumber: ";
cin >> phoneNumber;
// Use i, name, and phone number to dynamically create a Contact object on the heap
// HINT: Use the Overloaded Constructor here!
Contact * newContact = new Contact(i+1,name,phoneNumber);
// Add the Contact * to the vector...
contacts.push_back(newContact);
}
cout << "\n\n----- Contacts ----- \n\n";
// Loop through the vector of contacts and print out each contact info
std::vector<Contact *>::iterator itrContact;
for(itrContact = contacts.begin(); itrContact !=contacts.end(); itrContact++)
{
cout<< " Id : " << (*itrContact)->getId();
cout<< " Name : " << (*itrContact)->getName();
cout<< " Phone-Number : " << (*itrContact)->getPhoneNumber() <<endl;
}
// Make sure to call the destructor of each Contact object by looping through the vector and using the delete keyword
for(int i = 0; i < NUM_OF_CONTACTS; i++)
{
delete contacts[i];
}
return 0;
}
================== contact.h =====================
#ifndef CONTACT_H
#define CONTACT_H
#include <string>
#include <iostream>
using std::string;
using std::cout;
class Contact {
public:
Contact();
Contact(int id, string name, string phoneNumber);
~Contact();
Contact(const Contact& copy);
Contact& operator=(const Contact& copy);
// getters
int getId() const;
string getName() const;
string getPhoneNumber() const;
//setters
void setId(int id);
void setName(string name);
void setPhoneNumber(string phoneNumber);
private:
int *id = nullptr;
string *name = nullptr;
string *phoneNumber = nullptr;
};
#endif
================= contact.cpp ========================
#include "Contact.h"
Contact::Contact() {
this->id = new int(-1);
this->name = new string("No Name");
this->phoneNumber = new string("No Phone Number");
}
Contact::Contact(int id, string name, string phoneNumber) {
// Implement Overloaded Constructor
// Remember to initialize pointers on the heap!
this->id = new int(id);
this->name = new string(name);
this->phoneNumber = new string(phoneNumber);
}
Contact::~Contact() {
// Implement Destructor
if(this->id != nullptr)
delete this->id;
if(this->name != nullptr)
delete this->name;
if(this->phoneNumber != nullptr)
delete this->phoneNumber;
}
Contact::Contact(const Contact ©) {
// Implement Copy Constructor
this->id = new int(copy.getId());
this->name = new string(copy.getName());
this->phoneNumber = new string(copy.getPhoneNumber());
}
Contact &Contact::operator=(const Contact ©) {
// Implement Copy Assignment Operator
if(this == ©) // Checks for self Assignment
return *this;
this->id = new int(copy.getId());
this->name = new string(copy.getName());
this->phoneNumber = new string(copy.getPhoneNumber());
return *this;
}
// getters
int Contact::getId() const
{
return *(this->id);
}
string Contact::getName() const
{
return *(this->name);
}
string Contact::getPhoneNumber() const
{
return *(this->phoneNumber);
}
//setters
void Contact::setId(int id)
{
*(this->id) = id;
}
void Contact::setName(string name)
{
*(this->name) = name;
}
void Contact::setPhoneNumber(string phoneNumber)
{
*(this->phoneNumber) = phoneNumber;
}
====================Output =========================
is attached below