Учитывая классы A, B и C, я хотел бы знать, как я могу вернуть sh компоненты векторов, принадлежащих классам A и B, в один вектор, принадлежащий классу C.
Например, в следующем коде у меня есть простая программа для заказа пиццы. Программа позволяет пользователю вводить в систему новых клиентов и новые типы пиццы, после чего программа возвращает эти клиенты и векторы пиццы с этими данными.
Функция neworder () просит пользователя ввести идентификатор номера клиента и пицца, которая будет составлять заказ. Затем программа ищет эти идентификационные номера и, если есть совпадение, просит пользователя подтвердить, что эти идентификаторы представляют правильного клиента и пиццу.
Как только пользователь подтвердит, что заказ правильный, я бы хотел сделать sh возврат вектора заказов класса Order, чтобы включить компоненты вектора пиццы и вектора клиентов, которые соответствуют идентификаторам, которые дал пользователь ,
Например, если пользователь вводит идентификаторы 0 0 и подтверждает, что это правильный выбор, я хотел бы, чтобы программа отодвинула вектор заказов с обоими информацией, хранящейся в x. пиццы [0] (0, "пшеница", "аматрициана", "проволоне", "кальмар") и информация, хранящаяся в y.customers [0] {0, "Bjarne", "Stroustrup" }. (Обратите внимание, что идентификационные номера соответствуют позициям вектора.)
Каков наилучший способ сделать это? Спасибо как всегда за вашу помощь!
//Test to see how I can get a vector in one class to store elements from vectors from two other classes
using namespace std;
#include <iostream>
#include <vector>
class Pizza
{
public:
string crust;
string sauce;
//Mmm... string cheese
string cheese;
string topping;
int pizzaid;
vector<Pizza> pizzas;
void addtomenu();
Pizza(int a, string b, string c, string d, string e)
: pizzaid{a}, crust{b}, sauce{c}, cheese{d}, topping{e} {}
};
class Customer
{
public:
string firstname;
string lastname;
int customerid;
vector<Customer> customers;
void addcustomer();
Customer(int a, string b, string c)
: customerid{a}, firstname{b}, lastname{c} {}
};
class Order
{
public:
Pizza orderedpizza;
Customer customerordering;
int orderid;
vector<Order> orders;
void neworder();
void data();
Order(Pizza a, Customer b)
: orderedpizza{a}, customerordering{b} {}
};
//I added in these placeholder objects so that I could run functions/push back vectors that belong to these functions' classes. I'm not sure whether there is a more sophisticated way to do this.
//I used negative integers just in case positive ones would interfere with actual pizza/customer IDs.
Pizza x{-1, " ", " ", " ", " "};
Customer y{-1, " ", " "};
Order z{x, y};
void Pizza::addtomenu()
{
cout << "Please enter the crust, sauce, cheese, and topping that this pizza will have, all separated by spaces.\n";
cin >> crust >> sauce >> cheese >> topping;
pizzaid = pizzas.size();
x.pizzas.push_back(Pizza{pizzaid, crust, sauce, cheese, topping});
cout << "The pizza has been added.\n";
}
void Customer::addcustomer()
{
cout << "Please enter the first and last name of the customer, separated by spaces.\n";
cin >> firstname >> lastname;
customerid = customers.size();
y.customers.push_back(Customer{customerid, firstname, lastname});
cout << "The customer has been added.\n";
}
void Order::neworder()
{
int tempcustomerid;
int temppizzaid;
bool trueflag1 = 0;
bool trueflag2 = 0;
string entry;
cout << "Please enter the customer's ID, followed by the ID of the pizza being ordered. Separate the two with spaces.\n";
if (cin >> tempcustomerid >> temppizzaid)
{
for (int d = 0; d < y.customers.size(); d++)
{
if (tempcustomerid == y.customers[d].customerid)
{
trueflag1 = 1;
tempcustomerid = d;
}
}
if (trueflag1 == 0)
{
cin.clear();
cin.ignore(1000, '\n');
cout << "That ID does not match any customer in the system.\n";
return;
}
for (int e = 0; e < x.pizzas.size(); e++)
{
if (temppizzaid == x.pizzas[e].pizzaid)
{
cin.clear();
cin.ignore(1000, '\n');
trueflag2 = 1;
temppizzaid = e;
}
}
if (trueflag2 == 0)
{
cout << "That ID does not match any pizza in the system.\n";
return;
}
{
cout << "The following customer: " << y.customers[tempcustomerid].customerid << " " << y.customers[tempcustomerid].firstname << " " << y.customers[tempcustomerid].lastname << " ";
cout << "will be ordering the following pizza: " << x.pizzas[temppizzaid].pizzaid << " " << x.pizzas[temppizzaid].crust << " " << x.pizzas[temppizzaid].sauce << " " << x.pizzas[temppizzaid].cheese << " " << x.pizzas[temppizzaid].topping << ".\n";
cout << "Is that correct? If yes, press 1; if no, press 0.\n";
cin >> entry;
if (entry == "1")
{
//Here, I would like to push back my orders vector to include the contents of y.customers[tempcustomerid] and y.pizzas[temppizaid]. What would be the best way to do this?
cout << "The order has been placed.\n";
}
else
{
cout << "Order canceled.\n";
return;
}
}
}
else
{
cin.clear();
cin.ignore(10000, '\n');
cout << "Please enter your input in integer form.\n";
}
}
void Order::data()
{
cout << "Pizzas entered into system so far:\n";
for (int d = 0; d < x.pizzas.size(); d++)
{
cout << x.pizzas[d].pizzaid << " " << x.pizzas[d].crust << " " << x.pizzas[d].sauce << " " << x.pizzas[d].cheese << " " << x.pizzas[d].topping << "\n";
}
cout << "Customers entered into system so far:\n";
for (int d = 0; d < y.customers.size(); d++)
{
cout << y.customers[d].customerid << " " << y.customers[d].firstname << " " << y.customers[d].lastname << "\n";
}
}
int main()
{
//These two push_back functions are added to create test items for debugging purposes.
x.pizzas.push_back(Pizza{0, "wheat", "amatriciana", "provolone", "squid"});
y.customers.push_back(Customer{0, "Bjarne", "Stroustrup"});
string entry;
while (true)
{
cout << "Welcome to the Pizza Orderer 5000. To enter a new customer, enter addcustomer. To enter a new pizza to the menu, enter addtomenu. To enter a new order, enter order. To list all data in the system, enter data. To quit, enter quit.\n";
cin >> entry;
if (entry == "addcustomer")
y.addcustomer();
else if (entry == "addtomenu")
x.addtomenu();
else if (entry == "order")
z.neworder();
else if (entry == "data")
z.data();
else if (entry == "quit")
break;
else
{
cin.clear();
cin.ignore(10000, '\n');
cout << "I'm sorry, that input was not recognized. Please try again.\n";
}
}
}