【问题标题】:How to store values from previous classes into a vector in c++?如何将来自先前类的值存储到 C++ 中的向量中?
【发布时间】:2016-11-06 20:53:53
【问题描述】:

我是编程新手,目前遇到一个问题。我目前在通过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;
}

类事件任务

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();
}

类任务列表

   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();}

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

【问题讨论】:

  • 嗨,这个问题看起来像家庭作业。我相信你还没有做更多的研究或学习足够的 C++ 教程。 cprogramming.com/tutorial/stl/vector.html
  • 作为一般性评论,我会将您的输入和输出与您的实际课程分开。

标签: c++ class inheritance vector polymorphism


【解决方案1】:

您可以使用 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() {
...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多