2015-10-27 36 views
0

这是我第一次寻求编程方面的帮助。我一直在为我的编程课进行一个注册程序,这个课程涉及几个星期。它对我来说很沮丧。我必须使用两个类:StoreItem和Register。 StoreItem处理商店销售的小项目列表。注册类主要处理这些项目,制作总帐单并要求用户用现金支付。 这里是StoreItem.cpp文件:如何在C++中调用函数定义中的类

//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所函数定义的arent完呢......

// 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类中的函数正确调用到Register scanitem函数中?

回答

0

您有:

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; 
} 
0

为了能够叫你传递作为参数的对象的成员函数,你需要命名的参数,而不仅仅是它的类型。

我怀疑你想要的东西像

void Register::ScanItem(StoreItem item) 
{ 
    total += item.GetPrice(); 
} 
相关问题