1 Star 0 Fork 0

Fingsinz/DesignPatternsInCpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.cpp 973 Bytes
一键复制 编辑 原始数据 按行查看 历史
Fingsinz 提交于 2024-07-21 19:56 . updated 原型模式
#include <iostream>
#include <memory>
class Prototype {
public:
virtual ~Prototype() = default;
virtual Prototype *clone() = 0;
virtual void print() = 0;
};
class Rectangle : public Prototype {
public:
Rectangle(std::string const &color, int width, int height)
: m_color(color)
, m_width(width)
, m_height(height) {}
virtual ~Rectangle() = default;
Prototype *clone() override {
return new Rectangle(this->m_color, this->m_width, this->m_height);
}
void print() {
std::cout << "Color: " << m_color << ", Width: " << m_width << ", Height: " << m_height << std::endl;
}
private:
std::string m_color;
int m_width = 0;
int m_height = 0;
};
int main() {
std::string color;
int w, h;
std::cin >> color >> w >> h;
std::unique_ptr<Prototype> rectangle = std::make_unique<Rectangle>(color, w, h);
int n;
std::cin >> n;
for (size_t i = 0; i < n; ++i) {
std::unique_ptr<Prototype> clone(rectangle->clone());
clone->print();
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fingsinz/design-patterns-in-cpp.git
git@gitee.com:fingsinz/design-patterns-in-cpp.git
fingsinz
design-patterns-in-cpp
DesignPatternsInCpp
master

搜索帮助