当前位置:网站首页 > 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(); }

版权声明


相关文章:

  • java达内视频教程2025-01-18 23:42:02
  • java手机开发教程2025-01-18 23:42:02
  • 自学视频教程java2025-01-18 23:42:02
  • java编辑教程2025-01-18 23:42:02
  • java画图教程2025-01-18 23:42:02
  • java web server开发教程2025-01-18 23:42:02
  • java 缓存教程2025-01-18 23:42:02
  • java528教程2025-01-18 23:42:02
  • java插件开发教程2025-01-18 23:42:02
  • java graphics教程2025-01-18 23:42:02