fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Person
  6. {
  7. protected:
  8. string m_name;
  9.  
  10. public:
  11.  
  12. Person(const string &name)
  13. : m_name(name)
  14. {
  15. cout << "Creating Person Class" << endl;
  16. }
  17.  
  18. void about_me()
  19. {
  20. cout << "I am a person, my name is " << m_name << endl;
  21. }
  22. };
  23.  
  24. class Student : protected Person
  25. {
  26. private:
  27. int m_id;
  28. string m_school;
  29.  
  30. public:
  31.  
  32. Student(const string &name, int id, const string &school)
  33. : Person(name), m_id(id), m_school(school)
  34. {
  35. cout << "Creating Student Class" << endl;
  36. }
  37.  
  38. void about_me()
  39. {
  40. cout << "I am a student, my name is " << m_name << ", my id is " << m_id << " at " << m_school << endl;
  41. }
  42. };
  43.  
  44. int main()
  45. {
  46. Person* pperson = new Person("John Doe");
  47. Student* pstudent = new Student("Jane Doe", 12345, "Some School");
  48.  
  49. pperson->about_me();
  50. pstudent->about_me();
  51.  
  52. pperson-> about_me();
  53.  
  54. ((Student*)pperson)-> about_me();
  55.  
  56. delete pstudent;
  57. delete pperson;
  58.  
  59. return 0;
  60. }
Runtime error #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Creating Person Class
Creating Person Class
Creating Student Class
I am a person, my name is John Doe
I am a student, my name is Jane Doe, my id is 12345 at Some School
I am a person, my name is John Doe