๊ธ€ ์ž‘์„ฑ์ž: ๋˜ฅํด๋ฒ .
๋ฐ˜์‘ํ˜•

1. ์„ค๋ช…


  • public: ์–ด๋Š๊ณณ์—์„œ๋‚˜ ์ ‘๊ทผ ๊ฐ€๋Šฅ

  • protected: ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค์— ํ•œํ•ด์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ

  • private: ์ž๊ธฐ ์ž์‹ ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ

 ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋‚˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ๊ฐ™์€ ๊ฒฝ์šฐ ์ ‘๊ทผ ์ œ์–ด์ž๋Š” ์œ„์™€ ๊ฐ™์€ ๊ทœ์น™์„ ๋”ฐ๋ฅธ๋‹ค.

ํด๋ž˜์Šค์ธ ๊ฒฝ์šฐ default๋กœ private, ๊ตฌ์กฐ์ฒด์ธ ๊ฒฝ์šฐ default๋กœ public์ด๋‹ค.

 

 

class Parent : (์ ‘๊ทผ์ œ์–ด์ž) Child

 ์œ„์™€ ๊ฐ™์ด ์ƒ์†์— ์ ‘๊ทผ ์ œ์–ด์ž๊ฐ€ ์‚ฌ์šฉ๋œ ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ทœ์น™์ด ์ ์šฉ๋œ๋‹ค.

 

  • public: ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์˜ ์ ‘๊ทผ ์ œ์–ด์ž์— ์˜ํ–ฅ ์—†์ด ๊ทธ๋Œ€๋กœ ์ž‘๋™ํ•œ๋‹ค.

               ์ฆ‰ public์€ ๊ทธ๋Œ€๋กœ public, protected๋Š” ๊ทธ๋Œ€๋กœ protected, private๋Š” ๊ทธ๋Œ€๋กœ private

 

  • protected: ํŒŒ์ƒ ํด๋ž˜์Šค ์ž…์žฅ์—์„œ public์€ protected๋กœ ๋ฐ”๋€Œ๊ณ  ๋‚˜๋จธ์ง€๋Š” ๊ทธ๋Œ€๋กœ ์œ ์ง€

  • private: ๋ชจ๋“  ์ ‘๊ทผ ์ œ์–ด์ž๋“ค์ด private

 

2. ์˜ˆ์‹œ


2.1) public ์ƒ์†

#include <iostream>

class Parent
{
public:
	int member_public;
	void public_func() {};
protected:
	int member_protected;
	void protected_func() {};
private:
	int member_private;
	void private_func() {};
};

class Child : public Parent
{
public:
	Child()
	{
		this->member_public;
		this->member_protected;
		//this->member_private; ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ
	}
};

int main() {
	Child c;
	c.public_func();
	//c.protected_func(); ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ
	//c.private_func(); ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ

	return 0;
}

 

2.2) protected ์ƒ์†

#include <iostream>

class Parent
{
public:
	int member_public;
	void public_func() {};
protected:
	int member_protected;
	void protected_func() {};
private:
	int member_private;
	void private_func() {};
};

class Child : protected Parent
{
public:
	Child()
	{
		this->member_public;
		this->member_protected;
		//this->member_private; ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ
	}
};

int main() {
	Child c;
	//c.public_func(); ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ
	//c.protected_func(); ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ
	//c.private_func(); ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ

	return 0;
}

 

2.3) private ์ƒ์†

#include <iostream>

class Parent
{
public:
	int member_public;
	void public_func() {};
protected:
	int member_protected;
	void protected_func() {};
private:
	int member_private;
	void private_func() {};
};

class Child : private Parent
{
public:
	Child()
	{
		this->member_public;
		this->member_protected;
		//this->member_private; ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ
	}
};

class ChildChild : public Child
{
public:
	ChildChild()
	{
		//this->member_public; ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ
		//this->member_protected; ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ
		//this->member_private; ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ
	}
};

int main() {
	Child c;
	//c.public_func(); ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ
	//c.protected_func(); ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ
	//c.private_func(); ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ

	return 0;
}
๋ฐ˜์‘ํ˜•

'Language > C++' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[C++] keyword: const  (0) 2020.07.22
[C++11] keyword: final  (0) 2020.07.22
[C++11] keyword: override  (0) 2020.07.22
[C++] keyword: virtual  (0) 2020.07.22
[C/C++] Socket Send/Receive Buffer์— ๋Œ€ํ•œ ๊ณ ์ฐฐ  (1) 2019.07.08