Good day.
Task:
Determine the class hierarchy (according to option).
Define a static component in a class โ a pointer to the beginning of the linked list of objects and a static function for viewing the list.
Implement classes.
Write a demonstration program in which objects of different classes are created and placed in a list, after which the list is viewed.
main:
int main() { Worker worker1 = Worker(); worker1.setFio((char *) "WorkerFIO"); worker1.setAge(143); List test = List(); test.add(worker1); printArr(); worker1.~Worker(); } Staff class (basic, abstract)
class Staff { public: Staff() {} virtual ~Staff() {} virtual void calcSalary() = 0; void setFio(char *fio) { ... } void setAge(int age) { ... } char* getFio() { ... } int getAge() { ... } protected: /*some fields*/ }; Worker class (inherited from Staff)
class Worker : public Staff { public: Worker() {} ~Worker() {} void calcSalary() { ... } private: /* some fields */ }; List. Class objects inherited from Staff should be added to it. At the same time, there will be several classes derived from Staff; they should all be added to this list.
struct staff { Staff st; staff *next; }; static staff *begin; static void printArr() { staff *curr = begin; while (curr != NULL) { std::cout<<curr->st.getFio()<<std::endl; curr = curr->next; } } class List { private: public: List() { begin = NULL; } List(Staff st) { begin = NULL; add(st); } ~List() {} void add(Staff st) { staff *s = new staff; s->st = st; if (begin == NULL) { begin = s; } else { staff *curr = begin; while (curr->next != NULL) { curr = curr->next; } curr->next = s; } } }; The essence of the problem:
"C:\Program Files (x86)\JetBrains\CLion 2016.3.4\bin\cmake\bin\cmake.exe" --build G:\Development\C++\Lab4\cmake-build-debug --target Lab4 -- -j 4 [ 50%] Building CXX object CMakeFiles/Lab4.dir/main.cpp.obj In file included from G:\Development\C++\Lab4\main.cpp:14:0: G:\Development\C++\Lab4\List.h:12:11: error: cannot declare field 'staff::st' to be of abstract type 'Staff' Staff st; ^ In file included from G:\Development\C++\Lab4\main.cpp:13:0: G:\Development\C++\Lab4\Staff.h:4:7: note: because the following virtual functions are pure within 'Staff': class Staff { ^ G:\Development\C++\Lab4\Staff.h:9:18: note: virtual void Staff::calcSalary() virtual void calcSalary() = 0; ^ In file included from G:\Development\C++\Lab4\main.cpp:14:0: G:\Development\C++\Lab4\List.h:32:16: error: cannot declare parameter 'st' to be of abstract type 'Staff' List(Staff st) { ^ G:\Development\C++\Lab4\List.h:37:20: error: cannot declare parameter 'st' to be of abstract type 'Staff' void add(Staff st) { ^ G:\Development\C++\Lab4\List.h: In constructor 'List::List(Staff)': G:\Development\C++\Lab4\List.h:34:15: error: cannot allocate an object of abstract type 'Staff' add(st); ^ G:\Development\C++\Lab4\main.cpp: In function 'int main()': G:\Development\C++\Lab4\main.cpp:23:21: error: cannot allocate an object of abstract type 'Staff' test.add(worker1); ^ What is the problem, I realized. It is impossible to just take and create an object of an abstract class (he is an abstract one). The next question is how to add class objects derived from an abstract class to the list. There will be several derived classes.