【问题标题】:How to call a class within a function definition in c++如何在 C++ 中的函数定义中调用类
【发布时间】:2016-01-26 10:20:11
【问题描述】:

这是我第一次在编程方面寻求帮助。几周以来,我一直在为我的编程课程制定一个注册程序,其中涉及课程。这对我来说相当令人沮丧。我必须使用两个类:StoreItem 和Register。 StoreItem 处理商店销售的一小部分商品。收银机类主要处理物品的处理,制作总账单并要求用户用现金支付。 这是 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 类中的函数正确调用到 Register scanitem 函数中?

【问题讨论】:

    标签: c++ function class constructor destructor


    【解决方案1】:

    你有:

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

    【讨论】:

      【解决方案2】:

      为了能够调用作为参数传递的对象的成员函数,您需要命名参数,而不仅仅是其类型。

      我怀疑你想要类似的东西

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

      【讨论】:

        猜你喜欢
        • 2021-04-15
        • 1970-01-01
        • 2021-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-26
        • 2020-12-27
        • 1970-01-01
        相关资源
        最近更新 更多