如何在 C++ 中的函数定义中调用 class
How to call a class within a function definition in c++
这是我第一次寻求编程方面的帮助。数周以来,我一直在为我的编程 class 开发一个注册程序,其中涉及 classes。这让我很沮丧。我必须使用两个 classes:StoreItem 和 Register。 StoreItem 处理商店销售的一小部分商品。收银机 class 主要处理物品的处理,制作总账单并要求用户用现金支付。
这是 StoreItem.cpp 文件:
//function definition
#include <string>
#include <iostream>
#include "StoreItem.h"
#include "Register.h"
using namespace std;
StoreItem::StoreItem(string , double)
{
//sets the price of the current item
MSRP;
}
void StoreItem::SetDiscount(double)
{
// sets the discount percentage
MSRP * Discount;
}
double StoreItem::GetPrice()
{ // return the price including discounts
return Discount * MSRP;
}
double StoreItem::GetMSRP()
{
//returns the msrp
return MSRP;
}
string StoreItem::GetItemName()
{
//returns item name
return ItemName;
}
StoreItem::~StoreItem()
{
//deletes storeitem when done
}
这是 Register.cpp:
请注意,此函数中的最后 5 个函数定义尚未完成...
// definition of the register header
#include "Register.h"
#include "StoreItem.h"
using namespace std;
Register::Register()
{ // sets the initial cash in register to 400
CashInRegister = 400;
}
Register::Register(double)
{ //accepts initial specific amount
CashInRegister ;
}
void Register::NewTransAction()
{ //sets up the register for a new customer transaction (1 per checkout)
int NewTransactionCounter = 0;
NewTransactionCounter++;
}
void Register::ScanItem(StoreItem)
{ // adds item to current transaction
StoreItem.GetPrice();
// this probably isnt correct....
}
double Register::RegisterBalance()
{
// returns the current amount in the register
}
double Register::GetTransActionTotal()
{
// returns total of current transaction
}
double Register::AcceptCash(double)
{
// accepts case from customer for transaction. returns change
}
void Register::PrintReciept()
{
// Prints all the items in the transaction and price when finsished
}
Register::~Register()
{
// deletes register
}
我的主要问题是 Register::ScanItem(StoreItem)... 有没有办法正确地将 storeItem Class 中的函数调用到 Register scanitem 函数中?
你有:
void Register::ScanItem(StoreItem)
{ // adds item to current transaction
StoreItem.GetPrice();
// this probably isnt correct....
}
这意味着 ScanItem
函数接受一个 StoreItem
类型的参数。在 C++ 中,您可以只指定类型并让编译器满意。但是如果你打算使用这个参数,你必须给它一个名字。例如:
void Register::ScanItem(StoreItem item)
{
std::cout << item.GetItemName() << " costs " << item.GetPrice() << std::endl;
}
为了能够调用作为参数传递的对象的成员函数,您需要命名参数,而不仅仅是其类型。
我怀疑你想要
void Register::ScanItem(StoreItem item)
{
total += item.GetPrice();
}
这是我第一次寻求编程方面的帮助。数周以来,我一直在为我的编程 class 开发一个注册程序,其中涉及 classes。这让我很沮丧。我必须使用两个 classes:StoreItem 和 Register。 StoreItem 处理商店销售的一小部分商品。收银机 class 主要处理物品的处理,制作总账单并要求用户用现金支付。 这是 StoreItem.cpp 文件:
//function definition
#include <string>
#include <iostream>
#include "StoreItem.h"
#include "Register.h"
using namespace std;
StoreItem::StoreItem(string , double)
{
//sets the price of the current item
MSRP;
}
void StoreItem::SetDiscount(double)
{
// sets the discount percentage
MSRP * Discount;
}
double StoreItem::GetPrice()
{ // return the price including discounts
return Discount * MSRP;
}
double StoreItem::GetMSRP()
{
//returns the msrp
return MSRP;
}
string StoreItem::GetItemName()
{
//returns item name
return ItemName;
}
StoreItem::~StoreItem()
{
//deletes storeitem when done
}
这是 Register.cpp: 请注意,此函数中的最后 5 个函数定义尚未完成...
// definition of the register header
#include "Register.h"
#include "StoreItem.h"
using namespace std;
Register::Register()
{ // sets the initial cash in register to 400
CashInRegister = 400;
}
Register::Register(double)
{ //accepts initial specific amount
CashInRegister ;
}
void Register::NewTransAction()
{ //sets up the register for a new customer transaction (1 per checkout)
int NewTransactionCounter = 0;
NewTransactionCounter++;
}
void Register::ScanItem(StoreItem)
{ // adds item to current transaction
StoreItem.GetPrice();
// this probably isnt correct....
}
double Register::RegisterBalance()
{
// returns the current amount in the register
}
double Register::GetTransActionTotal()
{
// returns total of current transaction
}
double Register::AcceptCash(double)
{
// accepts case from customer for transaction. returns change
}
void Register::PrintReciept()
{
// Prints all the items in the transaction and price when finsished
}
Register::~Register()
{
// deletes register
}
我的主要问题是 Register::ScanItem(StoreItem)... 有没有办法正确地将 storeItem Class 中的函数调用到 Register scanitem 函数中?
你有:
void Register::ScanItem(StoreItem)
{ // adds item to current transaction
StoreItem.GetPrice();
// this probably isnt correct....
}
这意味着 ScanItem
函数接受一个 StoreItem
类型的参数。在 C++ 中,您可以只指定类型并让编译器满意。但是如果你打算使用这个参数,你必须给它一个名字。例如:
void Register::ScanItem(StoreItem item)
{
std::cout << item.GetItemName() << " costs " << item.GetPrice() << std::endl;
}
为了能够调用作为参数传递的对象的成员函数,您需要命名参数,而不仅仅是其类型。
我怀疑你想要
void Register::ScanItem(StoreItem item)
{
total += item.GetPrice();
}