在 C++ 中访问一个类的多个实例
Posted
技术标签:
【中文标题】在 C++ 中访问一个类的多个实例【英文标题】:Accessing multiple instances of a class in C++ 【发布时间】:2020-04-06 09:36:30 【问题描述】:我正在为一家餐厅创建一个菜单,每个类别可以有 5 道菜。到目前为止,我已经为肉类菜肴创建了一个类,并且最多可以添加 5 道菜,每道菜都有一个唯一的标识符。我遇到的问题是在创建对象后访问它们。 (会有多个类别,因此为什么到目前为止只有一个 case 的 switch 语句)。
例如,我将如何实现更改第二道菜的描述的方法?
到目前为止,这是我的代码:
肉.h
class Meat
private:
int meatNumber;
std::string meatCategory;
std::string meatDescription[MAX_ITEMS];
double meatPrice[MAX_ITEMS];
public:
Meat();
//setter functions
int setMeatNumber();
std::string setMeatDescription();
double setMeatPrice();
//getter functions
int getMeatNumber();
std::string getMeatCategory();
std::string getMeatDescription(int i);
double getMeatPrice(int i);
;
肉.cpp
#include "Meat.h"
//constructor
Meat::Meat()
meatNumber = 0;
meatCategory = "Meat";
meatDescription[MAX_ITEMS] = "No description written.";
meatPrice[MAX_ITEMS] = 0.0;
//setter functions
int Meat::setMeatNumber()
static int counter = 1;
meatNumber = counter++;
std::string Meat::setMeatDescription()
int i = 0;
std::cout << "Please enter a short description: " << std::endl;
std::cin >> meatDescription[i];
return meatDescription[i];
double Meat::setMeatPrice()
int i = 0;
std::cout << "Please set the price in a 00.00 format: " << std::endl;
std::cout << "£";
while(!(std::cin >> meatPrice[i]))
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Error. Please enter a number: ";
return meatPrice[i];
//getter functions
int Meat::getMeatNumber() return meatNumber;
std::string Meat::getMeatCategory() return meatCategory;
std::string Meat::getMeatDescription(int i) return meatDescription[i];
double Meat::getMeatPrice(int i) return meatPrice[i];
main.cpp
#include <iostream>
#include "Meat.h"
int main()
int choice;
std::cout << "Menu Creation Terminal\n\n" << std::endl;
std::cout << "\t Welcome\nto Wrapid™ Restaurants\n\n" << std::endl;
std::cout << "1. Add Meat Dish\n2. Add Fish Dish\n3. Add Vegetarian Dish\n4. Add Drink\n"
"5. Edit Current Menu\n6. Quit\n\n" << std::endl;
std::cout << "Please select an option: ";
std::cin >> choice;
switch (choice)
case 1:
int option = true;
int count = 0, i;
Meat meatDish;
std::cout << "Meat Dishes" << std::endl;
while (true)
meatDish.setMeatNumber();
meatDish.setMeatDescription();
meatDish.setMeatPrice();
//functions to add details to dish
std::cout << "You have added the following dish: " << std::endl;
std::cout << "Item number: \n" << meatDish.getMeatNumber() << std::endl;
std::cout << "Item Category: \n " << meatDish.getMeatCategory() << std::endl;
std::cout << "Item Description: \n" << meatDish.getMeatDescription(i) << std::endl;
std::cout << "Item Price: \n £" << meatDish.getMeatPrice(i) << std::endl;
std::cout << "Would you like to add another item? Press 1 for yes or 2 for no: " << std::endl;
std::cin >> option;
count += 1;
if (count == 5)
std::cout << "Error. Exceeded maximum items.";
break;
//breaks out of loop if more than 5 items
if (option == 2) break; //breaks out of loop when user is finished adding items
//while loop to contain menu
//brace for scope of case 1
return 0;
【问题讨论】:
【参考方案1】:当您使用 c++ 类 Meat
时,您可以使用 []
来实例化 N 个项目
例如5
对象
Meat meats[5];
如果你想修改第二个对象,那么
meats[1].setMeatDescription(<pass argument>);
您需要使用 this
关键字更改该方法
this->meatDescription = <pass argument>;
无需将meatDescription[]
创建为数组
使用这个代码https://pastebin.com/bCkzbFZV你可以使用meats[i].getMeatDescription()
【讨论】:
您好,感谢您的详细回复以及您对代码所做的所有更改。这非常有效。【参考方案2】:您可以创建一个名为 DishesContainer 的新类。这个类可能有:
一个私有的 std::vector => 它将保存每个实例 创建新菜的公共函数 一个公共函数,用于更改菜肉中的任何类型的值。例如更改描述
class DishContainer
public:
void ChangeDescription(int indexMeat, std::string newDescription)
meats_[indexMeat].setMeatDescription(newDescription);
private:
std::vector<Meat> meats_;
【讨论】:
以上是关于在 C++ 中访问一个类的多个实例的主要内容,如果未能解决你的问题,请参考以下文章