fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Player
  5. {
  6. private:
  7. std::string name;
  8. std::string password;
  9. int hScore = 0;
  10. int totalGames = 0;
  11. int totalScore = 0;
  12. int avgScore = 0;
  13. //...
  14.  
  15. public:
  16. //...
  17.  
  18. class Info
  19. {
  20. const Player &m_player;
  21.  
  22. void print(std::ostream &os) const {
  23. os << "name: " << m_player.name << "\n"
  24. << "password: " << m_player.password << "\n"
  25. << "score: " << m_player.hScore << "\n";
  26. }
  27.  
  28. public:
  29. Info(const Player &player) : m_player(player) {}
  30.  
  31. friend std::ostream& operator<<(std::ostream &os, const Info &info)
  32. {
  33. info.print(os);
  34. return os;
  35. }
  36. };
  37. friend class Info;
  38.  
  39. struct Stats
  40. {
  41. const Player &m_player;
  42.  
  43. void print(std::ostream &os) const
  44. {
  45. os << "total games: " << m_player.totalGames << "\n"
  46. << "total score: " << m_player.totalScore << "\n"
  47. << "avg score: " << m_player.avgScore << "\n";
  48. }
  49.  
  50. public:
  51. Stats(const Player &player) : m_player(player) {}
  52.  
  53. friend std::ostream& operator<<(std::ostream &os, const Stats &stats)
  54. {
  55. stats.print(os);
  56. return os;
  57. }
  58. };
  59. friend class Stats;
  60. };
  61.  
  62. int main()
  63. {
  64. Player p;
  65.  
  66. std::cout << "Player Info:" << std::endl;
  67. std::cout << Player::Info(p);
  68.  
  69. std::cout << std::endl;
  70.  
  71. std::cout << "Player Stats:" << std::endl;
  72. std::cout << Player::Stats(p);
  73.  
  74. return 0;
  75. }
Success #stdin #stdout 0s 5440KB
stdin
Standard input is empty
stdout
Player Info:
name: 
password: 
score: 0

Player Stats:
total games: 0
total score: 0
avg score: 0