自定义数据类型与运算符重载
1. 自定义数据类型操作示例
在实际编程中,我们常常需要处理各种自定义的数据类型。例如,在一个物流应用场景里,我们可能会定义 Box 和 Truckload 类来管理货物。下面通过一个示例来展示如何创建、操作和管理这些自定义数据类型的对象。
#include <iostream>
#include <vector>
#include <memory>
template <typename T> using ptr = std::shared_ptr<T>;
class Box {
public:
double length;
double width;
double height;
Box(double l, double w, double h) : length(l), width(w), height(h) {}
double volume() const {
return length * width * height;
}
void listBox() const {
std::cout << "Box(" << length << "," << width << "," << height << ") ";
}
};
class Truckload {
private:
class Pack
超级会员免费看
订阅专栏 解锁全文
202

被折叠的 条评论
为什么被折叠?



