fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <memory>
  4.  
  5. using namespace std;
  6.  
  7. // Base is an abstract base class.
  8. class Base {
  9. private:
  10. string myName; // Name of this person
  11. public:
  12. Base(string name) : myName(name) {}
  13. virtual ~Base() {}
  14.  
  15. // A pure virtual function with a function body
  16. virtual void hello() const {
  17. cout << "Hello, my name is " << myName << ".";
  18. }
  19. };
  20.  
  21. class ServiceAgent : public Base {
  22. public:
  23. ServiceAgent(string name) : Base(name) {}
  24.  
  25. void hello() const override {
  26. Base::hello();
  27. cout << " I'm a customer service representative. How may I help you?" << endl;
  28. }
  29. };
  30.  
  31. class Student : public Base {
  32. public:
  33. enum category { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR };
  34. category personType;
  35.  
  36. Student(string name, category pType) : Base(name), personType(pType) {}
  37.  
  38. void hello() const override {
  39. Base::hello();
  40. cout << " " << enumToString(personType) << endl;
  41. }
  42.  
  43. string enumToString (category c) const
  44. {
  45. switch (c) {
  46. case FRESHMAN:
  47. return "I'm a Freshman";
  48. case SOPHOMORE:
  49. return "I'm a Sophomore";
  50. case JUNIOR:
  51. return "I'm a Junior";
  52. case SENIOR:
  53. return "I'm a Senior";
  54. }
  55. return "";
  56. }
  57. };
  58.  
  59. class CSStudent : public Student {
  60. public:
  61. CSStudent(string name, Student::category type) : Student(name, type) {}
  62.  
  63. void hello() const override {
  64. Student::hello();
  65. cout << "I'm a computer science major." << endl;
  66. }
  67. };
  68.  
  69. class BUSStudent : public Student {
  70. public :
  71. BUSStudent(string name, Student::category type) : Student(name, type) {}
  72.  
  73. void hello() const override {
  74. Student::hello();
  75. cout << "I'm a business major." << endl;
  76. }
  77. };
  78.  
  79. int main() {
  80.  
  81. std::unique_ptr<ServiceAgent> agentJack(new ServiceAgent("Jacqueline"));
  82. std::unique_ptr<Student> studentJack(new Student("Jackson", Student::FRESHMAN));
  83. std::unique_ptr<CSStudent> studentCSS(new CSStudent("Jack", Student::SOPHOMORE));
  84. std::unique_ptr<BUSStudent> studentBus1(new BUSStudent("Jacky", Student::JUNIOR));
  85. std::unique_ptr<BUSStudent> studentBus2(new BUSStudent("Joyce", Student::SENIOR));
  86.  
  87. std::unique_ptr<Base> arr[] = { std::move(agentJack), std::move(studentJack), std::move(studentCSS), std::move(studentBus1), std::move(studentBus2) };
  88.  
  89. for (auto &var : arr)
  90. {
  91. var->hello();
  92. cout << "-----" << endl;
  93. }
  94.  
  95. return 0;
  96. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Hello, my name is Jacqueline. I'm a customer service representative. How may I help you?
-----
Hello, my name is Jackson. I'm a Freshman
-----
Hello, my name is Jack. I'm a Sophomore
I'm a computer science major.
-----
Hello, my name is Jacky. I'm a Junior
I'm a business major.
-----
Hello, my name is Joyce. I'm a Senior
I'm a business major.
-----