Created
December 30, 2017 14:05
-
-
Save FONQRI/aada609246dc80d5158f3fef4b54801d to your computer and use it in GitHub Desktop.
template design pattern c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
// Base class | |
// Template class | |
class Account { | |
public: | |
// Abstract Methods | |
virtual ~Account(); | |
virtual void Start() = 0; | |
virtual void Allow() = 0; | |
virtual void End() = 0; | |
virtual int MaxLimit() = 0; | |
// Template Method | |
void Withdraw(int amount) | |
{ | |
Start(); | |
int limit = MaxLimit(); | |
if (amount < limit) { | |
Allow(); | |
} | |
else { | |
cout << "Not allowed" << endl; | |
} | |
End(); | |
} | |
}; | |
Account::~Account() {} | |
// Derived class | |
class AccountNormal : public Account { | |
public: | |
~AccountNormal(); | |
void Start(); | |
void Allow(); | |
void End(); | |
int MaxLimit(); | |
}; | |
AccountNormal::~AccountNormal() {} | |
void AccountNormal::Start() { cout << "Start ..." << endl; } | |
void AccountNormal::Allow() { cout << "Allow ..." << endl; } | |
void AccountNormal::End() { cout << "End ..." << endl; } | |
int AccountNormal::MaxLimit() { return 1000; } | |
// Derived class | |
class AccountPower : public Account { | |
public: | |
~AccountPower(); | |
void Start(); | |
void Allow(); | |
void End(); | |
int MaxLimit(); | |
}; | |
AccountPower::~AccountPower() {} | |
void AccountPower::Start() { cout << "Start ..." << endl; } | |
void AccountPower::Allow() { cout << "Allow ..." << endl; } | |
void AccountPower::End() { cout << "End ..." << endl; } | |
int AccountPower::MaxLimit() { return 5000; } | |
int main() | |
{ | |
AccountPower power; | |
power.Withdraw(1500); | |
AccountNormal normal; | |
normal.Withdraw(1500); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment