简体   繁体   English

如何将私有成员变量传递给另一个类?

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

Based on my Snack.cpp, Snack header file, MiniVend header file & miniVend.cpp file, I am trying to move my Snack private member - price into my MiniVend.cpp file to generate the amount * price to return a total value of items in my machine. 基于我的Snack.cpp,Snack头文件,MiniVend头文件和miniVend.cpp文件,我试图将我的Snack私有成员 - 价格移动到我的MiniVend.cpp文件中以生成金额*价格以返回项目的总价值在我的机器上。 How do I access the price from another class? 如何从其他班级获取价格?

Portion of my miniVend.cpp file 我的miniVend.cpp文件的一部分

   double miniVend::valueOfSnacks()
    {

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

    }

miniVend header 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 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

what you need is friend keyword. 你需要的是朋友关键词。 Define the 定义

  friend class className;

I don't really understand why you don't just implement get() ? 我真的不明白为什么你不只是实现get() Accessing private data is really bad. 访问私人数据真的很糟糕。 You are breaking the encapsulation. 你打破了封装。 But if you really want to know (ie you should NOT do it, it is really BAD), then you just return a reference to a private data as shown below 但是如果你真的想知道(即你不应该这样做,那真的很糟糕),那么你只需要返回对私有数据的引用,如下所示

#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.

相关问题 如何将私有成员的对象传递给另一个类的方法? - How to pass an object of private member to another class's method? 如何在另一个类的静态成员函数中访问私有静态变量? - How to access private static variable in static member function of another class? 如何从另一个 class 访问私有成员变量,其成员变量是第一个 class 的实例? - How do I access to private member variable from another class whose member variable is instance of first class? 如何将班级成员传递给另一个班级的另一个成员 - How to pass a member of class to another member of another class 如何修复“ * private变量*是&#39;* class name *”的私有成员错误 - How to fix "*private variable* is a private member of '*class name*' error 如何在C ++中将私有整数从A类传递到B类中的另一个Int变量 - How to pass private integer from Class A to another Int Variable in Class B in C++ 如何将类成员(变量?)传递给函数? - How do pass a class member (variable?) to a function? 如何在继承类中创建作为私有成员的父类变量数组 - How to create an array of parent class variable as a private member in the inheriting class 如何访问作为另一个类的私有成员的类的方法 - How to access the method of a class which is a private member of another class 如何在class中使用vector作为私有成员来存储和传递数据 - How to use vector as a private member in class to store and pass data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM