fork download
  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Person
  6. {
  7. private:
  8. string name_;
  9.  
  10. public:
  11. string name() const { return name_; }
  12.  
  13. Person( string const& name )
  14. : name_( name )
  15. {}
  16. };
  17.  
  18. class Student
  19. : public Person
  20. {
  21. private:
  22. int studentId_;
  23.  
  24. public:
  25. int studentId() const { return studentId_; }
  26.  
  27. Student( string const& name, int const studentId )
  28. : Person( name ), studentId_( studentId )
  29. {}
  30. };
  31.  
  32. int main()
  33. {
  34. Person a( "Hillary Clinton" );
  35. Student b( "John Smith", 12345 );
  36. Person& bAsPerson = b;
  37.  
  38. bAsPerson = a;
  39. cout
  40. << "Name: " << b.name()
  41. << ". Student id: " << b.studentId() << "."
  42. << endl;
  43. }
  44.  
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
Name: Hillary Clinton. Student id: 12345.