#include "checklist.hpp"
int main() {
CheckList makeDinner{"Sausage and Mash", 7};
makeDinner.setStep(0, "Peel potatoes");
makeDinner.setStep(1, "Cook potatoes");
makeDinner.setStep(2, "Cook vegetables");
makeDinner.setStep(3, "Grill Sausages");
makeDinner.setStep(4, "Make Gravy");
makeDinner.setStep(5, "Mash Potatoes");
makeDinner.setStep(6, "Serve");
makeDinner.printToStream(std::cout);
}
#ifndef CHECKLIST_HPP_INCLUDED
#define CHECKLIST_HPP_INCLUDED
#include <string>
#include <iostream>
class CheckList {
public:
CheckList() = delete;
CheckList(std::string task, size_t numSteps);
CheckList(const CheckList& other);
~CheckList();
CheckList& operator=(CheckList rhs); // copy-and-swap idiom
void swap(CheckList& other);
std::string task() const;
size_t size() const;
std::string getStep(size_t index) const;
void setStep(size_t index, std::string newValue);
void printToStream(std::ostream& out);
private:
std::string task_;
size_t numSteps_;
std::string* steps_;
};
#endif // CHECKLIST_HPP_INCLUDED
#include "checklist.hpp"
#include <string>
CheckList::CheckList(std::string task, size_t numSteps)
: task_{task}, numSteps_{numSteps}, steps_{new std::string[numSteps_]} {
// Array of strings on heap has all elements default initialized to
// empty strings, which is fine, so nothing (else) to do.
}
CheckList::CheckList(const CheckList& other)
: task_{other.task_},
numSteps_{other.numSteps_},
steps_{new std::string[numSteps_]} {
// Rather than hand-write a for loop, this code uses std::copy to copy
// the array. The arguments are:
// source_start, source_past_end, dest_start
std::copy(other.steps_, other.steps_ + other.numSteps_, steps_);
}
CheckList::~CheckList() {
delete[] steps_;
}
CheckList& CheckList::operator=(CheckList rhs) { // copy-and-swap idiom
swap(rhs);
return *this;
}
void CheckList::swap(CheckList& other) {
std::swap(task_, other.task_);
std::swap(numSteps_, other.numSteps_);
std::swap(steps_, other.steps_);
}
size_t CheckList::size() const {
return numSteps_;
}
std::string CheckList::task() const {
return task_;
}
std::string CheckList::getStep(size_t index) const {
return steps_[index];
}
void CheckList::setStep(size_t index, std::string newValue) {
steps_[index] = newValue;
}
void CheckList::printToStream(std::ostream& out) {
out << task_ << ":" << std::endl;
for (std::string* p = steps_; p != steps_ + numSteps_; ++p) {
out << " - " << *p << std::endl;
}
}