BEST代写-线上编程学术专家

Best代写-最专业靠谱代写IT | CS | 留学生作业 | 编程代写Java | Python |C/C++ | PHP | Matlab | Assignment Project Homework代写

C++代写 | CSE 111 FAQ Assignment 2

C++代写 | CSE 111 FAQ Assignment 2

这个作业是用C++完成Shell模拟

CSE 111 FAQ
Assignment 2

1. Introduction: High-Level Goal
2. FAQ
Introduction: High-Level Goal
You are building an in-memory bash shell emulator. When you start up the program, you only
have one directory: the root directory. Your shell is currently CDed into that root directory.
WARNINGS: THINGS THAT CAN GO WRONG
WARNING 1
Be careful about the following line of code:
int main(void) {
map<string, size_t> my_map;
my_map[“hello”] = 5;
if (my_map[“interesting”] == 0) {
cout << “this is interesting” << endl;
}
return 0;
}
“this is interesting” will ALWAYS PRINT OUT. Know why?
When you call std::map::operator[], if there exists no element in the map with the provided key
(in this case, “interesting”), then we will end up creating one, and the value will be initialized to
its default value (in this case, equal to zero). So, if you want to search a map to see if it has key
“interesting”, then use:
auto fiter = my_map.find(“interesting”);
if (fiter != my_map.end())
cout << “this is interesting” << endl;
WARNING 2
When recursively destroying directories, BE VERY VERY VERY careful not to iterate the “.” and
“..” entries of dirents!!! Else, you will most certainly crash your program!!!
FAQ
What should the file system look like at the start of the program?
● inode_state: contains “inode_ptr”s that point to “root” and “cwd”. “root” will never change,
but “cwd” will point to the inode that is your “current working directory”. The latter gets
changed whenever you say “cd <path>”.
● inode: Will always point to either a directory or a plain_file.
● directory: Contains a map “dirents” that maps file/directory names to an inode_ptr.
Where do we define a root node?
You create the root inode in the inode_state constructor. The inode_state class contains the
following fields:
1. inode_ptr root
2. inode_ptr cwd
You want to create a root inode of type DIRECTORY_TYPE via make_shared:
root = make_shared<inode>(/* you insert correct type here */);
How should we insert the “.” and “..” entries into directory::dirents?
Here are a couple of suggestions:
1. Make a virtual member function for base_file and directory that returns a non-const
reference to dirents, which you can use to populate dirents.
2. Do the above, but instead of retrieving a reference, pass in a pair of arguments to insert
as an entry into dirents.
3. (Mackey doesn’t like this, but…) upcast “root->contents” to a “directory_ptr”, then friend
inode_state from directory.
How do I make a virtual function?
To know how to make a virtual function, let’s first dissect the syntax of some virtual functions
already done for you:
class base_file {
protected:
base_file() = default;
virtual const string& error_file_type() const = 0;
public:
virtual ~base_file() = default;
base_file (const base_file&) = delete;
base_file& operator= (const base_file&) = delete;
virtual size_t size() const = 0;
virtual const wordvec& readfile() const;
virtual void writefile (const wordvec& newdata);
virtual void remove (const string& filename);
virtual inode_ptr mkdir (const string& dirname);
virtual inode_ptr mkfile (const string& filename);
};
“virtual” means: “A derived class can override this function”.
“ = 0” means that the function does NOT have to be implemented in base_file, but it HAS TO
BE implemented in the inheritance tree.
● As a consequence, because we do not implement “error_file_type()” or “size()”, we
cannot instantiate a “base_file” object, in other words, the following will give you a
compile error:
○ base_file my_base_file;
Now, why do we have “non-pure” virtual functions, such as “readfile”, “writefile”, etc? The reason
is: some of these functions will be overridden in directory, and the others overridden in
plain_file. If one of those two classes aren’t overriding a function, then we shouldn’t have to
implement that function in that class.
directory-overridden functions: plain_file overridden functions:
remove writefile
mkdir readfile
mkfile
To “make a virtual member function for base_file and directory”, mimic what Mackey does for
“remove”, “mkdir”, or “mkfile”, but adapt what he wrote to implement what you want. That
means:
1. Rename the function name and arguments.
2. Change the return type.
Though, please do understand what the syntax means, because you will be tested on it.
Ok… now what does “override” do?
The “override” keyword is not required, but… it serves a very special purpose! It’s a compile
time check (much like a static_assert()) that will fail the compilation if you wrote the code wrong.
Meaning, the compiler will check to make sure that you’re actually overriding a function in the
base class. What this means is that we’re searching for a function with the same signature:
● Same function name
● Same arguments
● Same return type

bestdaixie

评论已关闭。