如何使用C++;班级成员是单独还是同时作为一个整体? 我有以下C++代码< /P>
矩形.h如何使用C++;班级成员是单独还是同时作为一个整体? 我有以下C++代码< /P>,c++,loops,collections,iteration,C++,Loops,Collections,Iteration,矩形.h class Rectangle { public: Rectangle(int _id); void draw(); int getId(); private: int id; }; 矩形.cpp #include "Rectangle.h" #include <iostream> Rectangle::Rectangle(int _id) { id = _id; } void Rectangle::draw(
class Rectangle {
public:
Rectangle(int _id);
void draw();
int getId();
private:
int id;
};
矩形.cpp
#include "Rectangle.h"
#include <iostream>
Rectangle::Rectangle(int _id) {
id = _id;
}
void Rectangle::draw() {
std::cout << "Drawing rectangle with id: " << id << std::endl;
}
int Rectangle::getId() {
return id;
}
矩形集合.cpp
#include "RectanglesCollection.h"
RectanglesCollection::RectanglesCollection() :
rectangle_00(10),
rectangle_01(20),
rectangle_02(30),
rectangle_03(40)
{}
void RectanglesCollection::update()
{
rectangle_00.draw();
rectangle_01.draw();
rectangle_02.draw();
rectangle_03.draw();
}
main.cpp
#include "Rectangle.h"
#include "RectanglesCollection.h"
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
RectanglesCollection rectangles;
rectangles.update();
std::cout << "Id of the first rectangle in collection of rectangles: " << rectangles.rectangle_00.getId() << std::endl;
return 0;
}
#包括“Rectangle.h”
#包括“矩形集合.h”
#包括
#包括
使用名称空间std;
int main(int argc,字符**argv){
矩形集合矩形;
矩形。更新();
std::cout如果在矩形集合中将矩形成员变量表示为std::array
,则可以通过索引和运算符[](size\t)
访问器成员函数轻松访问它们
#include <array>
#include <cstddef>
#include <iostream>
#include <stdexcept>
using std::array;
using std::cout;
using std::ostream;
using std::out_of_range;
using std::size_t;
// NAME_OF for String-ify.
#define NAME_OF(x) #x
class Rectangle {
int _id;
public:
Rectangle(int id);
void draw(ostream&) const;
auto id() const -> int;
};
Rectangle::Rectangle(int id_) : _id{id_} { }
void Rectangle::draw(ostream& out) const {
out << "Drawing rectangle with id: " << id() << "\n";
}
auto Rectangle::id() const -> int {
return _id;
}
class RectanglesCollection {
array<Rectangle, 4> rectangles = {10, 20, 30, 40};
public:
auto operator[](size_t i) const -> Rectangle;
void display() const;
};
auto RectanglesCollection::operator[](size_t i) const -> Rectangle {
if (i >= rectangles.size())
throw out_of_range(NAME_OF(i));
return rectangles[i];
}
void RectanglesCollection::display() const {
for (auto&& r : rectangles) {
r.draw(cout);
}
}
int main() {
auto rectangles = RectanglesCollection();
rectangles.display();
cout << "Id of the first rectangle in collection of rectangles: " << rectangles[0].id() << "\n";
}
#包括
#包括
#包括
#包括
使用std::数组;
使用std::cout;
使用std::ostream;
使用std::超出\u范围;
使用std::size\u t;
//字符串的名称\ u。
#定义(x)#x的名称
类矩形{
内部id;
公众:
矩形(int-id);
无效绘制(ostream&)常数;
自动id()常量->整数;
};
矩形::矩形(int-id):\u-id{id}{
空心矩形::绘制(ostream&out)常量{
外矩形{
如果(i>=矩形.size())
丢弃范围内的(名称(i));
返回矩形[i];
}
void RectanglesCollection::display()常量{
用于(自动(&r:矩形){
r、 绘制(cout);
}
}
int main(){
自动矩形=矩形集合();
矩形显示();
不能使用向量(如果运行时元素的数量可能发生变化)或者数组?通常在数组或其他容器上循环。只需将矩形放在某种容器中并在其上循环。您正在寻找数组。谢谢您的回答。您建议的解决方案也出现在我的脑海中。但我希望避免一些缺点。首先,我需要使用矩形like矩形[0]
而不是rectangle_xyz
,即我需要记住特定矩形放置在哪个索引处。第二个问题是我需要指定能够分配数组的矩形数量。正如我在问题中提到的,我想以某种方式自动化该任务。可能以某种方式通过sizeof
operator.@Jimatura教授•你可以制作一个自动矩形_xyz()常数->矩形{返回矩形[0];}
accessor member function来命名索引,并使用它们,而不必记住带有某种元语义的特定索引位置。如果矩形的数量是可变的,则更适合使用std::vector
,但这很难与特定命名的成员函数访问器协调。Automa使用模板
参数化可能更适合该任务。
#include <array>
#include <cstddef>
#include <iostream>
#include <stdexcept>
using std::array;
using std::cout;
using std::ostream;
using std::out_of_range;
using std::size_t;
// NAME_OF for String-ify.
#define NAME_OF(x) #x
class Rectangle {
int _id;
public:
Rectangle(int id);
void draw(ostream&) const;
auto id() const -> int;
};
Rectangle::Rectangle(int id_) : _id{id_} { }
void Rectangle::draw(ostream& out) const {
out << "Drawing rectangle with id: " << id() << "\n";
}
auto Rectangle::id() const -> int {
return _id;
}
class RectanglesCollection {
array<Rectangle, 4> rectangles = {10, 20, 30, 40};
public:
auto operator[](size_t i) const -> Rectangle;
void display() const;
};
auto RectanglesCollection::operator[](size_t i) const -> Rectangle {
if (i >= rectangles.size())
throw out_of_range(NAME_OF(i));
return rectangles[i];
}
void RectanglesCollection::display() const {
for (auto&& r : rectangles) {
r.draw(cout);
}
}
int main() {
auto rectangles = RectanglesCollection();
rectangles.display();
cout << "Id of the first rectangle in collection of rectangles: " << rectangles[0].id() << "\n";
}