簡體   English   中英

如何將私有成員變量傳遞給另一個類?

[英]How to pass a private member variable to another class?

基於我的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

假設getSnackPrice()是公共的,並且Snack.h確實存在,那么你應該可以調用它

snackObject.getSnackPrice() * ammount

你需要的是朋友關鍵詞。 定義

  friend class className;

我真的不明白為什么你不只是實現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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM