diff options
| author | Syndamia <kamen@syndamia.com> | 2024-03-20 13:20:09 +0200 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2024-03-20 13:20:09 +0200 |
| commit | cdd5b6c28b12a4763c9b1eef9ba45ca85a6ddaa2 (patch) | |
| tree | c3dca402c9203b0e06b06574bd6459b3b58d5fbd /week04/Exercise3.cpp | |
| parent | 6b7a20b652bd3388ceaac60c96479419ac2c4859 (diff) | |
| download | oop-2023-solutions-cdd5b6c28b12a4763c9b1eef9ba45ca85a6ddaa2.tar oop-2023-solutions-cdd5b6c28b12a4763c9b1eef9ba45ca85a6ddaa2.tar.gz oop-2023-solutions-cdd5b6c28b12a4763c9b1eef9ba45ca85a6ddaa2.zip | |
[w4] Solved exercises 1-5
Diffstat (limited to 'week04/Exercise3.cpp')
| -rw-r--r-- | week04/Exercise3.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/week04/Exercise3.cpp b/week04/Exercise3.cpp new file mode 100644 index 0000000..c834097 --- /dev/null +++ b/week04/Exercise3.cpp @@ -0,0 +1,43 @@ +#include <iostream> +#include <cstring> + +struct Paper { +private: + char contents[1024]; + unsigned page; + + void copyFrom(const Paper& other) { + strncpy(this->contents, other.contents, 1024); + page = other.page + 1; + } + +public: + Paper(const char* contents) { + strncpy(this->contents, contents, 1024); + page = 1; + } + + Paper(const Paper& other) { + copyFrom(other); + } +}; + +int main() { + int N; + std::cin >> N; + + Paper** papers = new Paper*[N]; + + char buffer[1024]; + std::cin.ignore(); + std::cin.getline(buffer, 1024); + papers[0] = new Paper(buffer); + for (int i = 1; i < N; i++) { + papers[i] = new Paper(*papers[0]); + } + + for (int i = 0; i < N; i++) { + delete papers[i]; + } + delete[] papers; +} |
