Help with classes and object

My question would be the output statements for my TaskList.cpp. For outputFromList() I want to output the Task(s) the user has input into the vector and for getTask(int num) I want to get the specific task at the index of the vector. I'm not sure if my implementation is correct, because I'm still a bit hazy on objects.

Task.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #ifndef TASK_H
#define TASK_H
#include <string>
using namespace std;

class Task{

public:
	Task();
	~Task(){}
	void setType(string t){ type = t; }
	string getType(){ return type; }
	void setDeadline(int dl){ deadline = dl; }
	int getDeadline(){ return deadline; }
	void getDescription(string des){ description = des; }
	string setDescription(){ return description; }
	
protected:
	int deadline;
	string description;
	string type;
};

#endif 


TaskList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef TASK_LIST_H
#define TASK_LIST_H
#include "Task.h"
#include <vector>

class TaskList{
	
	public:
		TaskList(){};
		~TaskList(){};
		void addToList(Task t);
		void removeFromList(int num);
		void outputFromList();
		void getTask(int num);

	private:
		vector<Task>someTask;
};


TaskList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef TASK_LIST_CPP
#define TASK_LIST_CPP
#include "TaskList.h"
#include <vector>
#include <iostream>
using namespace std;

void TaskList::addToList(Task t){

	someTask.push_back(t);
}

void TaskList::removeFromList(int num){

	someTask.erase(someTask.begin()+num);
}

void TaskList::outputFromList(){
	
	Task aTask;
	for(unsigned int index=0; index<someTask.size(); index++){
		cout << someTask[index].aTask << endl;
	}
}

void TaskList::getTask(int num){
	Task aTask;
	cout << someTask[num-1].aTask << endl;
}

#endif 
Last edited on
Some comments on your code:
1. there is no need for the using directive in the header files, for any string data type in the headers just use std::string though you might have to #include<vector>
2. the two header files should be combined; TaskList has a vector<Task> data member, taskList (which is itself a bad idea to name a data member so similar to the Class name) and keeping the two classes in the same header and their implementations in the same source file avoids the danger of splitting them up in case you're shipping your program
3. addToList() argument should be const Task& t, removeFromList() argument const int&
4. outputFromList(), getTask() should both be const
5. taskList is vector<Task>, so before you can do cout<<taskList.at(index).aTask you need to overload the << operator for class Task, also you don't need the .at(index) but it can be written as taskList[index].aTask since [] is overloaded for vectors
6. index needs to be unsigned int and not just int since it is used in conjunction with taskList.size() that returns size_t which is an unsigned int
7. in class Task, getType(), getDeadline(), getDescription() should all be const
8. in setType() and setDescription() the arguments could be const string& and for setDeadline() const int&
@gunnerfunner thanks for the comments. There are some things that I'm confused about.

1. Why would I need to pass-by-reference and not just by value. Since I am just storing the each task into an element of the vector wouldn't pass-by-value suffice?

2. I'm not sure if creating Task object aTask makes sense in my program. What I had in mind was create a Task object that has the member variables. Then at the specific index of the vector, I can manipulate that Task by either printing it out, removing the whole task, etc. Maybe I could create it was a member variable in my class, since I'm getting compilation errors stating that there's no member aTask in Task

Side note: As per the specs of my assignment, Task is a base class with 3 different derived classes, therefore I don't think I should include it into my TaskList.

Again I appreciate your comments @gunnerfunner
Last edited on
You're welcome.

Why would I need to pass-by-reference and not just by value. Since I am just storing the each task into an element of the vector wouldn't pass-by-value suffice?

When you pass by value you're actually passing copies of the Task objects to the function and these draws resources (to make the copies) and consequently slows down your program depending on how many objects there are and the size of each object. When you pass by reference you're passing the object itself to the function but note that we have a safeguard - const - that means that the function can't make any changes to the object we pass to it unless, of course, we need it to in which case the const qualifier is dropped

I'm not sure if creating Task object aTask makes sense in my program

Probably not because someTask has already been declared as vector<Task>. So as long as you overload the insertion operator << you should be able to print out the vector elements (i.e. the Task objects) directly without having an additional Task object in between

Maybe I could create it was a member variable in my class

You can't have a class object as a non-static member of the class itself, you could have a reference or pointer to a class object as a data member though. However in your case I don't really see the need for any of these

It seems you're making some honest efforts with your assignment yourself in contrast to some who outright copy/paste their assignment and request 'help' (i.e for someone to do their assignment for them). So as you progress further with it you're welcome to circle back with any additional queries you might have.
Last edited on
Topic archived. No new replies allowed.