C++ program. you will experiment with constructors and destructors. Your goal is
ID: 3591189 • Letter: C
Question
C++ program. you will experiment with constructors and destructors. Your goal is to avaid unecessary copies and use references and constant as much as possible (but not more).
Create a source and a header file for the class and name them device.cpp and device.h, respectively. You can find the file main.cpp with this post. Make sure to use the names exactly as typed.
Define a structure DateOfManufacture that has three integer fields for day, month and year. Define a single function print that accepts an ostream. The class ostream is the type of cout as well as of cerr. Your print function is to insert in the passed ostream object (and not directly in cout). Note that ostream has a deleted copy constructor and you cannot pass ostream by value.
Define a class Device that has three fields: one string for the name, one Type for the type of the device and one DateOfManufacture for the device. The Type should be a inner scoped enumeration in Device. The Device class should have a print function that accepts an ostream. In addition define a function toStr that accept an argument of type Type and returns the type as string. Define an all-argument constructor which accepts a name, type and date of manufacture as input.
Define a class ChargeStack that holds devices in a std::vector. Define the functions pushthat accept a device and inserts it into the vector and pop that returns the last device added to the vector and removes it from the vector.
When your classes work with the provided main program than define your own copy constructor and destructor for the class Device. Your own copy constructor and destructor should work the same as the built-in versions, except it should print the device to std::cerr. The Copy constructor for a device named clunker of type phone with a date of manufacture of 2010/3/1 should print Copied device: clunker Phone Date: 2010/3/1
Main Program
#include
#include
#include "device.h"
using std::cout;
using std::cin;
using std::endl;
int main() { // Make 6 devices using all four different device types at least once
std::array dev{{
{"clunker", Device::Type::phone, {1,3,2010}},
{"jewel", Device::Type::watch, {12,5,2017}},
{"doorstop", Device::Type::laptop, {21,7,2016}},
{"swish", Device::Type::tablet, {5,11,2009}},
{"red", Device::Type::phone, {9,6,2017}},
{"light", Device::Type::laptop, {3,3,2015}}}};
// Pushing the six devices onto the charge stack
cout << "=================================" << endl;
cout << "Pushing the devices on the stack: " << endl;
cout << "=================================" << endl;
ChargeStack cs;
for ( auto& d:dev ) {
cs.push(d);
}
cout << "=================================" << endl;
cout << "Printing the queue: " << endl;
cout << "=================================" << endl;
cs.print(cout);
cout << "=================================" << endl;
cout << "Removing the last three devices: " << endl;
cout << "=================================" << endl;
for (int i=0;i<3;++i) {
cs.pop();
}
cout << "=================================" << endl;
cout << "Printing the queue again: " << endl;
cout << "=================================" << endl;
cs.print(cout);
// Make a new queue by copying the queue
cout << "=================================" << endl;
cout << "Making a new stack by copy: " << endl;
cout << "=================================" << endl;
ChargeStack cs2{cs};
cout << "===============================================" << endl;
cout << "Moving the remaining devices to the new Stack: " << endl;
cout << "===============================================" << endl;
// moving the remaining three
for (int i=0;i<3;++i) {
cs2.push(cs.pop());
}
cout << "=================================" << endl;
cout << "Printing the new stack: " << endl;
cout << "=================================" << endl;
cs2.print(cout);
cout << "=================================" << endl;
return 0;
`}
An test run of your program should then look as follows (try to analyze the output):
=================================
Pushing the devices on the stack:
=================================
Copied device: clunker Phone Date: 2010/3/1
Copied device: jewel Smartwatch Date: 2017/5/12
Copied device: doorstop Laptop Date: 2016/7/21
Copied device: swish Tablet Date: 2009/11/5
Copied device: red Phone Date: 2017/6/9
Copied device: light Laptop Date: 2015/3/3
=================================
Printing the queue:
=================================
clunker Phone Date: 2010/3/1
jewel Smartwatch Date: 2017/5/12
doorstop Laptop Date: 2016/7/21
swish Tablet Date: 2009/11/5
red Phone Date: 2017/6/9
light Laptop Date: 2015/3/3
=================================
Removing the last three devices:
=================================
Copied device: light Laptop Date: 2015/3/3
Destructing device: light Laptop Date: 2015/3/3
Destructing device: light Laptop Date: 2015/3/3
Copied device: red Phone Date: 2017/6/9
Destructing device: red Phone Date: 2017/6/9
Destructing device: red Phone Date: 2017/6/9
Copied device: swish Tablet Date: 2009/11/5
Destructing device: swish Tablet Date: 2009/11/5
Destructing device: swish Tablet Date: 2009/11/5
=================================
Printing the queue again:
=================================
clunker Phone Date: 2010/3/1
jewel Smartwatch Date: 2017/5/12
doorstop Laptop Date: 2016/7/21
=================================
Making a new stack by copy:
=================================
Copied device: clunker Phone Date: 2010/3/1
Copied device: jewel Smartwatch Date: 2017/5/12
Copied device: doorstop Laptop Date: 2016/7/21
===============================================
Moving the remaining devices to the new Stack:
===============================================
Copied device: doorstop Laptop Date: 2016/7/21
Destructing device: doorstop Laptop Date: 2016/7/21
Copied device: doorstop Laptop Date: 2016/7/21
Copied device: clunker Phone Date: 2010/3/1
Copied device: jewel Smartwatch Date: 2017/5/12
Copied device: doorstop Laptop Date: 2016/7/21
Destructing device: clunker Phone Date: 2010/3/1
Destructing device: jewel Smartwatch Date: 2017/5/12
Destructing device: doorstop Laptop Date: 2016/7/21
Destructing device: doorstop Laptop Date: 2016/7/21
Copied device: jewel Smartwatch Date: 2017/5/12
Destructing device: jewel Smartwatch Date: 2017/5/12
Copied device: jewel Smartwatch Date: 2017/5/12
Destructing device: jewel Smartwatch Date: 2017/5/12
Copied device: clunker Phone Date: 2010/3/1
Destructing device: clunker Phone Date: 2010/3/1
Copied device: clunker Phone Date: 2010/3/1
Destructing device: clunker Phone Date: 2010/3/1
=================================
Printing the new stack:
=================================
clunker Phone Date: 2010/3/1
jewel Smartwatch Date: 2017/5/12
doorstop Laptop Date: 2016/7/21
doorstop Laptop Date: 2016/7/21
jewel Smartwatch Date: 2017/5/12
clunker Phone Date: 2010/3/1
=================================
Destructing device: clunker Phone Date: 2010/3/1
Destructing device: jewel Smartwatch Date: 2017/5/12
Destructing device: doorstop Laptop Date: 2016/7/21
Destructing device: doorstop Laptop Date: 2016/7/21
Destructing device: jewel Smartwatch Date: 2017/5/12
Destructing device: clunker Phone Date: 2010/3/1
Destructing device: light Laptop Date: 2015/3/3
Destructing device: red Phone Date: 2017/6/9
Destructing device: swish Tablet Date: 2009/11/5
Destructing device: doorstop Laptop Date: 2016/7/21
Destructing device: jewel Smartwatch Date: 2017/5/12
Destructing device: clunker Phone Date: 2010/3/1
Explanation / Answer
Given below are the needed files . Please name the ChargeStack.h according to the way it is included in main program. In the posted question, the part after #include is missing . So not able to figure out what case is the filename is . (It would matter especially if you working in unix based system).
device.h
#ifndef device_h
#define device_h
#include <string>
#include <iostream>
using std::ostream;
using std::string;
struct DateOfManufacture
{
int day;
int month;
int year;
};
class Device
{
public:
enum Type
{
phone, watch, laptop, tablet
};
void print(ostream &out) const;
string toStr(Type t) const;
Device(string n , Type t, DateOfManufacture d);
Device(const Device &d);
~Device();
private:
string name;
Type type;
struct DateOfManufacture date;
};
#endif /* device_h */
device.cpp
#include "device.h"
using std::cout;
using std::endl;
void Device::print(ostream &out) const
{
out << name << " " << toStr(type) << " Date: " << date.year << "/" << date.month << "/" << date.day << endl;
}
string Device::toStr(Type t) const
{
if(t == phone)
return "Phone";
else if(t == laptop)
return "Laptop";
else if(t == watch)
return "Smartwatch";
else if(t == tablet)
return "Tablet";
else
return "NONE";
}
Device::Device(string n , Type t, DateOfManufacture d)
{
name = n;
type = t;
date = d;
}
Device::Device(const Device &d)
{
name = d.name;
type = d.type;
date = d.date;
cout << "Copied device: ";
d.print(cout);
}
Device::~Device()
{
cout << "Destructing device: ";
print(cout);
}
chargestack.h
#ifndef ChargeStack_h
#define ChargeStack_h
#include <vector>
#include "device.h"
using std::vector;
class ChargeStack
{
private:
vector<Device> devices;
public:
ChargeStack();
void push(const Device &d);
Device pop();
void print(ostream & out);
};
#endif /* ChargeStack_h */
chargestack.cpp
#include "ChargeStack.h"
ChargeStack::ChargeStack()
{
}
void ChargeStack::push(const Device &d)
{
devices.push_back(d);
}
Device ChargeStack::pop()
{
Device d = devices.back();
devices.pop_back();
return d;
}
void ChargeStack::print(ostream &out)
{
for(int i = 0; i < devices.size(); i++)
devices[i].print(out);
}
output
=================================
Pushing the devices on the stack:
=================================
Copied device: clunker Phone Date: 2010/3/1
Copied device: jewel Smartwatch Date: 2017/5/12
Copied device: clunker Phone Date: 2010/3/1
Destructing device: clunker Phone Date: 2010/3/1
Copied device: doorstop Laptop Date: 2016/7/21
Copied device: jewel Smartwatch Date: 2017/5/12
Copied device: clunker Phone Date: 2010/3/1
Destructing device: jewel Smartwatch Date: 2017/5/12
Destructing device: clunker Phone Date: 2010/3/1
Copied device: swish Tablet Date: 2009/11/5
Copied device: red Phone Date: 2017/6/9
Copied device: swish Tablet Date: 2009/11/5
Copied device: doorstop Laptop Date: 2016/7/21
Copied device: jewel Smartwatch Date: 2017/5/12
Copied device: clunker Phone Date: 2010/3/1
Destructing device: swish Tablet Date: 2009/11/5
Destructing device: doorstop Laptop Date: 2016/7/21
Destructing device: jewel Smartwatch Date: 2017/5/12
Destructing device: clunker Phone Date: 2010/3/1
Copied device: light Laptop Date: 2015/3/3
=================================
Printing the queue:
=================================
clunker Phone Date: 2010/3/1
jewel Smartwatch Date: 2017/5/12
doorstop Laptop Date: 2016/7/21
swish Tablet Date: 2009/11/5
red Phone Date: 2017/6/9
light Laptop Date: 2015/3/3
=================================
Removing the last three devices:
=================================
Copied device: light Laptop Date: 2015/3/3
Destructing device: light Laptop Date: 2015/3/3
Destructing device: light Laptop Date: 2015/3/3
Copied device: red Phone Date: 2017/6/9
Destructing device: red Phone Date: 2017/6/9
Destructing device: red Phone Date: 2017/6/9
Copied device: swish Tablet Date: 2009/11/5
Destructing device: swish Tablet Date: 2009/11/5
Destructing device: swish Tablet Date: 2009/11/5
=================================
Printing the queue again:
=================================
clunker Phone Date: 2010/3/1
jewel Smartwatch Date: 2017/5/12
doorstop Laptop Date: 2016/7/21
=================================
Making a new stack by copy:
=================================
Copied device: clunker Phone Date: 2010/3/1
Copied device: jewel Smartwatch Date: 2017/5/12
Copied device: doorstop Laptop Date: 2016/7/21
===============================================
Moving the remaining devices to the new Stack:
===============================================
Copied device: doorstop Laptop Date: 2016/7/21
Destructing device: doorstop Laptop Date: 2016/7/21
Copied device: doorstop Laptop Date: 2016/7/21
Copied device: doorstop Laptop Date: 2016/7/21
Copied device: jewel Smartwatch Date: 2017/5/12
Copied device: clunker Phone Date: 2010/3/1
Destructing device: doorstop Laptop Date: 2016/7/21
Destructing device: jewel Smartwatch Date: 2017/5/12
Destructing device: clunker Phone Date: 2010/3/1
Destructing device: doorstop Laptop Date: 2016/7/21
Copied device: jewel Smartwatch Date: 2017/5/12
Destructing device: jewel Smartwatch Date: 2017/5/12
Copied device: jewel Smartwatch Date: 2017/5/12
Destructing device: jewel Smartwatch Date: 2017/5/12
Copied device: clunker Phone Date: 2010/3/1
Destructing device: clunker Phone Date: 2010/3/1
Copied device: clunker Phone Date: 2010/3/1
Destructing device: clunker Phone Date: 2010/3/1
=================================
Printing the new stack:
=================================
clunker Phone Date: 2010/3/1
jewel Smartwatch Date: 2017/5/12
doorstop Laptop Date: 2016/7/21
doorstop Laptop Date: 2016/7/21
jewel Smartwatch Date: 2017/5/12
clunker Phone Date: 2010/3/1
=================================
Destructing device: clunker Phone Date: 2010/3/1
Destructing device: jewel Smartwatch Date: 2017/5/12
Destructing device: doorstop Laptop Date: 2016/7/21
Destructing device: doorstop Laptop Date: 2016/7/21
Destructing device: jewel Smartwatch Date: 2017/5/12
Destructing device: clunker Phone Date: 2010/3/1
Destructing device: light Laptop Date: 2015/3/3
Destructing device: red Phone Date: 2017/6/9
Destructing device: swish Tablet Date: 2009/11/5
Destructing device: doorstop Laptop Date: 2016/7/21
Destructing device: jewel Smartwatch Date: 2017/5/12
Destructing device: clunker Phone Date: 2010/3/1
Program ended with exit code: 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.