PageRenderTime 32ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/rel-1.3.35/Examples/java/extend/example.h

#
C++ Header | 56 lines | 48 code | 7 blank | 1 comment | 2 complexity | 68fa66396bc2597b161f04b9a2d81849 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 *get_item(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. };