class Seller
{
private:
float salestotal; // run total of sales in dollars
int lapTopSold; // running total of lap top computers sold
int deskTopSold; // running total of desk top computers sold
int tabletSold; // running total of tablet computers sold
string name; // name of the seller
Seller::Seller(string newname)
{
name = newname;
salestotal = 0.0;
lapTopSold = 0;
deskTopSold = 0;
tabletSold = 0;
}
bool Seller::SellerHasName ( string nameToSearch )
{
if(name == nameToSearch)
return true;
else
return false;
}
class SellerList
{
private:
int num; // current number of salespeople in the list
Seller salespeople[MAX_SELLERS];
public:
// default constructor to make an empty list
SellerList()
{
num = 0;
}
// member functions
// If a salesperson with thisname is in the SellerList, this
// function returns the associated index; otherwise, return NOT_FOUND.
// Params: in
int Find ( string thisName );
void Add(string sellerName);
void Output(string sellerName);
};
int SellerList::Find(string thisName)
{
for(int i = 0; i < MAX_SELLERS; i++)
if(salespeople[i].SellerHasName(thisName))
return i;
return NOT_FOUND;
}
// Add a salesperson to the salespeople list IF the list is not full
// and if the list doesn't already contain the same name.
void SellerList::Add(string sellerName)
{
Seller(sellerName);
num++;
}
我的SellerList类中的函数中的参数存在一些问题 . 我想在salesperson数组中添加一个人,所以我记录了我的所有卖家...... Bob,Pam,Tim等...我的构造函数Seller(sellerName)创建了一个名为sellerName的卖家 .
如何将此Seller添加到Salespeople数组,并具有将数据拉回并在更多函数(如Update函数或输出函数)中使用它的方法?
MAX_SELLERS = 10 ....我想我的问题是不知道是否只使用Add(string)或Add(Seller,string)参数 . 任何帮助,将不胜感激 .
3 回答
也许是这样的?
Not reinvent the wheel . 选择适合您问题的容器 . 在这种情况下,因为您正在通过
std::string
引用/搜索Seller
,我建议您使用像std::unordered_map
这样的哈希表(如果您无权访问C 11,则使用std::map
搜索树):如何在SellerList中使用STD向量而不是数组 .
你可以做
x.push_back(Seller(...))
或x[0].SellerHasName()
和x.size()
会给你卖家的数量 .