2015-10-27 33 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(); 
} 
相關問題