代码拉取完成,页面将自动刷新
#include <iostream>
#include <memory>
class Sofa {
public:
virtual ~Sofa() = default;
virtual void show() = 0;
};
class ModernSofa : public Sofa {
public:
void show() override {
std::cout << "modern sofa\n";
}
};
class ClassicalSofa : public Sofa {
public:
void show() override {
std::cout << "classical sofa\n";
}
};
class Chair {
public:
virtual ~Chair() = default;
virtual void show() = 0;
};
class ModernChair : public Chair {
public:
void show() override {
std::cout << "modern chair\n";
}
};
class ClassicalChair : public Chair {
public:
void show() override {
std::cout << "classical chair\n";
}
};
class Factory {
public:
virtual ~Factory() = default;
virtual Sofa *createSofa() = 0;
virtual Chair *createChair() = 0;
};
class ModernFactory : public Factory {
public:
Sofa *createSofa() override {
return new ModernSofa();
}
Chair *createChair() override {
return new ModernChair();
}
};
class ClassicalFactory : public Factory {
public:
Sofa *createSofa() override {
return new ClassicalSofa();
}
Chair *createChair() override {
return new ClassicalChair();
}
};
class Orders {
public:
void addOrder(std::string type) {
std::unique_ptr<Factory> factory;
if (type == "modern") {
factory = std::make_unique<ModernFactory>();
} else if (type == "classical") {
factory = std::make_unique<ClassicalFactory>();
}
if (factory) {
std::unique_ptr<Chair> chair(std::move(factory->createChair()));
std::unique_ptr<Sofa> sofa(std::move(factory->createSofa()));
chair->show();
sofa->show();
}
}
};
int main() {
Orders orders;
int n;
std::cin >> n;
while (n--) {
std::string type;
std::cin >> type;
orders.addOrder(type);
}
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。