fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class Person {
  8. public:
  9. Person();
  10. Person(std::string name, int age);
  11.  
  12. std::string getName();
  13. int getAge();
  14.  
  15. virtual void print();
  16.  
  17. private:
  18. std::string name;
  19. int age;
  20. };
  21. Person::Person() {
  22. name = "";
  23. age = 1;
  24. }
  25. Person(string name, int age) {
  26. this->name = name;
  27. this->age = age;
  28. }
  29. string Person::getName() {
  30. return name;
  31. }
  32. int Person::getAge() {
  33. return age;
  34. }
  35.  
  36. void Person::print() {
  37. cout << this->getName() << " " << this->getAge() << "\n";
  38. }
  39.  
  40. class Student : public Person {
  41. public:
  42. Student() {
  43. name = "";
  44. age = 1;
  45. major = "";
  46. }
  47. Student(std::string name, int age, string major) :Person(name, age) {
  48. this->major = major;
  49. }
  50.  
  51. string getMajor() {
  52. return major;
  53. }
  54.  
  55. void print() {
  56. cout<< this->getName() << " "<< this->getAge() <<" "<< getMajor()<<"\n" }
  57.  
  58. private:
  59. string major;
  60. };
  61.  
  62.  
  63.  
  64.  
  65. int main()
  66. {
  67. vector<Person> people;
  68.  
  69. people[0] = Person("Tom", 25);
  70. people[1] = Student("Jane", 19, "Computer");
  71. people[2] = Student("Brown", 22, "Management");
  72. people[3] = Person("Micky", 27);
  73.  
  74. for (int i = 0; i < 4; i++) {
  75. people[i].print();
  76. cout << endl;
  77. }
  78.  
  79. return 0;
  80.  
  81.  
  82.  
  83. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:25:14: error: expected ‘)’ before ‘name’
 Person(string name, int age) {
       ~      ^~~~~
              )
prog.cpp: In constructor ‘Student::Student()’:
prog.cpp:43:3: error: ‘std::__cxx11::string Person::name’ is private within this context
   name = "";
   ^~~~
prog.cpp:18:14: note: declared private here
  std::string name;
              ^~~~
prog.cpp:44:3: error: ‘int Person::age’ is private within this context
   age = 1;
   ^~~
prog.cpp:19:6: note: declared private here
  int age;
      ^~~
prog.cpp: In member function ‘virtual void Student::print()’:
prog.cpp:56:74: error: expected ‘;’ before ‘}’ token
   cout<< this->getName() << " "<< this->getAge() <<" "<< getMajor()<<"\n" }
                                                                          ^~
                                                                          ;
stdout
Standard output is empty