Advertisement
jusohatam

Untitled

Mar 23rd, 2021
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class BaseObject {
  8. private:
  9. string name;
  10. BaseObject * root;
  11.  
  12. public:
  13. vector<BaseObject*> childs;
  14. vector<BaseObject*>::iterator it_child;
  15.  
  16. BaseObject(BaseObject * root = nullptr, string name = "");
  17. void set_name(string& name);
  18. string get_name();
  19. void set_root(BaseObject * new_root = nullptr);
  20. BaseObject * get_root();
  21. void add_child(BaseObject * child);
  22.  
  23. void show_object_tree();
  24. void show_object_next(BaseObject * root, int level);
  25. };
  26.  
  27. void BaseObject::set_name(string& new_name) {
  28. this->name = new_name;
  29. }
  30.  
  31. string BaseObject::get_name() {
  32. return this->name;
  33. }
  34.  
  35. void BaseObject::set_root(BaseObject *new_root) {
  36. if (new_root) {
  37. root = new_root;
  38. root->add_child(this);
  39. }
  40. }
  41.  
  42. BaseObject::BaseObject(BaseObject *root, string name) {
  43. set_name(name);
  44.  
  45. set_root(root);
  46.  
  47. }
  48.  
  49. void BaseObject::add_child(BaseObject *child) {
  50. childs.push_back(child);
  51. }
  52.  
  53. void BaseObject::show_object_tree() {
  54. int level = 0;
  55. show_object_next(get_root(), level);
  56. }
  57.  
  58. void BaseObject::show_object_next(BaseObject *root, int level) {
  59. cout << root->get_name() << "\n";
  60. if (root->childs.empty()) {
  61. return;
  62. }
  63. root->it_child = root->childs.begin();
  64. while (root->it_child != root->childs.end()) {
  65. show_object_next(*(root->it_child), level++);
  66. root->it_child++;
  67. }
  68. }
  69.  
  70.  
  71. class Application : public BaseObject {
  72. public:
  73. void build_tree_objects();
  74. int exec_app();
  75. void show_object_tree();
  76.  
  77. private:
  78. void show_object_next(BaseObject * root, int level);
  79. };
  80.  
  81. void Application::build_tree_objects() {
  82. string main_root_name, root_name, child_name;
  83. cin >> main_root_name;
  84. this->set_root(new BaseObject(nullptr, main_root_name));
  85.  
  86. }
  87.  
  88. int main() {
  89. Application app;
  90. app.build_tree_objects();
  91. return app.exec_app();
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement