模板模式
C++ 方式:
Game.h
#include
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
#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();
}chpeagle chpeagle
chp***le@126.com
5年前 (2020-08-17)
最新发布
-
“祇、祗、衹、袛”四个字,看着眼熟,容易搞混,如何区分?
2025-06-07 07:52:30 -
人才凋零?本届世界杯共27名35岁以上球员出场,创历史之最
2025-06-02 08:38:54 -
2023年淘宝喵币攻略
2025-06-29 19:19:08 -
妻子出轨了应该怎么办,最正确的做法
2025-06-27 18:23:28 -
庙寺庙的庙怎么组词(庙能组什么词语?)
2025-05-30 02:41:14 -
什么是P2P? P2P投资该注意些什么?
2025-05-03 23:54:44 -
做汤圆用什么糯米粉好 不同的汤圆做法
2025-06-21 21:29:49 -
dota跟魔兽争霸哪个早
2025-05-09 01:19:35 -
怎么裁剪视频左右边框,三种裁剪模式轻松掌握
2025-06-10 16:07:55 -
喜欢彼岸花的女生性格
2025-05-25 04:49:07