将值传递给其他类

Passing values to other classes

本文关键字:其他 值传      更新时间:2024-09-21

我必须编写两个类,一个类允许用户存储棒棒糖的重量:

#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 << "nnPlease input the lollipop's weight: ";
cin >> this->weight;
while (this->weight <= 0)
{
cout << "nnThe input is invalid, please try again with a number greater than 0..."
"nnPlease 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);
}

另一个是将棒棒糖的重量储存在袋子里,然后将重量加在一起。然后,它将允许用户比较两个袋子的重量。我该如何继续从一个班级到另一个班级分担重量?

  • LollipopgetWeight方法应该是公共的。您可以将Lollipop设置为结构,这样所有成员和方法默认情况下都是公共的,或者设置为类,在这种情况下,您必须明确声明类的哪些部分是公共的
  • 您可以创建一个具有专用std::vector<Lollipop> bag成员的LollipopBag结构/类来存储棒棒糖
  • 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]; }
void add(const Lollipop& lollipop) { bag.push_back(lollipop); }
size_t size() const { return bag.size(); }
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 << "nn[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 << "nn[2] Bag weight: " << bag.weight();
// Comparing lollipops
if (bag[0] < bag[1])
{
std::cout << fmt::format("nnLollipop 0 ({}) weighs less than lollipop 1 ({})",
bag[0].getWeight(), bag[1].getWeight());
}
}
}
// Outputs:
//
//   [1] Bag weight: 6
//
//   [2] Bag weight: 15
//
//   Lollipop 0 (4) weighs less than lollipop 1 (5)
  • 为了使上面的选项2起作用,我添加了一个explicit Lollipop(double w)构造函数,这样您就可以简单地从double创建一个棒棒糖。否则,您将不得不创建一个空的,并通过changeWeight给它一个值
explicit Lollipop(double w) : weight{w} {}
  • 顺便说一句,由于Lollipop结构/类只有一个成员,它是一个double,所以它可以简化很多:
    • 如果不需要构造函数接收double,就可以去掉其他构造函数、析构函数和赋值运算符的所有代码,并使结构/类成为聚合
    • 如果保留它,则可以默认其他构造函数、析构函数和赋值运算符
    • 您也可以默认使用三元比较操作符(太空船操作符(来比较Lollipop对象
Lollipop() = default;
explicit Lollipop(double w) : weight{w} {}
Lollipop(const Lollipop& l) = default;
Lollipop& operator=(const Lollipop& l) = default;
~Lollipop() = default;
auto operator<=>(const Lollipop& l) const = default;

【演示】