2017-04-19 205 views
1

基于我的Snack.cpp,Snack头文件,MiniVend头文件& miniVend.cpp文件,我试图将我的Snack私有成员 - 价格移动到我的MiniVend.cpp文件中以生成金额*价格以返回总值在我的机器中的项目。我如何从另一个班级获得价格?我的miniVend.cpp的如何将私有成员变量传递给另一个类?

部分文件

double miniVend::valueOfSnacks() 
    { 

     return //// I don't know how to get snacks price in here? I need to access snacks & getSnackPrice. 

    } 

miniVend头

#ifndef MINIVEND 
    #define MINIVEND 
    #include <string> 
    #include "VendSlot.h" 
    #include "Snack.h" 
    using std::string; 

    class miniVend 
    { 
    public: 
     miniVend(VendSlot, VendSlot, VendSlot, VendSlot, double); //constructor 
     int numEmptySlots(); 
     double valueOfSnacks(); 
     //void buySnack(int); 
     double getMoney(); 
     ~miniVend(); //desructor 


    private: 
     VendSlot vendslot1; //declare all the vending slots. 
     VendSlot vendslot2; //declare all the vending slots. 
     VendSlot vendslot3; //declare all the vending slots. 
     VendSlot vendslot4; //declare all the vending slots. 
     double moneyInMachine; //money in the machine 

    }; 
    #endif // !MINIVEND 

Snack.cpp

#include "Snack.h" 
    #include <iostream> 
    #include <string> 

    using std::endl; 
    using std::string; 
    using std::cout; 
    using std::cin; 

    Snack::Snack() //default constructor 
    { 
     nameOfSnack = "bottled water"; 
     snackPrice = 1.75; 
     numOfCalories = 0; 
    } 

    Snack::Snack(string name, double price, int cals) 
    { 
     nameOfSnack = name; 
     snackPrice = price; 
     numOfCalories = cals; 

    } 

    Snack::~Snack() 
    { 

    } 

    string Snack::getNameOfSnack() 
    { 
     return nameOfSnack; 
    } 

    double Snack::getSnackPrice() 
    { 
     return snackPrice; 
    } 

    int Snack::getNumOfCalories() 
    { 
     return numOfCalories; 
    } 

Snack.h file 
#ifndef SNACK_CPP 
#define SNACK_CPP 
#include <string> 
using std::string; 

class Snack 
{ 
private: 
    string nameOfSnack; 
    double snackPrice; 
    int numOfCalories; 

public: 
    Snack(); //default constructor 
    Snack(string name, double price, int cals); //overload constructor 
    ~Snack(); //destructor 

       //Accessor functions 

    string getNameOfSnack(); //returns name of snack 
    double getSnackPrice(); //returns the price of the snack 
    int getNumOfCalories(); //returns number of calories of snack 
}; 



#endif // !SNACK_CPP 
+0

你叫'getSnackPrice()'的零食很明显? – immibis

+0

虽然我需要从miniVend调用。我需要从我的miniVend课程中确定总价值。 – Timk10

+0

返回私人日期的引用,但这真的很糟糕。 – CroCo

回答

0

假设getSnackPrice()是公共的,和Snack.h确实存在,你应该只能打电话

snackObject.getSnackPrice() * ammount 
+0

我是否必须在miniVend头文件中创建snackObject项目? – Timk10

+0

我可能会将数组或零食地图传递给getValueOfSnacks()函数,然后遍历它。这一切都取决于您如何将数据存储在自动售货机中。 – rhysvo

0

你需要的是朋友关键字。定义

friend class className; 
+0

不幸的是,我们还没有涉及继承,所以我不能使用它:(这将是100倍更容易。 – Timk10

+0

您可以使用代理类如:ClassName * VariableName = reinterpret_cast &PrivateVariableClass;这样你可以访问私有变量。 –

0

我真的不明白你为什么不只是实施get()?访问私人数据非常糟糕。你打破封装。但是,如果你真的想知道(即你不应该这样做,这是非常糟糕的),那么你可以返回到私人数据的引用如下图所示

#include <iostream> 

class A 
{ 
public: 
    A(int a) : x(a) {} 
    int &getPrivateDataBAD() { return x; } 
    void print() { std::cout << x << std::endl; } 
private: 
    int x; 
}; 

class B 
{ 
public: 
    void print(int &s) { std::cout << s << std::endl; } 
}; 

int main() 
{ 
    A obj(2); 
    B bObj; 
    bObj.print(obj.getPrivateDataBAD()); 

    return 0; 
} 
+0

你的意思是在VendSlot中实现get函数来将零食价格移动到那里? – Timk10

+0

对不起,但我没有看看代码,我只是回答了标题,我说的是如果你想要获取A类的私有数据,然后实现一个成员函数'get()'返回私有数据的值,'A obj; x = obj.get();' – CroCo

+0

不用担心 - 我确实有一个获取函数,但是我需要从另一个类完全访问该函数。 – Timk10

相关问题