Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //application.cpp
- #include "Application.h"
- #include "Secondary.h"
- #include "Obect.h"
- using namespace std;
- void Application::build_Tree()
- {
- Object* curr_parent, * curr_child;
- string parent_name, child_name;
- cin >> parent_name;
- set_name(parent_name);
- curr_parent = this;
- cin >> parent_name >> child_name;
- while (true)
- {
- if (parent_name == child_name) return;
- if (parent_name != curr_parent->get_name())
- {
- if (parent_name != curr_parent->get_name())
- {
- curr_parent == curr_child;
- }
- else
- continue;
- }
- curr_child = new Secondary(curr_parent, child_name);
- cin >> parent_name >> child_name;
- }
- }
- Application::Application(Object* parent) : Object(parent) {}
- int Application::exec()
- {
- print();
- return 0;
- }
- //application.h
- #ifndef APPLICATION_H
- #define APPLICATION_H
- #include "Obect.h"
- class Application : public Object
- {
- public:
- void build_Tree();
- Application(Object* parent = 0);
- int exec();
- };
- #endif
- //main.cpp
- #include "Application.h"
- int main()
- {
- setlocale(LC_ALL, "RUS");
- Application obj;
- obj.build_Tree();
- return obj.exec();
- }
- //object.h
- #ifndef OBECT_H
- #define OBECT_H
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- class Object
- {
- string name;
- Object* parent;
- vector<Object*> children;
- public:
- Object(Object*, string = "root");
- void set_parent(Object*);
- Object* get_parent();
- void set_name(string);
- string get_name();
- void print();
- ~Object();
- };
- #endif
- //object.cpp
- #include "Obect.h"
- using namespace std;
- Object::Object(Object* parent, string name)
- {
- this->parent = NULL;
- this->name = "root";
- set_parent(parent);
- set_name(name);
- }
- void Object::set_parent(Object* p_parent)
- {
- if (parent)
- {
- for (int i = 0; i < parent->children.size(); i++)
- {
- if (parent->children[i] == this)
- parent->children.erase(children.begin() + i);
- }
- }
- parent = p_parent;
- if (parent)
- parent->children.push_back(this);
- }
- Object* Object::get_parent()
- {
- return this->parent;
- }
- void Object::set_name(string name)
- {
- this->name = name;
- }
- string Object::get_name()
- {
- return name;
- }
- void Object::print()
- {
- if (!parent) cout << get_name();
- vector<Object*>::iterator children_iterator;
- if (children.empty()) return;
- cout << endl << name;
- children_iterator = children.begin();
- while (children_iterator != children.end())
- {
- cout << " " << (*children_iterator)->get_name();
- children_iterator++;
- }
- children_iterator--;
- (*children_iterator)->print();
- }
- Object::~Object()
- {
- for (int i = 0; i < children.size(); i++)
- {
- delete children[i];
- }
- }
- //secondary.cpp
- #include <iostream>
- #include <string>
- #include "Secondary.h"
- using namespace std;
- Secondary::Secondary(Object* parent, string name) : Object(parent, name) {}
- //secondary.h
- #ifndef SECONDARY_H
- #define SECONDARY_H
- #include "Obect.h"
- class Secondary : public Object
- {
- public:
- Secondary(Object*, string);
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement