【发布时间】:2016-08-09 08:42:48
【问题描述】:
自从我上次编写代码以来已经有一段时间了,但我正在努力整理一下我在学习时获得的一些技能。现在,我只是想为我在网上看到的陈述/问题实施解决方案。
为此,我正在尝试构建一个过敏类来存储用户输入提供的信息(类别、名称、症状)。我开始只是为每个参数输入字符串,但在现实世界中,人们可能有多种症状。为此,我想为症状创建一个列表参数而不是单个字符串。这是我的文件:
Allergy.hpp:
#ifndef Allergy_hpp
#define Allergy_hpp
#include <iostream>
#include <string>
#include <list>
using namespace std;
class Allergy {
public:
Allergy();
Allergy(string, string, list <string>);
~Allergy();
//getters
string getCategory() const;
string getName() const;
list <string> getSymptom() const;
private:
string newCategory;
string newName;
list <string> newSymptom;
};
#endif /* Allergy_hpp */
Allergy.cpp:
#include "Allergy.hpp"
Allergy::Allergy(string name, string category, list <string> symptom){
newName = name;
newCategory = category;
newSymptom = symptom;
}
Allergy::~Allergy(){
}
//getters
string Allergy::getName() const{
return newName;
}
string Allergy::getCategory() const{
return newCategory;
}
list Allergy::getSymptom() const{
return newSymptom;
}
main.cpp:
#include <iostream>
#include <string>
#include "Allergy.hpp"
using namespace std;
int main() {
string name;
string category;
string symptom;
cout << "Enter allergy name: ";
getline(cin, name);
cout << "Enter allergy category: ";
getline(cin, category);
cout << "Enter allergy symptom: ";
getline(cin, symptom);
Allergy Allergy_1(name, category, symptom);
cout << endl << "Allergy Name: " << Allergy_1.getName() << endl <<
"Allergy Category: " << Allergy_1.getCategory() << endl <<
"Allergy Symptom: " << Allergy_1.getSymptom() << endl;
return 0;
}
我还没有在 main.cpp 中实现。现在我一直在为 Allergy.cpp 中的列表创建一个吸气剂。非常感谢任何指导!!!
【问题讨论】:
标签: c++ list class parameters getter