include
include
include
using namespace std;
class AbstractFile {
public:
virtual void add(AbstractFile* element) = 0;
virtual void remove(AbstractFile* element) = 0;
virtual void display() = 0;
};
class VideoFile : public AbstractFile {
private:
string fileName;
public:
VideoFile(string fileName) {
this->fileName = fileName;
}
void add(AbstractFile* element) {
cout << "VideoFile:add -> " << this->fileName << endl;
}
void remove(AbstractFile* element) {
cout << "VideoFile:remove -> " << this->fileName << endl;
}
void display() {
cout << "VideoFile:display -> " << this->fileName << endl;
}
};
class TextFile :public AbstractFile {
private:
string fileName;
public:
TextFile(string fileName) {
this->fileName = fileName;
}
void add(AbstractFile* element) {
cout << "TextFile:add -> " << this->fileName << endl;
}
void remove(AbstractFile* element) {
cout << "TextFile:remove -> " << this->fileName << endl;
}
void display() {
cout << "TextFile:display -> " << this->fileName << endl;
}
};
class ImageFile :public AbstractFile {
private:
string fileName;
public:
ImageFile(string fileName) {
this->fileName = fileName;
}
void add(AbstractFile* element) {
cout << "ImageFile:add -> " << this->fileName << endl;
}
void remove(AbstractFile* element) {
cout << "ImageFile:remove -> " << this->fileName << endl;
}
void display() {
cout << "ImageFile:display -> " << this->fileName << endl;
}
};
class Folder :public AbstractFile {
private:
list<AbstractFile> _list;
string fileName;
public:
Folder(string fileName) {
this->fileName = fileName;
_list = new list<AbstractFile>;
}
~Folder() {
if (_list != NULL)
{
while (_list->empty()) {
delete (_list->begin());
_list->erase(_list->begin());
}
delete _list;
}
}
string getFileName() const {
return fileName;
}
void add(AbstractFile element) {
cout << "Folder:add";
element->display();
_list->push_back(element);
}
void remove(AbstractFile element) {
cout << "Folder:remove";
element->display();
_list->remove(element);
}
void display() {
if (_list != NULL) {
list<AbstractFile>::iterator it = _list->begin();
while (it != _list->end()) {
(it++)->display();
}
}
}
};
int main() {
AbstractFile *obj1, *obj2, *obj3, *obj4;
Folder *plate1, *plate2;
obj1 = new ImageFile("ImageFile");
obj2 = new TextFile("TextFile");
plate1 = new Folder("folder1");
plate1->add(obj1);
plate1->add(obj2);
cout << "-------------plate1中文件内容-----------------" << endl;
plate1->display();
cout << "------------------------------" << endl;
obj3 = new VideoFile("VideoFile");
plate2 = new Folder("folder2");
plate2->add(obj3);
cout << "-------------plate2中文件内容-----------------" << endl;
plate2->display();
cout << "------------------------------" << endl;
plate1->remove(obj1);
cout << "-------------plate1中文件内容-----------------" << endl;
plate1->display();
}
标签:总结,cout,11.12,11.6,AbstractFile,fileName,list,element,void From: https://www.cnblogs.com/XiMenXve/p/17825929.html