fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Клас Гра
  5. class Game {
  6. public:
  7. string title;
  8.  
  9. Game(string t) {
  10. title = t;
  11. }
  12.  
  13. void show() {
  14. cout << "Гра: " << title << endl;
  15. }
  16. };
  17.  
  18. // Клас Гравець
  19. class Player {
  20. private:
  21. Game* game; // агрегація: посилання на гру
  22.  
  23. public:
  24. Player(Game* g) {
  25. game = g;
  26. }
  27.  
  28. void info() {
  29. cout << "Гравець грає у ";
  30. game->show();
  31. }
  32. };
  33.  
  34. int main() {
  35. Game csgo("CS:GO"); // гра існує окремо
  36. Player player1(&csgo); // гравець посилається на гру
  37.  
  38. player1.info();
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Гравець грає у Гра: CS:GO