Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* * * *
- * Напишите обьевление класса для связного списка класса узлов обьектов типа
- * ( Worker , *** Pencil , Cat ... ) и его реализацию, включив у нее такие методы :
- * Insert () , Delete () , Display(), Find() и оператор []. протестировать в главной программе
- *
- * * * * */
- #include<iostream>
- #include<cstdlib>
- #include<string>
- using namespace std;
- class Employee;
- class EmployeeList;
- struct Node
- {
- Node * next;
- Employee * employee;
- };
- class Employee
- {
- private:
- string name;
- int age;
- int days_of_employment;
- int salary;
- public:
- // Setters and Getters
- void set_name(string name) { this->name = name; }
- string get_name() { return this->name; }
- void set_age(int age) { this->age = age; }
- int get_age() { return this->age; }
- void set_days_of_employment(int days) { this->days_of_employment = days; }
- int get_days_of_employment() { return this->days_of_employment; }
- void set_salary(int salary) { this->salary = salary; }
- int get_salary() { return this->salary; }
- // Output methods
- string description()
- {
- return "Employee: " + this->name + "\n" +
- " age: " + to_string(this->age) + "\n" +
- " days: " + to_string(this->days_of_employment) + "\n" +
- " salary: " + to_string(this->salary) +"\n\n";
- }
- void print_description() { std::cout << description(); }
- // Constructor
- Employee( string name, int age = 0, int days_of_employment = 0, int salary = 0 )
- {
- this->name = name;
- this->age = age;
- this->days_of_employment = days_of_employment;
- this->salary = salary;
- }
- };
- class EmployeeList
- /* Employee linked List */
- {
- public:
- int length;
- Node * head;
- EmployeeList();
- ~EmployeeList();
- Employee * operator [](unsigned int i)
- {
- // As of now return the first employee
- return head->employee;
- }
- void add_employee( Employee *employee );
- void delete_employee( Employee *employee);
- void print();
- };
- EmployeeList::EmployeeList()
- {
- this->length = 0;
- this->head = nullptr;
- }
- EmployeeList::~EmployeeList() { }
- void EmployeeList::add_employee( Employee * employee)
- {
- Node * node = new Node();
- node->employee = employee;
- node->next = this->head;
- this->head = node;
- this->length++;
- }
- void EmployeeList::delete_employee(Employee * employee)
- {
- // 1. find employee index
- Node * head = this->head;
- /*
- for( Node *iterator = head; iterator; iterator = iterator->next)
- {
- std::cout << iterator->employee->description();
- //std::cout << "Employee address: " << employee-> << std::endl;
- //std::cout << "Iterator address: " << iterator->employee << std::endl;
- std::cout << "Employee address: " << iterator->employee << std::endl;
- std::cout << "Iterator next: " << iterator->next << std::endl;
- if( iterator->next == nullptr )
- {
- std::cout << "THIS IS THE END!!!" << std::endl;
- }
- }
- // 2. check if it is the first one
- */
- }
- void EmployeeList::print()
- {
- Node * head = this->head;
- int i = 1;
- while(head)
- {
- cout << head->employee->description();
- head = head->next;
- i++;
- }
- }
- int main(int argc, char const *argv[])
- {
- Employee employee1 = Employee("Anatoly Shirokov", 31, 365, 280000),
- employee2 = Employee( "DarkPerl", 32, 780, 180000),
- employee3 = Employee("Illia Zviagin", 35, 180, 220000),
- employee4 = Employee("Alex", 18, 12, 6000);
- EmployeeList * list = new EmployeeList();
- list->add_employee( &employee1);
- list->add_employee( &employee4);
- list->add_employee( &employee2);
- list->add_employee( &employee3);
- list->delete_employee( &employee4);
- //list->print();
- std::cout << (&list[0])->description;
- delete list;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement