U E D R , A S I H C RSS

5인용C++스터디/클래스상속보충

예제 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!!");
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:19
Processing time 0.0106 sec