当前位置:网站首页 > Java教程 > 正文

java模板菜鸟教程



C++ 方式:

Game.h

#include <iostream> class Game { protected: virtual void initialize() = 0; virtual void startPlay() = 0; virtual void endPlay() = 0; public: virtual void play() { initialize(); startPlay(); endPlay(); } };

Cricket.h

#include "Game.h" class Cricket : public Game { protected: virtual void initialize() { std::cout << "Cricket Game Finished!" << std::endl; } virtual void startPlay() { std::cout << "Cricket Game Initialized! Start playing." << std::endl; } virtual void endPlay() { std::cout << "Cricket Game Started. Enjoy the game!" << std::endl; } };

Football.h

#include "Game.h" class Football : public Game { protected: virtual void initialize() { std::cout << "Football Game Finished!" << std::endl; } virtual void startPlay() { std::cout << "Football Game Initialized! Start playing." << std::endl; } virtual void endPlay() { std::cout << "Football Game Started. Enjoy the game!" << std::endl; } };

Main.cpp

#include <iostream> #include "Cricket.h" #include "Football.h" int main(int argc, char* argv[]) { Game *game = new Cricket(); game->play(); std::cout << std::endl; game = new Football(); game->play(); }

版权声明


相关文章:

  • 菜鸟教程 配置java2024-12-08 16:50:06
  • java教程1672024-12-08 16:50:06
  • 超清java教程2024-12-08 16:50:06
  • 498集java教程2024-12-08 16:50:06
  • java算法教程2024-12-08 16:50:06
  • java视频教程高清2024-12-08 16:50:06
  • java加法教程2024-12-08 16:50:06
  • mac开发java教程2024-12-08 16:50:06
  • 建筑模组java教程2024-12-08 16:50:06
  • java fiddler教程2024-12-08 16:50:06