2017-04-19 12 views
1

私のSnack.cpp、スナックヘッダーファイル、MiniVendヘッダーファイル& miniVend.cppファイルに基づいて、私は合計値を返す金額*価格を生成するために私の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からifを呼び出す必要があります。私は私のminiVendクラスから合計値を決定する必要があります。 – Timk10

+0

秘密の日付の参照を返しますが、これは本当に悪いです。 – CroCo

答えて

0

getSnackPrice()はパブリックであると仮定すると、そしてSnack.hが存在します、あなたはただ電話することができるはずです

snackObject.getSnackPrice() * ammount 
+0

miniVendヘッダーファイルにsnackObjectアイテムを作成する必要がありますか? – Timk10

+0

私はおそらく、スナップの配列やマップをgetValueOfSnacks()関数に渡し、それを繰り返し処理します。それは本当にあなたが自動販売機にあなたのデータをどのように保存するかにかかっています。 – rhysvo

0

あなたが必要とするものは、友人のキーワードです。あなただけのget()を実装していない理由を私は本当に理解していない

friend class className; 
+0

は、残念ながらまだ継承をカバーしていないので、私はそれを使用することはできません:(それは100X容易になるだろう – Timk10

+0

あなたのようなプロキシクラスを使用することができます。クラス名*変数名= reinterpret_castは&PrivateVariableClassを、この方法では、プライベート変数をアクセスもできます。 –

0

を定義しますか?私的なデータへのアクセスは本当に悪いです。あなたはカプセル化を破っています。しかし、あなたが本当に知りたい(つまり、あなたがそれをすべきではない、それは本当に悪いです)場合

#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 obj; x = obj.get();' – CroCo

+0

私は個人的なデータの値を返すメンバ関数 'get()'を実装しています。しかし、私はそのクラスへのアクセス権を他のクラスから得る必要があります。 – Timk10

関連する問題