《C++游戏编程入门》第9章 高级类与动态内存:Game Lobby

03-18 阅读 0评论

《C++游戏编程入门》第9章 高级类与动态内存:Game Lobby

    • 9.1 使用聚合体
        • 09.critter_farm.cpp
        • 9.2 使用友元函数与运算符重载
            • 09.friend_critter.cpp
            • 9.3 动态分配内存
                • 09.heap.cpp
                • 9.4 使用数据成员与堆
                    • 09.heap_data_member.cpp
                    • 9.5 Game Lobby程序
                        • 09.game_lobby.cpp

                          9.1 使用聚合体

                          对象的组合,对象成员变量含其他对象。

                          《C++游戏编程入门》第9章 高级类与动态内存:Game Lobby,《C++游戏编程入门》第9章 高级类与动态内存:Game Lobby,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,访问,游戏,存储,第1张
                          (图片来源网络,侵删)
                          09.critter_farm.cpp
                          #include 
                          #include 
                          #include 
                          using namespace std;
                          class Critter
                          {
                          public:
                              Critter(const string &name = "");
                              string GetName() const;
                          private:
                              string m_Name; // 对象包含关系
                          };
                          Critter::Critter(const string &name)
                              : m_Name(name)
                          {
                          }
                          inline string Critter::GetName() const
                          {
                              return m_Name;
                          }
                          class Farm
                          {
                          public:
                              Farm(int spaces = 1);
                              void Add(const Critter &aCritter);
                              void RollCall() const;
                          private:
                              vector m_Critters;
                          };
                          Farm::Farm(int spaces)
                          {
                              m_Critters.reserve(spaces);
                          }
                          void Farm::Add(const Critter &aCritter)
                          {
                              m_Critters.push_back(aCritter);
                          }
                          void Farm::RollCall() const
                          {
                              for (vector::const_iterator iter = m_Critters.begin(); iter != m_Critters.end(); ++iter)
                                  cout GetName() 
                              Critter crit("Poochie");
                              cout 
                              // 友元函数
                              friend void Peek(const Critter &aCritter);
                              friend ostream &operator
                          }
                          void Peek(const Critter &aCritter);
                          ostream &operator
                              Critter crit("Poochie");
                              cout 
                              cout 
                              os 
                              int *pHeap = new int;//new运算符
                              *pHeap = 10;
                              cout 
                              int *pTemp = new int(20);//分配同时初始化
                              return pTemp;
                          }
                          void leak1()
                          {
                              int *drip1 = new int(30);
                          }
                          void leak2()
                          {
                              int *drip2 = new int(50);
                              drip2 = new int(100);
                              delete drip2;
                          }
                          
                          public:
                              Critter(const string &name = "", int age = 0); // 构造函数
                              ~Critter();                                    // 析构函数
                              Critter(const Critter &c);                     // 拷贝构造函数
                              Critter &operator=(const Critter &c);          // 拷贝赋值运算符
                              void Greet() const;
                          private:
                              string *m_pName;
                              int m_Age;
                          };
                          Critter::Critter(const string &name, int age)
                          {
                              cout 
                              cout 
                              cout 
                              cout 
                                  delete m_pName;
                                  m_pName = new string(*(c.m_pName));
                                  m_Age = c.m_Age;
                              }
                              return *this;
                          }
                          void Critter::Greet() const
                          {
                              cout 
                              testDestructor();
                              cout 
                              Critter toDestroy("Rover", 3);
                              toDestroy.Greet();
                          }
                          void testCopyConstructor(Critter aCopy)
                          {
                              aCopy.Greet();
                          }
                          void testAssignmentOp()
                          {
                              Critter crit1("crit1", 7);
                              Critter crit2("crit2", 9);
                              crit1 = crit2;
                              crit1.Greet();
                              crit2.Greet();
                              cout 
                          public:
                              Player(const string &name = "");
                              string GetName() const;
                              Player *GetNext() const;
                              void SetNext(Player *next);
                          private:
                              string m_Name;
                              Player *m_pNext; // Pointer to next player in list
                          };
                          Player::Player(const string &name)
                              : m_Name(name), m_pNext(nullptr)
                          {
                          }
                          string Player::GetName() const
                          {
                              return m_Name;
                          }
                          Player *Player::GetNext() const
                          {
                              return m_pNext;
                          }
                          void Player::SetNext(Player *next)
                          {
                              m_pNext = next;
                          }
                          class Lobby
                          {
                              friend ostream &operator
                          }
                          Lobby::~Lobby()
                          {
                              Clear();
                          }
                          void Lobby::AddPlayer()
                          {
                              // create a new player node
                              cout 
                                  m_pHead = pNewPlayer;
                              }
                              // otherwise find the end of the list and add the player there
                              else
                              {
                                  Player *pIter = m_pHead;
                                  while (pIter-GetNext() != nullptr)
                                      pIter = pIter-GetNext();
                                  pIter-SetNext(pNewPlayer);
                              }
                          }
                          void Lobby::RemovePlayer()
                          {
                              if (m_pHead == nullptr)
                              {
                                  cout 
                                  Player *pTemp = m_pHead;
                                  m_pHead = m_pHead-GetNext();
                                  delete pTemp;
                              }
                          }
                          void Lobby::Clear()
                          {
                              while (m_pHead != nullptr)
                                  RemovePlayer();
                          }
                          ostream &operator
                              Player *pIter = aLobby.m_pHead;
                              os 
                                  os 
                                  while (pIter != nullptr)
                                  {
                                      os 
                              Lobby myLobby;
                              int choice;
                              do
                              {
                                  cout 
                                  case 0:
                                      cout 

免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,人围观)

还没有评论,来说两句吧...

目录[+]