将值传递给其他类
Passing values to other classes
我必须编写两个类,一个类允许用户存储棒棒糖的重量:
#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);
}
另一个是将棒棒糖的重量储存在袋子里,然后将重量加在一起。然后,它将允许用户比较两个袋子的重量。我该如何继续从一个班级到另一个班级分担重量?
Lollipop
的getWeight
方法应该是公共的。您可以将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;
【演示】
相关文章:
- 为什么可以将左值传递给"std::async",即使它引用了右值
- 如何将特定字符视为其他值?
- 当我尝试将范围值存储为 8 位的固定宽度整数时,它向我显示一些其他值 [ASCII]
- 创建一个 const std::vector ,其中包含另一个 const std::vector 和其他值
- 当值传递给C++中的运算符重载函数时会发生什么
- 为什么第一行会出现零以外的其他值?
- 如何将非可变参数值传递给 fmt::format?
- 将 qml 项目值传递给 cpp 后如何与它们进行交互?
- NDK:将文本字段值传递给 c++ 字符串
- 如何从用户输入中获取某些值并忽略其他值
- 为什么我不能继续使用 std::cin 向同一变量输入其他值?
- 如何将值传递给 lambda 函数
- 如何在C++中将std::string值传递给RegSetValueEx()
- 如何将 c ++ 变量的值传递给 hadoop HDFS 的 bash 系统命令?
- C++ 通过参数将值传递给 char 数组
- 如何将值传递给MFC消息映射函数on_command
- 使用JNI将长值传递给Java
- sprintf,不打印时间(0)和其他值
- types.h中的内容——编译器在哪里定义int、带符号int和其他值的宽度
- C++文件中的$include,以及代码中的其他$值