// // todo.cpp: sample C++ OO code using pointers // #include using namespace std; class Task { private: string _description; double _hours; public: Task(string description, double hours) : _description{description}, _hours(hours) { } double hours() { return _hours; } string description() { return _description; } }; class TaskList { private: static constexpr int MAX = 50; int count; Task *todo[MAX]; // note the *! public: TaskList() : count{0} { } void add(Task *new_task) { if ( count >= MAX ) throw "Task list full."; todo[count] = new_task; ++count; } double totalHours() { auto tot = 0.0; for(int i = 0; i < count; ++i) tot += todo[i]->hours(); return tot; } }; int main() { TaskList *tasks = new TaskList(); tasks->add(new Task("sleep", 6.5)); tasks->add(new Task("shower", 0.17)); tasks->add(new Task("program", 16.5)); tasks->add(new Task("eat", 0.5)); cout << "Total time: " << tasks->totalHours() << endl; return 0; } // // output: // // Total time: 23.67 //