在cpp中实例化一个新对象
instantiating a new object in cpp
我有 3 个 class 叫做 Starter、Pizza 和 Dessert,它们在创建对象时接受可变数量的字符串输入,例如,
//pizza takes 2 inputs
Pizza p("margarita","large size");
//starter takes 3 inputs
Starter s("ribs","bbq sauce","small size");
但我想使用函数 add() 创建一个 new 对象,该函数接受一个字符串并将其与 class 匹配以创建一个新对象。例如
add(string type)
{
if(type == "Pizza")
{
Pizza *p = new Pizza();
}
else if(type == "Starter ")
{
Starter *p = new Starter ();
}
}
现在我的问题是,如何以用户友好的方式向 classes 提供输入?通过用户友好,我认为用户可以将 class 的所有输入写在一行中,而不是使用 cin 来获取每个输入。
说我们要披萨,然后是我不想要的,
cout<<"What type of pizza";
cin>>*input* <<endl;
cout<<"What size";
cin>>*input* <<endl;
我想将所有输入写在一行中,例如,
输入 "margarita","large"
// Read complete string.
// Eg. margarita large
string order;
getline(cin, order);
// It automatically parses string based on space
istringstream is(order);
string meal, size;
is >> meal;
is >> size;
感谢@MuratKarakus。只是扩展他的答案以支持这种类型的输入 "margarita","large"
// Read complete string.
// Eg. margarita large
string order;
getline(cin, order);
std::replace( order.begin(), order.end(), ',', ' '); // this'll replace all ',' with space
// It automatically parses string based on space
istringstream is(order);
string meal, size;
is >> meal;
is >> size;
--------更新
下面的代码是为了支持像"1/2 margarita 1/2 bbq delux", "large"
这样的输入
// Read complete string.
// Eg. margarita large
string order;
getline(cin, order);
std::replace( order.begin(), order.end(), ' ', '-'); // this'll replace all space with '-'
std::replace( order.begin(), order.end(), ',', ' '); // this'll replace all ',' with space
// It automatically parses string based on space
istringstream is(order);
string meal, size;
is >> meal;
std::replace( meal.begin(), meal.end(), '-', ' '); // this'll replace all '-' with space
is >> size;
我有 3 个 class 叫做 Starter、Pizza 和 Dessert,它们在创建对象时接受可变数量的字符串输入,例如,
//pizza takes 2 inputs
Pizza p("margarita","large size");
//starter takes 3 inputs
Starter s("ribs","bbq sauce","small size");
但我想使用函数 add() 创建一个 new 对象,该函数接受一个字符串并将其与 class 匹配以创建一个新对象。例如
add(string type)
{
if(type == "Pizza")
{
Pizza *p = new Pizza();
}
else if(type == "Starter ")
{
Starter *p = new Starter ();
}
}
现在我的问题是,如何以用户友好的方式向 classes 提供输入?通过用户友好,我认为用户可以将 class 的所有输入写在一行中,而不是使用 cin 来获取每个输入。
说我们要披萨,然后是我不想要的,
cout<<"What type of pizza";
cin>>*input* <<endl;
cout<<"What size";
cin>>*input* <<endl;
我想将所有输入写在一行中,例如,
输入 "margarita","large"
// Read complete string.
// Eg. margarita large
string order;
getline(cin, order);
// It automatically parses string based on space
istringstream is(order);
string meal, size;
is >> meal;
is >> size;
感谢@MuratKarakus。只是扩展他的答案以支持这种类型的输入 "margarita","large"
// Read complete string.
// Eg. margarita large
string order;
getline(cin, order);
std::replace( order.begin(), order.end(), ',', ' '); // this'll replace all ',' with space
// It automatically parses string based on space
istringstream is(order);
string meal, size;
is >> meal;
is >> size;
--------更新
下面的代码是为了支持像"1/2 margarita 1/2 bbq delux", "large"
// Read complete string.
// Eg. margarita large
string order;
getline(cin, order);
std::replace( order.begin(), order.end(), ' ', '-'); // this'll replace all space with '-'
std::replace( order.begin(), order.end(), ',', ' '); // this'll replace all ',' with space
// It automatically parses string based on space
istringstream is(order);
string meal, size;
is >> meal;
std::replace( meal.begin(), meal.end(), '-', ' '); // this'll replace all '-' with space
is >> size;