简体   繁体   English

将值传递给其他类

[英]Passing values to other classes

I have to code two classes, a class that allows the user to store the weight of a lollipop:我必须编写两个类,一个 class 允许用户存储棒棒糖的重量:

    #include "Lollipop.h"

Lollipop::Lollipop()
{
    this->weight = 0.0; //weight in grams
}

Lollipop::Lollipop(const Lollipop & aWeight)
{
    this->weight = aWeight.weight;
}


Lollipop::~Lollipop()
{
}

Lollipop & Lollipop::operator=(const Lollipop & aWeight)
{
    this->weight = aWeight.weight;
    return(*this);
}

void Lollipop::changeWeight()
{
    cout << "\n\nPlease input the lollipop's weight: ";
    cin >> this->weight;
    while (this->weight <= 0)
    {
        cout << "\n\nThe input is invalid, please try again with a number greater than 0..."
            "\n\nPlease input the lollipop's weight: ";
        cin >> this->weight;
    }
}

double Lollipop::getWeight()
{
    return (this->weight);
}

void Lollipop::showWeight() const
{
    cout << "This is the weight of the lollipop: " << this->weight << "g" << endl;
}

bool Lollipop::equalW(const Lollipop & aWeight) const
{
    cout << "The lollipops are of equal weight.";
    return (this->weight == aWeight.weight);
}

bool Lollipop::notEqualW(const Lollipop & aWeight) const
{
    cout << "The lollipops are not of equal weight.";
    return (this->weight != aWeight.weight);
}

ostream & operator<<(ostream& out, const Lollipop & aWeight)
{
    out << aWeight.weight;
    return(out);
}

istream & operator>>(istream & in, Lollipop & aWeight)
{
    in >> aWeight.weight;
    return(in);
}

bool Lollipop::operator==(const Lollipop& aWeight) const
{
    return (this->weight == aWeight.weight);
}

bool Lollipop::operator==(bool Assign) const
{
    return (this->weight == Assign);
}

bool operator==(double value, const Lollipop& aWeight)
{
    return (value == aWeight.weight);
}

bool Lollipop::operator!=(const Lollipop& aWeight) const
{
    return (this->weight != aWeight.weight);
}

bool Lollipop::operator!=(bool Assign) const
{
    return (this->weight != Assign);
}

bool operator!=(double value, const Lollipop& aWeight)
{
    return (value != aWeight.weight);
}

And another that will take the weight of those lollipops and store them in a bag, to then add the weight all together.另一个将承受棒棒糖的重量并将它们存放在袋子中,然后将重量加在一起。 It will then allow the user to compare the weight of two bags.然后它将允许用户比较两个袋子的重量。 How would I go on about sharing the weight from one class to another?我将如何 go 将重量从一个 class 分配给另一个?

  • Lollipop 's getWeight method should be public. LollipopgetWeight方法应该是公开的。 You can make Lollipop a struct, so all the members and methods are public by default, or a class, in which case you explicitly have to declare what parts of the class are public.您可以使Lollipop成为一个结构,因此所有成员和方法默认都是公共的,或者是 class,在这种情况下,您必须明确声明 class 的哪些部分是公共的。
  • You could create a LollipopBag struct/class having a private std::vector<Lollipop> bag member to store the lollipops.您可以创建一个具有私有std::vector<Lollipop> bag成员的LollipopBag结构/类来存储棒棒糖。
  • LollipopBag should define some methods to allow a user to add lollipops to the bag and calculate the total weight, at least. LollipopBag至少应该定义一些方法以允许用户将棒棒糖添加到袋子中并计算总重量。
class LollipopBag
{
public:
    LollipopBag() = default;
    LollipopBag(size_t n) : bag(n) {}    
    Lollipop& operator[](size_t i) { return bag[i]; }
    const Lollipop& operator[](size_t i) const { return bag[i]; }
    size_t size() const { return bag.size(); }
    void add(const Lollipop& lollipop) { bag.push_back(lollipop); }
    double weight() const { return std::accumulate(std::begin(bag), std::end(bag), 0.0,
        [](auto total, const auto& b) { return total + b.getWeight(); }); }
private:
    std::vector<Lollipop> bag{};
};

int main()
{
    // Option 1
    {
        LollipopBag bag{3};  // creating a bag with 3 lollipops
        for (size_t i{0}; i < bag.size(); ++i)  // for each element in the bag
        {
            auto& lollipop{ bag[i] };
            lollipop.changeWeight();  // change the weight of the lollipop using changeWeight
        }
        std::cout << "\n\n[1] Bag weight: " << bag.weight();
    }

    // Option 2
    {
        LollipopBag bag{};  // creating an empty bag of lollipops
        for (auto&& weight : { 4.0, 5.0, 6.0 })
        {
            bag.add(Lollipop{weight});  // add 3 lollipops using a constructor accepting a double
        }
        std::cout << "\n\n[2] Bag weight: " << bag.weight();
    }
}

// Outputs:
//
//   [1] Bag weight: 6
//
//   [2] Bag weight: 15
  • For the option 2 above to work, I've added an explicit Lollipop(double w) constructor, so that you can create a lollipop simply from a double.为了使上面的选项 2 起作用,我添加了一个explicit Lollipop(double w)构造函数,这样您就可以简单地从一个 double 创建一个棒棒糖。 Otherwise, you'd have to create an empty one and the give it a value through changeWeight .否则,您必须创建一个空的并通过changeWeight给它一个值。
explicit Lollipop(double w) : weight{w} {}

[Demo] [演示]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM