Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / lecture-hw1.cpp
diff options
context:
space:
mode:
1 files changed, 65 insertions, 0 deletions
-rw-r--r--Year 1/Object-oriented Programming/lecture-hw1.cpp65
diff --git a/Year 1/Object-oriented Programming/lecture-hw1.cpp b/Year 1/Object-oriented Programming/lecture-hw1.cpp
new file mode 100644
index 0000000..d3ded74
--- /dev/null
+++ b/Year 1/Object-oriented Programming/lecture-hw1.cpp
@@ -0,0 +1,65 @@
+#include <iostream>
+#include <cstring>
+
+class Command
+{
+ char name;
+
+public:
+ Command(char name);
+ char getName() const;
+};
+
+Command::Command(char name)
+{
+ this->name = name;
+}
+
+char Command::getName() const
+{
+ return name;
+}
+
+class CommandList
+{
+ Command** commands;
+ int commandCount;
+
+public:
+ CommandList(const char* commands);
+ ~CommandList();
+
+ void printAll() const;
+};
+
+CommandList::CommandList(const char* commands)
+{
+ commandCount = strlen(commands);
+ this->commands = new Command*[commandCount];
+
+ for (int i = 0; i < commandCount; i++)
+ {
+ Command* ptr = new Command(commands[i]);
+ this->commands[i] = ptr;
+ }
+}
+
+CommandList::~CommandList()
+{
+ for (int i = 0; i < commandCount; i++)
+ delete commands[i];
+ delete[] commands;
+}
+
+void CommandList::printAll() const
+{
+ for (int i = 0; i < commandCount; i++)
+ std::cout << commands[i]->getName() << " ";
+ std::cout << std::endl;
+}
+
+int main()
+{
+ CommandList cl("abcdefghijklmnop");
+ cl.printAll();
+}