Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- class BaseObject {
- private:
- string name;
- BaseObject * root;
- public:
- vector<BaseObject*> childs;
- vector<BaseObject*>::iterator it_child;
- BaseObject(BaseObject * root = nullptr, string name = "");
- void set_name(string& name);
- string get_name();
- void set_root(BaseObject * new_root = nullptr);
- BaseObject * get_root();
- void add_child(BaseObject * child);
- void show_object_tree();
- void show_object_next(BaseObject * root, int level);
- };
- void BaseObject::set_name(string& new_name) {
- this->name = new_name;
- }
- string BaseObject::get_name() {
- return this->name;
- }
- void BaseObject::set_root(BaseObject *new_root) {
- if (new_root) {
- root = new_root;
- root->add_child(this);
- }
- }
- BaseObject::BaseObject(BaseObject *root, string name) {
- set_name(name);
- set_root(root);
- }
- void BaseObject::add_child(BaseObject *child) {
- childs.push_back(child);
- }
- void BaseObject::show_object_tree() {
- int level = 0;
- show_object_next(get_root(), level);
- }
- void BaseObject::show_object_next(BaseObject *root, int level) {
- cout << root->get_name() << "\n";
- if (root->childs.empty()) {
- return;
- }
- root->it_child = root->childs.begin();
- while (root->it_child != root->childs.end()) {
- show_object_next(*(root->it_child), level++);
- root->it_child++;
- }
- }
- class Application : public BaseObject {
- public:
- void build_tree_objects();
- int exec_app();
- void show_object_tree();
- private:
- void show_object_next(BaseObject * root, int level);
- };
- void Application::build_tree_objects() {
- string main_root_name, root_name, child_name;
- cin >> main_root_name;
- this->set_root(new BaseObject(nullptr, main_root_name));
- }
- int main() {
- Application app;
- app.build_tree_objects();
- return app.exec_app();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement