2024.11.6(周三)
用透明组合模式实现教材中的“文件夹浏览”这个例子。
实验要求:
1.文件的执行不需真正实现,只需简单提示即可;
2.提交源代码;
3.注意编程规范。
1、类图
2、源代码
#include <iostream>
#include <string>
#include<list>
using namespace std;
class AbstractFile
{
public:
virtual void add() { }
virtual void remove() { }
virtual void display() { }
};
class ImageFile : public AbstractFile
{
private:
string fileName;
public:
ImageFile(string filename)
{
fileName = filename;
}
void add()
{
cout << "添加成功" << endl;
}
void remove()
{
cout << "删除成功" << endl;
}
void display()
{
cout << fileName << endl;
}
};
class TextFile : public AbstractFile
{
private:
string fileName;
public:
TextFile(string filename)
{
fileName = filename;
}
void add()
{
cout << "添加成功" << endl;
}
void remove()
{
cout << "删除成功" << endl;
}
void display()
{
cout << fileName << endl;
}
};
class VideoFile : public AbstractFile
{
private:
string fileName;
public:
VideoFile(string filename)
{
fileName = filename;
}
void add()
{
cout << "添加成功" << endl;
}
void remove()
{
cout << "删除成功" << endl;
}
void display()
{
cout << fileName << endl;
}
};
class Folder : public AbstractFile
{
private:
string fileName;
int level;
list<AbstractFile*> abstractfiles;
public:
Folder(string filename)
{
fileName = filename;
}
Folder(string filename, int level)
{
fileName = filename;
this->level = level;
}
void add(AbstractFile* abstractfile)
{
abstractfiles.push_front(abstractfile);
}
void remove(AbstractFile* abstractfile)
{
abstractfiles.remove(abstractfile);
}
void display()
{
cout << fileName << endl;
list<AbstractFile*>::iterator iter = abstractfiles.begin();
for (; iter != abstractfiles.end(); iter++)
{
if (this->level != 1)
{
cout << " -";
(*iter)->display();
}
else {
cout << " *";
(*iter)->display();
}
}
}
};
int main()
{
int choice;
bool flag = true;
cout << "输入根目录文件夹名:" << endl;
string str;
cin >> str;
Folder* fold3 = new Folder(str);
Folder* fold1 = NULL;
Folder* fold2 = NULL;
Folder* fold4 = NULL;
AbstractFile* obj1 = NULL;
AbstractFile* obj2 = NULL;
AbstractFile* obj4 = NULL;
while(flag){
cout << "1、创建图像文件夹" << endl;
cout << "2、创建视频文件夹" << endl;
cout << "3、创建文本文件夹" << endl;
cout << "4、展示文件夹" << endl;
cout << "5、退出" << endl;
cout << "******请选择******" << endl;
cin >> choice;
if (choice==1) {
cout << "请输入文件夹名:";
string name;
cin >> name;
fold1 = new Folder(name, 1);
cout << "请输入文件名:";
string name1;
cin >> name1;
obj1 = new ImageFile(name1);
fold1->add(obj1);
fold3->add(fold1);
}
else if (choice == 2) {
cout << "请输入文件夹名:";
string name;
cin >> name;
fold2 = new Folder(name, 1);
cout << "请输入文件名:";
string name1;
cin >> name1;
obj2 = new VideoFile(name1);
fold2->add(obj2);
fold3->add(fold2);
}
else if (choice == 3) {
cout << "请输入文件夹名:";
string name;
cin >> name;
fold4 = new Folder(name, 1);
cout << "请输入文件名:";
string name1;
cin >> name1;
obj4 = new TextFile(name1);
fold4->add(obj4);
fold3->add(fold4);
}
else if (choice == 4) {
fold3->display();
}
else if (choice == 5) {
cout << "****再见******" << endl;
flag = false;
}
else {
cout << "输入错误!!!!" << endl;
flag = false;
}
}
delete obj1, obj2, obj4;
delete fold1, fold2, fold3, fold4;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/831587.html
如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!