#include "task.hpp"
#include "todo.hpp"
#include <iostream>
using namespace std;
void printIncomplete(ToDo list) {
// Give
size_t numTasks = list.getNumTasks();
for (size_t i = 0; i < numTasks; ++i) {
Task t = list.getTask(i);
if (!t.isComplete()) {
cout << t.getName() << endl;
}
}
// You
}
int main() {
ToDo myList;
Task myTask{"Object lifetime example"};
// Never
myList.addTask(myTask);
// Gonna
printIncomplete(myList);
return 0;
} // Up
#ifndef TASK_HPP_INCLUDED
#define TASK_HPP_INCLUDED
#include <string>
class Task {
public:
/**
* \brief Creates a completed task with name "None"
*/
Task();
Task(const std::string& name);
/**
* \brief Creates a task that copies another task
* \param other the task to copy
*/
Task(const Task& other);
~Task();
/**
* \brief Makes this task a copy of a given task
* \param rhs the task to copy
* \returns a reference to this task
*/
Task& operator=(const Task& rhs);
/**
* \brief Gets the name of this task
* \returns the name
*/
std::string getName() const;
/**
* \brief Are we done yet?
* \returns whether the task is complete
*/
bool isComplete() const;
private:
std::string name_;
bool complete_;
};
#endif
#include "task.hpp"
#include <iostream>
using namespace std;
Task::Task() :
name_{"None"},
complete_{true} {
cout << "Task default constructor" << endl;
}
Task::Task(const string& name) :
name_{name},
complete_{false} {
cout << "Task parameterized constructor" << endl;
}
Task::Task(const Task& other) :
name_{other.name_},
complete_{other.complete_} {
cout << "Task copy constructor" << endl;
}
Task::~Task() {
cout << "Task destructor" << endl;
// Nothing else to do
}
Task& Task::operator=(const Task& rhs) {
name_ = rhs.name_;
complete_ = rhs.complete_;
cout << "Task assignment operator" << endl;
// Don't worry about this line. It will make sense soon!
return *this;
}
std::string Task::getName() const {
return name_;
}
bool Task::isComplete() const {
return complete_;
}
#ifndef TODO_HPP_INCLUDED
#define TODO_HPP_INCLUDED
#include "task.hpp"
class ToDo {
public:
ToDo();
/**
* \brief Creates a to-do list that copies another to-do list
* \param other the ToDo to copy
*/
ToDo(const ToDo& other);
~ToDo();
/**
* \brief Makes this ToDo a copy of a given ToDo
* \param rhs the ToDo to copy
* \returns a reference to this ToDo
*/
ToDo& operator=(const ToDo& rhs);
/**
* \brief Add a task to the list
* \param rhs the Task to copy
*/
void addTask(Task t);
/**
* \brief Find the task at a specific index
* \returns a copy of the task at that index
* \note It might have been better for this to return a reference rather
* than making a copy
*/
Task getTask(size_t idx) const;
size_t getNumTasks() const;
private:
/**
* \brief Makes this ToDo a copy of a given ToDo
* \param other the ToDo to copy
*/
void copyTasks(const ToDo& other);
static constexpr size_t NUMTASKS_ = 3;
Task tasks_[NUMTASKS_];
size_t numTasks_;
};
#endif // TODO_HPP_INCLUDED
#include "todo.hpp"
#include <iostream>
using namespace std;
ToDo::ToDo() :
numTasks_{0} {
cout << "ToDo default constructor" << endl;
}
ToDo::ToDo(const ToDo& other) :
numTasks_{other.numTasks_} {
cout << "ToDo copy constructor" << endl;
copyTasks(other);
}
ToDo::~ToDo() {
cout << "ToDo destructor" << endl;
// Nothing else do to
}
ToDo& ToDo::operator=(const ToDo& rhs) {
cout << "ToDo assignment operator" << endl;
numTasks_ = rhs.numTasks_;
copyTasks(rhs);
// Don't worry about this line. It will make sense soon!
return *this;
}
void ToDo::addTask(Task t) {
tasks_[numTasks_] = t;
++numTasks_;
}
Task ToDo::getTask(size_t idx) const {
return tasks_[idx];
}
size_t ToDo::getNumTasks() const {
return numTasks_;
}
void ToDo::copyTasks(const ToDo& other) {
for (size_t i = 0; i < NUMTASKS_; ++i) {
tasks_[i] = other.tasks_[i];
}
}