1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
HACTEHA [7]
3 years ago
5

In this lab, you will be creating a class that implements the Rule of Three (A Destructor, A Copy Constructor, and a Copy Assign

ment Operator). You are to create a program that prompts users to enter in contact information, dynamically create each object, then print the information of each contact to the screen. Some code has already been provided for you. To receive full credit make sure to implement the following:
Default Constructor - set Contact id to -1
Overloaded Constructor - used to set the Contact name, phoneNumber and id (Should take in 3 parameters)
Destructor
Copy Constructor
Copy Assignment Operator
Any other useful functions (getters/setters)
Main.cpp

#include
#include
#include "Contact.h"

using namespace std;

int main() {
const int NUM_OF_CONTACTS = 3;
vector 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;

// TODO: Use i, name, and phone number to dynamically create a Contact object on the heap
// HINT: Use the Overloaded Constructor here!


// TODO: Add the Contact * to the vector...
}
cout << "\n\n----- Contacts ----- \n\n";

// TODO: Loop through the vector of contacts and print out each contact info

// TODO: Make sure to call the destructor of each Contact object by looping through the vector and using the delete keyword

return 0;
}

Contact.h

#ifndef CONTACT_H
#define CONTACT_H

#include
#include

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);

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) {
// TODO: Implement Overloaded Constructor
// Remember to initialize pointers on the heap!
}

Contact::~Contact() {
// TODO: Implement Destructor
}

Contact::Contact(const Contact ©) {
// TODO: Implement Copy Constructor
}

Contact &Contact::operator=(const Contact ©) {
// TODO: Implement Copy Assignment Operator
return *this;
}

Engineering
1 answer:
Elodia [21]3 years ago
3 0

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 &copy) {

// 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 &copy) {

// Implement Copy Assignment Operator

if(this == &copy) // 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

You might be interested in
Why do the quadrants in coordinate plane go anti-clockwise?.
tia_tia [17]

Answer:

Quadrants are counter-clockwise because angles are measured counter-clockwise; and angles are measured counter-clockwise so that Cross Product of unit vector in X direction with that in the Y direction has to be the unit vector in the Z direction (coming towards us from the origin).

Explanation:

7 0
3 years ago
What type of cartilage provides support and shock absorption?.
Katena32 [7]

Answer:

fibrocartilage

7 0
3 years ago
Is a jeep cherokee faster than a bmw 325i
soldier1979 [14.2K]

Answer:

Yes

Explanation:

8 0
3 years ago
Read 2 more answers
technician a says that the higher the gear selected the more torque is available. technician b says that a transaxle contains ge
umka21 [38]

Technician A is correct. Technician B is wrong because a gear's transmission is used to increase or decrease torque.

The relation torque is relying on multiplying the circumferential detail with the resource of the usage of the radius; massive gears experience a greater amount of torque, at the same time as smaller gears experience a great deal much less torque. Similarly, the torque ratio is equal to the ratio of the gears' radii. A gear's transmission torque modifications as it will boom or decreases speed. Commonly, with the resource of the usage of lowering the speed, a small torque on the doorway issue is transferred as a massive torque at the output issue. The calculation of torque is quantified with the resource of the usage of an extensive form of teeth.

Learn more about the torque at brainly.com/question/28220969

#SPJ4

3 0
1 year ago
Water at 15°C is to be discharged from a reservoir at a rate of 18 L/s using two horizontal cast iron pipes connected in series
Zina [86]

Answer:

0.245 m^3/s

Explanation:

Flow rate through pipe a is 0.4 m3/s Parallel pipes have a diameter D = 30 cm => r = 15 cm = 0.15 m Length of Pipe a = 1000m Length of Pipe b = 2650m Temperature = 15 degrees Va = V / A = (0.4m3/s) / (3.14 (0.15m)^2) = 5.66 m/s h = (f(LV^2)) / D2g (fa(LaVa^2)) / Da2g = (fb(LbVb^2)) / Da2g and Da = Db; fa = fb LaVa^2 = LbVb^2 => La/Lb = Vb^2/Va^2 Vd^2 = Va^2(La/Lb) => Vb = Va(La/Lb)^(1/2) Vb = 5.66 (1000/2650)^(1/2) => 5.66 x 0.6143 = 3.4769 m/s Vb = 3.4769 m/s V = AVb = 3.14(0.15)^2 x 3.4769 m/s = 0.245 m^3/s

5 0
3 years ago
Other questions:
  • A First Stage in a turbine receives steam at 10 MPa, 800 C with an exit pressure of 800 KPa. Assume the stage is adiabatic and r
    5·1 answer
  • A(n)______ is a device used to ensure positive position of a valve or damper actuator A. calibrator B. positioner C. actuator D.
    6·1 answer
  • Is it more difficult to pump oil from a well on dry land or a well under water?Why?
    11·1 answer
  • A part has been tested to have Sut = 530 MPa, f = 0.9, and a fully corrected Se = 210 MPa. The design requirements call for the
    10·1 answer
  • A Barnes and Books is interested in purchasing a two-story building for a new
    5·1 answer
  • Ring rolling is a deformation process in which a thick-walled ring of smaller diameter is rolled into a thin-walled ring of larg
    11·1 answer
  • using the two transistor analogy to explain what happens when an SCR is supplied with some gate current.​
    15·1 answer
  • How do i do this? if y’all don’t mind helping lol
    13·1 answer
  • According to the article, what is one reason why commercial carmakers aim to develop driverless technology?
    9·1 answer
  • Technician A says that the low level brake fluid switch on a master cylinder will turn on the brake warning light when the syste
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!