C++
Project design
Design the skeleton of the intermediate final mini app.
Project: Task manager
In this 5-lesson project you will build a task manager. Each lesson adds one piece, and by the final lesson you will have a program that combines OOP, templates, STL and file I/O.
You will use what you learned in previous groups: classes, templates, vectors and file streams. The first lesson defines a Task class: it holds a title and a done flag. The following lessons add a template function, an STL layer and file saving on top of this class. Each lesson's starting code contains the previous lesson's solution.
Task class skeleton
First we define the Task class. title is a string and done is a bool. The constructor takes the title and sets done to false.
Example: Task(std::string t) : title(t), done(false) {}. Here the member initializer list sets title from the parameter and done directly to false. When you create a task it is not yet done. In main you write Task t("Homework"); to create it. t.done starts as false which is 0.
Exercise Define the Task class and print it
Define a class named Task. public members: std::string title, bool done. Constructor: Task(std::string t) : title(t), done(false) {}. In main create Task t("Homework"); and print t.title and t.done separated by a space with std::cout.
Show example solution
#include <iostream>
#include <string>
class Task {
public:
std::string title;
bool done;
Task(std::string t) : title(t), done(false) {}
};
int main() {
Task t("Homework");
std::cout << t.title << " " << t.done;
return 0;
}
Test yourself
-
Which two members should the Task class contain?
class Task {
std::string title;
bool done;
};
- Color and size
- Price and stock
- Title and done flag
- Date and time
Show answer
Title and done flag — Task contains title and done members.
-
Constructor initializer list
Task(std::string t) : title(t), done(____) {}
Show answer
Task(std::string t) : title(t), done(false) {}
-
Match each piece to its role
- class Task
- Class definition
- std::string title
- Title member
- bool done
- Done flag
- Task(std::string t)
- Constructor
Next lesson: OOP model