PageRenderTime 23ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/d/extend/example.h

#
C++ Header | 56 lines | 48 code | 7 blank | 1 comment | 2 complexity | b55e3393f431b6ae5f4a9a67efd6b607 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* File : example.h */
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <cmath>
  7. class Employee {
  8. private:
  9. std::string name;
  10. public:
  11. Employee(const char* n): name(n) {}
  12. virtual std::string getTitle() { return getPosition() + " " + getName(); }
  13. virtual std::string getName() { return name; }
  14. virtual std::string getPosition() const { return "Employee"; }
  15. virtual ~Employee() { printf("~Employee() @ %p\n", this); }
  16. };
  17. class Manager: public Employee {
  18. public:
  19. Manager(const char* n): Employee(n) {}
  20. virtual std::string getPosition() const { return "Manager"; }
  21. };
  22. class EmployeeList {
  23. std::vector<Employee*> list;
  24. public:
  25. EmployeeList() {
  26. list.push_back(new Employee("Bob"));
  27. list.push_back(new Employee("Jane"));
  28. list.push_back(new Manager("Ted"));
  29. }
  30. void addEmployee(Employee *p) {
  31. list.push_back(p);
  32. std::cout << "New employee added. Current employees are:" << std::endl;
  33. std::vector<Employee*>::iterator i;
  34. for (i=list.begin(); i!=list.end(); i++) {
  35. std::cout << " " << (*i)->getTitle() << std::endl;
  36. }
  37. }
  38. const Employee *getItem(int i) {
  39. return list[i];
  40. }
  41. ~EmployeeList() {
  42. std::vector<Employee*>::iterator i;
  43. std::cout << "~EmployeeList, deleting " << list.size() << " employees." << std::endl;
  44. for (i=list.begin(); i!=list.end(); i++) {
  45. delete *i;
  46. }
  47. std::cout << "~EmployeeList empty." << std::endl;
  48. }
  49. };