예제 1 ¶
~cpp #include <iostream> #include <string> using namespace std; class Phone { protected: void ConnectSMSServer() { cout << "Not implemented!\n"; } void SendToSMSServer(string number, string message) { cout << "Not implemented!\n"; } void DisconnectSMSServer() { cout << "Not implemented!\n"; } public: void SendMessage(string number, string message) { ConnectSMSServer(); SendToSMSServer(number, message); DisconnectSMSServer(); } }; class SKPhone : public Phone { protected: void ConnectSMSServer() { cout << "Connect SK SMS Server.\n"; } void SendToSMSServer(string number, string message) { cout << "Send to SK SMS Server... " << number << " " << message << endl; } void DisconnectSMSServer() { cout << "Disconnect SK SMS Server.\n"; } }; class KTFPhone : public Phone { protected: void ConnectSMSServer() { cout << "Connect KTF SMS Server.\n"; } void SendToSMSServer(string number, string message) { cout << "Send to KTF SMS Server... " << number << " " << message << endl; } void DisconnectSMSServer() { cout << "Disconnect KTF SMS Server.\n"; } }; void main() { SKPhone skp; skp.SendMessage("0112345678", "Hello!"); KTFPhone ktfp; ktfp.SendMessage("0167890123", "Hi!!"); }
예제 2 ¶
~cpp #include <iostream> #include <string> using namespace std; class Phone { protected: virtual void ConnectSMSServer() { cout << "Not implemented!\n"; } virtual void SendToSMSServer(string number, string message) { cout << "Not implemented!\n"; } virtual void DisconnectSMSServer() { cout << "Not implemented!\n"; } public: void SendMessage(string number, string message) { ConnectSMSServer(); SendToSMSServer(number, message); DisconnectSMSServer(); } }; class SKPhone : public Phone { protected: void ConnectSMSServer() { cout << "Connect SK SMS Server.\n"; } void SendToSMSServer(string number, string message) { cout << "Send to SK SMS Server... " << number << " " << message << endl; } void DisconnectSMSServer() { cout << "Disconnect SK SMS Server.\n"; } }; class KTFPhone : public Phone { protected: void ConnectSMSServer() { cout << "Connect KTF SMS Server.\n"; } void SendToSMSServer(string number, string message) { cout << "Send to KTF SMS Server... " << number << " " << message << endl; } void DisconnectSMSServer() { cout << "Disconnect KTF SMS Server.\n"; } }; void main() { SKPhone skp; skp.SendMessage("0112345678", "Hello!"); KTFPhone ktfp; ktfp.SendMessage("0167890123", "Hi!!"); }