如何将以前类中的值存储到c ++中的向量中?

时间:2016-11-06 20:53:53

标签: c++ class inheritance vector polymorphism

我是编程新手,目前我遇到了问题。我目前仍然坚持如何在传递Tasklist类后保存要保存在向量中的输入值。

课程任务

class{void Task::description(){
string descrip;

cout<< "How would you describe this task" <<endl;
getline(cin, descrip);
cin.ignore();

}

void Task::deadline(){
int due;

cout<< "In how many days is task due?"<<endl;
cin >> due;
}

class EventTask

void EventTask::locatioin(){
string location;

cout<< "Where is the event taking place?"<<endl;
getline(cin,location);
cin.ignore();


 }

void EventTask::time(){
string time;

cout<< "What time is the event?"<<endl;
getline(cin,time);
cin.ignore();
}

类TaskList

   void Tasklist::Add_Task()
 {

string add_cmd;
cout<< "What type of Task is this? [G: Generic, E: Event]"<<endl;
cin>> add_cmd;

if (add_cmd == "g"){
    Task t;
    t.deadline();
    t.description();

}
    if (add_cmd == "e"){
    EventTask et;
    et.deadline();
    et.description();
    et.locatioin();
    et.time();

    }

main.cpp

int main(){

 Tasklist tl;
 tl.Add_Task();}

我的主要问题是如何将输入值保存到矢量中,然后输出矢量中包含的内容。

1 个答案:

答案 0 :(得分:0)

您可以使用C ++类继承功能并将字符串定义为Task类的私有变量,如下所示:

 class Task{
  private:
string location;
string add_cmd;
string description;
public:
//.... functions to set and get variable's value plus other functions  

    like description(), location() and Add_Task() ....
}
class EventTask: public Task {
...
}
class TaskList: public Task {
....
}

int main() {
...
}