Ai
1 Star 0 Fork 0

mktime/design-pattern-cpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
composite.cpp 1.68 KB
一键复制 编辑 原始数据 按行查看 历史
内部项目 提交于 2021-05-07 18:35 +08:00 . 几个常用模式的c++demo
#include <iostream>
#include <vector>
using namespace std;
class File {
protected:
string fname;
public:
virtual void display(int dept) = 0;
};
class TxtFile : public File{
public:
TxtFile(string name) {
fname = name;
}
void display(int dept) {
for (int i=0; i<dept; i++) {
cout << "-";
}
cout << fname << endl;
}
};
class MovFile : public File{
public:
MovFile(string name) {
fname = name;
}
void display(int dept) {
for (int i=0; i<dept; i++) {
cout << "-";
}
cout << fname << endl;
}
};
class JpegFile : public File{
public:
JpegFile(string name) {
fname = name;
}
void display(int dept) {
for (int i=0; i<dept; i++) {
cout << "-";
}
cout << fname << endl;
}
};
class Folder: public File {
private:
vector<File*> files;
public:
Folder(string name) {
fname = name;
}
void add(File* f) {
files.push_back(f);
}
void display(int dept) {
for (int i=0; i<dept; i++) {
cout << "-";
}
cout << fname << endl;
for(vector<File*>::iterator iter=files.begin(); iter!=files.end(); iter++) {
(*iter)->display(dept+1);
}
}
};
int main(int argc, char** argv) {
Folder* folder = new Folder("directory1");
folder->add(new TxtFile("1.txt"));
folder->add(new JpegFile("hmccb.jpeg"));
folder->add(new MovFile("yellow.rmvb"));
Folder* subdir = new Folder("subdir2");
subdir->add(new TxtFile("2.txt"));
subdir->add(new JpegFile("another.jpeg"));
folder->add(subdir);
folder->display(0);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mktime/design-pattern-cpp.git
git@gitee.com:mktime/design-pattern-cpp.git
mktime
design-pattern-cpp
design-pattern-cpp
master

搜索帮助