#include <iostream>
#include<vector>
using namespace std;
class FileSystem
{
public:
virtual void ls()=0;
virtual void openall()=0;
virtual int getsize()=0;
virtual string getname()=0;
virtual bool isfolder()=0;
virtual FileSystem* cd(string name)=0;
};
class File : public FileSystem
{
int size;
string name;
public:
File(const string& nam,int siz)
{
name = nam;
size = siz;
}
void ls()
{
cout<<" "<<getname()<<endl;
}
void openall()
{
cout<<" -"<<getname()<<endl;
}
int getsize()
{
return size;
}
string getname()
{
return name;
}
bool isfolder()
{
return false;
}
FileSystem* cd(string name)
{
return nullptr;
}
};
class Folder : public FileSystem
{
string name;
vector<FileSystem*> child;
public:
Folder(const string& _name)
{
name = _name;
}
void add(FileSystem* item)
{
child.push_back(item);
}
void ls()
{
for(auto itr: child)
{
if(itr->isfolder())
cout<<"+"<<itr->getname()<<endl;
else
cout<<" -"<<itr->getname()<<endl;
}
}
void openall()
{
cout<<" +"<<name<<endl;
for(auto itr: child)
{
itr->openall();
}
}
int getsize()
{
int totalsize=0;
for(auto itr: child)
{
totalsize+=itr->getsize();
}
return totalsize;
}
string getname()
{
return name;
}
bool isfolder()
{
return true;
}
FileSystem* cd(string name)
{
for(auto itr:child)
{
if(itr->isfolder() && itr->getname()==name)
{
return itr;
}
}
return nullptr;
}
};
int main() {
Folder* root = new Folder("root");
root->add(new File("file1.txt",1));
root->add(new File("file2.txt",1));
root->add(new File("file3.txt",1));
Folder* docs = new Folder("docs");
docs->add(new File("file4.txt",1));
docs->add(new File("file5.txt",1));
root->add(docs);
Folder* image = new Folder("image");
image->add(new File("pic1.jpg",1));
docs->add(image);
//root->openall();
docs->ls();
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: