[C++] keyword: public, protected, private
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 |
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote
๋ค๋ฅธ ๊ธ
-
[C++11] keyword: final
[C++11] keyword: final
2020.07.22 -
[C++11] keyword: override
[C++11] keyword: override
2020.07.22 -
[C++] keyword: virtual
[C++] keyword: virtual
2020.07.22 -
[C/C++] Socket Send/Receive Buffer์ ๋ํ ๊ณ ์ฐฐ
[C/C++] Socket Send/Receive Buffer์ ๋ํ ๊ณ ์ฐฐ
2019.07.08