From 51ff352a533ff14e9961deb642d110f2d5dc843c Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 31 Mar 2022 10:32:53 +0300 Subject: Completed first lecture homework --- Year 1/Object-oriented Programming/lecture-hw1.cpp | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Year 1/Object-oriented Programming/lecture-hw1.cpp (limited to 'Year 1/Object-oriented Programming/lecture-hw1.cpp') 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 +#include + +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(); +} -- cgit v1.3.1-23-gd82d