ํด๋์ค ๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ํ์ฅ๋ ๊ฐ๋
์
๋๋ค . ๋ฐ์ดํฐ ๊ตฌ์กฐ ์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฐ์ดํฐ ๋ฉค๋ฒ๋ฅผ ํฌํจํ ์ ์์ง๋ง ํจ์๋ฅผ ๋ฉค๋ฒ๋ก ํฌํจํ ์๋ ์์ต๋๋ค.
๊ฐ์ฒด ๋ ํด๋์ค์ ์ธ์คํด์คํ์
๋๋ค . ๋ณ์์ ๊ด์ ์์ ๋ณด๋ฉด ํด๋์ค๋ ์ ํ์ด๊ณ ๊ฐ์ฒด๋ ๋ณ์์
๋๋ค.
ํด๋์ค๋ ๋ค์ ๊ตฌ๋ฌธ๊ณผ ํจ๊ป ํค์๋ class๋๋ ํค์๋ ๋ฅผ ์ฌ์ฉํ์ฌ ์ ์๋ฉ๋๋ค.struct
class class_name {
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
์ฌ๊ธฐ์ class_name๋ ํด๋์ค์ ์ ํจํ ์๋ณ์์ด๊ณ object_names๋ ์ด โโํด๋์ค์ ๊ฐ์ฒด์ ๋ํ ์ ํ์ ์ด๋ฆ ๋ชฉ๋ก์
๋๋ค. ์ ์ธ์ ๋ณธ๋ฌธ์๋ ๋ฐ์ดํฐ ๋๋ ํจ์ ์ ์ธ์ด ๋ ์ ์๋ ๋ฉค๋ฒ ๊ฐ ํฌํจ๋ ์ ์์ผ๋ฉฐ ์ ํ์ ์ผ๋ก ์ง์ ์์ ์ก์ธ์คํ ์ ์์ต๋๋ค . ํด๋์ค๋ ํจ์๋ฅผ ํฌํจํ ์ ์๊ณ ์ก์ธ์ค ์ง์ ์๋ผ๋ ์๋ก์ด ํญ๋ชฉ์ ๊ฐ์ง ์ ์๋ค๋ ์ ์ ์ ์ธํ๋ฉด ์ผ๋ฐ ๋ฐ์ดํฐ ๊ตฌ์กฐ
์ ๋์ผํ ํ์์ ๊ฐ ์ต๋๋ค. ์ก์ธ์ค ์ง์ ์๋ ๋ค์ ์ธ ๊ฐ์ง ํค์๋ ์ค ํ๋์
๋๋ค. , ๋๋ . ์ด๋ฌํ ์ง์ ์๋ ๋ค์ ์ค๋ ๊ตฌ์ฑ์์ ๋ํ ์ก์ธ์ค ๊ถํ์ ์์ ํฉ๋๋ค.privatepublicprotected
- privateํด๋์ค์ ๊ตฌ์ฑ์์ ๊ฐ์ ํด๋์ค์ ๋ค๋ฅธ ๊ตฌ์ฑ์(๋๋ "์น๊ตฌ" )์์๋ง ์ก์ธ์คํ ์ ์์ต๋๋ค.
- protected๋ฉค๋ฒ๋ ๊ฐ์ ํด๋์ค์ ๋ค๋ฅธ ๋ฉค๋ฒ(๋๋ "์น๊ตฌ" )์์ ์ก์ธ์คํ ์ ์์ง๋ง ํ์ ํด๋์ค์ ๋ฉค๋ฒ์์๋ ์ก์ธ์คํ ์ ์์ต๋๋ค.
- ๋ง์ง๋ง์ผ๋ก public๊ฐ์ฒด๊ฐ ํ์๋๋ ๋ชจ๋ ์์น์์ ๋ฉค๋ฒ์ ์ก์ธ์คํ ์ ์์ต๋๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก classํค์๋๋ก ์ ์ธ๋ ํด๋์ค์ ๋ชจ๋ ๋ฉค๋ฒ๋ ๋ชจ๋ ๋ฉค๋ฒ์ ๋ํด ๋น๊ณต๊ฐ ์ก์ธ์ค ๊ถํ์ ๊ฐ์ต๋๋ค. ๋ฐ๋ผ์ ๋ค๋ฅธ ์ก์ธ์ค ์ง์ ์ ๋ณด๋ค ๋จผ์ ์ ์ธ๋ ๋ชจ๋ ๋ฉค๋ฒ๋ ์๋์ผ๋ก ๊ฐ์ธ ์ก์ธ์ค ๊ถํ์ ๊ฐ์ต๋๋ค. ์๋ฅผ ๋ค์ด:
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area (void);
} rect;
Rectangleํธ์ถ๋ ํด๋์ค(์ฆ, ์ ํ) ์ ์ด ํด๋์ค์ ๊ฐ์ฒด(์ฆ, ๋ณ์)๋ฅผ ๋ผ๊ณ ์ ์ธํฉ๋๋ค rect. ์ด ํด๋์ค์๋ 4๊ฐ์ ๋ฉค๋ฒ๊ฐ ํฌํจ๋์ด ์์ต๋๋ค. ๋น๊ณต๊ฐ ์ก์ธ์ค (private๊ฐ ๊ธฐ๋ณธ ์ก์ธ์ค ์์ค์ด๊ธฐ ๋๋ฌธ์ ) ์ ํ์ ๋ฐ์ดํฐ int๋ฉค๋ฒ 2 width๊ฐ์ ๊ณต๊ฐ ์ก์ธ์ค ๊ฐ ์๋ ๋ฉค๋ฒ ํจ์ 2๊ฐ : ํจ์ ๋ฐ , ์ง๊ธ์ ๊ทธ ์ ์ธ๋ง ํฌํจํ์ต๋๋ค. , ๊ทธ๋ฌ๋ ๊ทธ๋ค์ ์ ์๋ ์๋๋๋ค. ํด๋์ค ์ด๋ฆ ๊ณผ ๊ฐ์ฒด ์ด๋ฆ ์ ์ฐจ์ด์ ์ ์ฃผ๋ชฉํ์ญ์์ค . ์ด์ ์์์ ๋ ํด๋์ค ์ด๋ฆ (์ฆ, ์ ํ)์ธ ๋ฐ๋ฉด ๋ ์ ํ์ ๊ฐ์ฒด์
๋๋ค.heightset_valuesarea
RectanglerectRectangle. ๋์ผํ ๊ด๊ณ int์ด๋ฉฐ a๋ค์ ์ ์ธ์ ์์ต๋๋ค.
int a;
์ฌ๊ธฐ์ int๋ ์ ํ ์ด๋ฆ(ํด๋์ค)์ด๊ณ a๋ ๋ณ์ ์ด๋ฆ(๊ฐ์ฒด)์
๋๋ค. ๋ฐ
์ ์ ์ธ ํ, object์ ๋ชจ๋ public ๋ฉค๋ฒ๋ object name ๊ณผ member name ์ฌ์ด ์ ์ ()์ ์ฝ์
ํ๊ธฐ๋ง ํ๋ฉด ์ผ๋ฐ ํจ์๋ ์ผ๋ฐ ๋ณ์์ฒ๋ผ ์ก์ธ์คํ ์ ์์ต๋๋ค . ์ด๊ฒ์ ์ผ๋ฐ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ๋ฉค๋ฒ์ ์ก์ธ์คํ๋ ๊ฒ๊ณผ ๋์ผํ ๊ตฌ๋ฌธ์ ๋ฐ๋ฆ
๋๋ค. ์๋ฅผ ๋ค์ด:Rectanglerectrect.
rect.set_values (3,4);
myarea = rect.area();
rectํด๋์ค ์ธ๋ถ์์ ์ก์ธ์คํ ์ ์๋ ์ ์ผํ ๋ฉค๋ฒ ๋ width๋ฐ height, ๋น๊ณต๊ฐ ์ก์ธ์ค ๊ถํ์ด ์๊ณ ๋์ผํ ํด๋์ค์ ๋ค๋ฅธ ๋ฉค๋ฒ ๋ด์์๋ง ์ฐธ์กฐํ ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค.
๋ค์์ Rectangle ํด๋์ค์ ์ ์ฒด ์์
๋๋ค.
// classes example
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area() {return width*height;}
};
void Rectangle::set_values (int x, int y) {
width = x;
height = y;
}
int main () {
Rectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}
์ด ์ ์์๋ ๋ค์์คํ์ด์ค์ ๊ด๋ จํ์ฌ ์ด์ ์ฅ์์ ๋ณธ ๋ฒ์ ์ฐ์ฐ์ ( ::, ๋ ๊ฐ์ ์ฝ๋ก )๋ฅผ ๋ค์ ๋์
ํฉ๋๋ค. ์ฌ๊ธฐ set_values์์ ํด๋์ค ์์ฒด ์ธ๋ถ์ ํด๋์ค ๋ฉค๋ฒ๋ฅผ ์ ์ํ๊ธฐ ์ํด ํจ์ ์ ์์ ์ฌ์ฉ๋ฉ๋๋ค.
๋ฉค๋ฒ ํจ์ ์ ์ ์ ๋ ๋งค์ฐ ๋จ์ํ๊ธฐ ๋๋ฌธ์ areaํด๋์ค ์ ์ ๋ด์ ์ง์ ํฌํจ๋์ด ์์ต๋๋ค . Rectangle๋ฐ๋๋ก, set_valuesํด๋์ค ๋ด์์ ํ๋กํ ํ์
์ผ๋ก ์ ์ธ๋ ๋ฟ์ด์ง๋ง ์ ์๋ ํด๋์ค ์ธ๋ถ์ ์์ต๋๋ค. ์ด ์ธ๋ถ ์ ์์์ ๋ฒ์( )์ ์ฐ์ฐ์๋ ์ ์๋๋ ํจ์๊ฐ ์ผ๋ฐ ๋น๋ฉค๋ฒ ํจ์๊ฐ ์๋๋ผ ::ํด๋์ค์ ๋ฉค๋ฒ์์ ์ง์ ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค . ๋ฒ์ ์ฐ์ฐ์(Rectangle
::) ์ ์๋๋ ๋ฉค๋ฒ๊ฐ ์ํ ํด๋์ค๋ฅผ ์ง์ ํ์ฌ ์ด ํจ์ ์ ์๊ฐ ํด๋์ค ์ ์ ๋ด์ ์ง์ ํฌํจ๋ ๊ฒ์ฒ๋ผ ์ ํํ ๋์ผํ ๋ฒ์ ์์ฑ์ ๋ถ์ฌํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ์ด์ ์์ ์ ํจ์ ๋ class ์ private ๋ฉค๋ฒ์ธ set_values๋ณ์ width๋ฐ ์ ์ก์ธ์ค ํ ์ ์์ผ๋ฏ๋ก this์ ๊ฐ์ ํด๋์ค์ ๋ค๋ฅธ ๋ฉค๋ฒ์์๋ง ์ก์ธ์คํ ์ ์์ต๋๋ค. ํด๋์ค ์ ์ ๋ด์์ ๋ฉค๋ฒ ํจ์๋ฅผ ์์ ํ ์ ์ํ๋ ๊ฒ๊ณผ ํจ์์ ์ ์ธ์ ํฌํจํ๊ณ ๋์ค์ ํด๋์ค ์ธ๋ถ์์ ์ ์ํ๋ ๊ฒ์ ์ ์ผํ ์ฐจ์ด์ ์ ์ฒซ ๋ฒ์งธ ๊ฒฝ์ฐ์ ํจ์๊ฐ ์๋์ผ๋ก ์ธ๋ผ์ธ ์ผ๋ก ๊ฐ์ฃผ๋๋ค๋ ๊ฒ์
๋๋ค.heightRectangle
์ปดํ์ผ๋ฌ์ ์ํ ๋ฉค๋ฒ ํจ์์ธ ๋ฐ๋ฉด ๋ ๋ฒ์งธ์์๋ ์ผ๋ฐ(์ธ๋ผ์ธ์ด ์๋) ํด๋์ค ๋ฉค๋ฒ ํจ์์
๋๋ค. ์ด๋ก ์ธํด ๋์์๋ ์ฐจ์ด๊ฐ ์์ง๋ง ๊ฐ๋ฅํ ์ปดํ์ผ๋ฌ ์ต์ ํ์์๋ง ๋ฐ์ํฉ๋๋ค.
๋ฉค๋ฒ width๋ฐ height๊ฐ์ธ ์ก์ธ์ค ๊ถํ์ด ์์ต๋๋ค(๋ค๋ฅธ ํญ๋ชฉ์ด ์ง์ ๋์ง ์์ ๊ฒฝ์ฐ ํค์๋๋ก ์ ์๋ ํด๋์ค์ ๋ชจ๋ ๋ฉค๋ฒ๋ ๊ฐ์ธ ์ก์ธ์ค ๊ถํ์ด ์์์ ๊ธฐ์ตํ์ญ์์ค class). private๋ก ์ ์ธํ๋ฉด ํด๋์ค ์ธ๋ถ์์ ์ ๊ทผํ ์ ์์ต๋๋ค. ๊ฐ์ฒด ๋ด์ ํด๋น ๋ฉค๋ฒ์ ๋ํ ๊ฐ์ ์ค์ ํ๋ ๋ฉค๋ฒ ํจ์๋ฅผ ์ด๋ฏธ ์ ์ํ๊ธฐ ๋๋ฌธ์ ์ด๋ ์๋ฏธ๊ฐ ์์ต๋๋ค.set_values. ๋ฐ๋ผ์ ํ๋ก๊ทธ๋จ์ ๋๋จธ์ง ๋ถ๋ถ์ ์ง์ ์ก์ธ์คํ ํ์๊ฐ ์์ต๋๋ค. ์๋ง๋ ์ด์ ๊ฐ์ ๊ฐ๋จํ ์์์๋ ์ด๋ฌํ ๋ณ์์ ๋ํ ์ก์ธ์ค๋ฅผ ์ ํํ๋ ๊ฒ์ด ์ผ๋ง๋ ์ ์ฉํ์ง ์๊ธฐ ์ด๋ ค์ธ ์ ์์ง๋ง ๋ ํฐ ํ๋ก์ ํธ์์๋ ๊ฐ์ด ์๊ธฐ์น ์์ ๋ฐฉ์์ผ๋ก ์์ ๋ ์ ์๋ค๋ ๊ฒ์ด ๋งค์ฐ ์ค์ํ ์ ์์ต๋๋ค(๊ด์ ์์ ์์์น ๋ชปํ ๊ทธ ๋ฌผ์ฒด).
ํด๋์ค์ ๊ฐ์ฅ ์ค์ํ ์์ฑ์ ์ ํ์ด๋ฉฐ, ๋ฐ๋ผ์ ํด๋์ค์ ์ฌ๋ฌ ๊ฐ์ฒด๋ฅผ ์ ์ธํ ์ ์๋ค๋ ๊ฒ์
๋๋ค. ์๋ฅผ ๋ค์ด, class ์ ์ด์ ์์ ์ ๋ฐ๋ผ ์ฐ๋ฆฌ๋ object ์ธ์ object๋ฅผ Rectangle์ ์ธํ ์ ์์์ต๋๋ค .rectbrect
// example: one class, two objects
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area () {return width*height;}
};
void Rectangle::set_values (int x, int y) {
width = x;
height = y;
}
int main () {
Rectangle rect, rectb;
rect.set_values (3,4);
rectb.set_values (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
์ด ํน๋ณํ ๊ฒฝ์ฐ์ ํด๋์ค(๊ฐ์ฒด์ ์ ํ)๋ ์ด๋ฉฐ Rectangle, ๊ทธ ์ค ๋ ๊ฐ์ ์ธ์คํด์ค(์ฆ, ๊ฐ์ฒด)๊ฐ rect์์ต๋๋ค rectb. ๊ทธ๋ค ๊ฐ๊ฐ์๋ ๊ณ ์ ํ ๋ฉค๋ฒ ๋ณ์์ ๋ฉค๋ฒ ํจ์๊ฐ ์์ต๋๋ค.
์ ๋ํ ํธ์ถ์ ์ ๋ํ ํธ์ถ rect.area()๊ณผ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ ๊ณตํ์ง ์์ต๋๋ค rectb.area(). ์ด๋ ํด๋์ค์ ๊ฐ ๊ฐ์ฒด์ Rectangle๊ณ ์ ํ ๋ณ์๊ฐ width์๊ณ height, ์ด๋ค ๋ฐฉ์์ผ๋ก๋ ๊ณ ์ ํ ํจ์ ๋ฉค๋ฒ ๊ฐ set_value์๊ณ area๊ฐ์ฒด์ ๊ณ ์ ํ ๋ฉค๋ฒ ๋ณ์์์ ์๋ํ๊ธฐ ๋๋ฌธ์
๋๋ค.
ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ์ฒด ์งํฅ ํจ๋ฌ๋ค์์ ์ฌ์ฉํ์ฌ ํ๋ก๊ทธ๋๋ฐํ ์ ์์ต๋๋ค. ๋ฐ์ดํฐ์ ํจ์๋ ๋ชจ๋ ๊ฐ์ฒด์ ๋ฉค๋ฒ์ด๋ฏ๋ก ํธ๋ค๋ฌ ๋๋ ๊ธฐํ ์ํ ๋ณ์๋ฅผ ๋ฉค๋ฒ๊ฐ ํธ์ถ๋๋ ๊ฐ์ฒด์ ์ผ๋ถ์ด๊ธฐ ๋๋ฌธ์ ํจ์์ ๋ํ ์ธ์๋ก ์ ๋ฌํ๊ณ ์ ๋ฌํ ํ์๊ฐ ์ค์ด๋ญ๋๋ค. rect.area๋๋ ์ ๋ํ ํธ์ถ์ ์ธ์๊ฐ ์ ๋ฌ๋์ง ์์์ต๋๋ค rectb.area. ์ด๋ฌํ ๋ฉค๋ฒ ํจ์๋ ํด๋น ๊ฐ์ฒด rect๋ฐ ์ ๋ฐ์ดํฐ ๋ฉค๋ฒ๋ฅผ ์ง์ ์ฌ์ฉํ์ต๋๋ค rectb.
์์ฑ์
area์ด์ ์์ ์์ ๋ฉค๋ฒ ํจ์ ๋ฅผ ํธ์ถํ๊ธฐ ์ ์ ํธ์ถํ๋ค๋ฉด set_values์ด๋ป๊ฒ ๋ ๊น์? ๋ฉค๋ฒ width์ height๊ฐ์ด ํ ๋น๋ ์ ์ด ์๊ธฐ ๋๋ฌธ์ ๊ฒฐ์ ๋์ง ์์ ๊ฒฐ๊ณผ์
๋๋ค. ์ด๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ํด๋์ค์๋ ํด๋น ํด๋์ค ์ ์ ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋๋ง๋ค ์๋์ผ๋ก ํธ์ถ๋๋ ์์ฑ์
๋ผ๋ ํน์ ํจ์๊ฐ ํฌํจ๋ ์ ์์ผ๋ฏ๋ก ํด๋์ค๊ฐ ๋ฉค๋ฒ ๋ณ์๋ฅผ ์ด๊ธฐํํ๊ฑฐ๋ ์คํ ๋ฆฌ์ง๋ฅผ ํ ๋นํ ์ ์์ต๋๋ค. ์ด ์์ฑ์ ํจ์๋ ์ผ๋ฐ ๋ฉค๋ฒ ํจ์์ฒ๋ผ ์ ์ธ๋์ง๋ง ํด๋์ค ์ด๋ฆ๊ณผ ์ผ์นํ๋ ์ด๋ฆ์ผ๋ก ๋ฐํ ์ ํ์ด ์์ต๋๋ค. ์๋ . ์ ์ ํด๋์ค๋ ์์ฑ์๋ฅผ ๊ตฌํํ์ฌ ์ฝ๊ฒ ๊ฐ์ ํ ์ ์์ต๋๋ค.
void
Rectangle
// example: class constructor
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
Rectangle (int,int);
int area () {return (width*height);}
};
Rectangle::Rectangle (int a, int b) {
width = a;
height = b;
}
int main () {
Rectangle rect (3,4);
Rectangle rectb (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
์ด ์์ ์ ๊ฒฐ๊ณผ๋ ์ด์ ์์ ์ ๊ฒฐ๊ณผ์ ๋์ผํฉ๋๋ค. ๊ทธ๋ฌ๋ ์ด์ ํด๋์ค Rectangle์๋ ๋ฉค๋ฒ ํจ์๊ฐ ์๊ณ set_values๋์ ๋น์ทํ ์์ ์ ์ํํ๋ ์์ฑ์๊ฐ ์์ต๋๋ค. ์ฆ , ์ ๋ฌ๋ ์ธ์ ๋ก width๋ฐ ๊ฐ์ ์ด๊ธฐํํฉ๋๋ค. ์ด ํด๋์ค์ ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ ์๊ฐ์ ์ด๋ฌํ ์ธ์๊ฐ ์์ฑ์์ ์ด๋ป๊ฒ ์ ๋ฌ๋๋์ง ํ์ธํ์ญ์์ค.height
Rectangle rect (3,4);
Rectangle rectb (5,6);
์์ฑ์๋ ์ผ๋ฐ ๋ฉค๋ฒ ํจ์์ธ ๊ฒ์ฒ๋ผ ๋ช
์์ ์ผ๋ก ํธ์ถํ ์ ์์ต๋๋ค. ํด๋น ํด๋์ค์ ์ ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ํ ๋ฒ๋ง ์คํ๋ฉ๋๋ค.
์์ฑ์ ํ๋กํ ํ์
์ ์ธ(ํด๋์ค ๋ด)์ด๋ ํ์์ ์์ฑ์ ์ ์ ๋ชจ๋ ๋ฐํ ๊ฐ์ ๊ฐ์ง ์๋ ๋ฐฉ๋ฒ์ ์ฃผ๋ชฉํ์ญ์์ค. not even void: ์์ฑ์๋ ๊ฐ์ ๋ฐํํ์ง ์๊ณ ๋จ์ํ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํฉ๋๋ค.
์์ฑ์ ์ค๋ฒ๋ก๋ฉ
๋ค๋ฅธ ํจ์์ ๋ง์ฐฌ๊ฐ์ง๋ก ์์ฑ์๋ ๋ค๋ฅธ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ๋ค๋ฅธ ๋ฒ์ ์ผ๋ก ์ค๋ฒ๋ก๋๋ ์๋ ์์ต๋๋ค. ์ปดํ์ผ๋ฌ๋ ๋งค๊ฐ๋ณ์๊ฐ ์ธ์์ ์ผ์นํ๋ ๊ฒ์ ์๋์ผ๋ก ํธ์ถํฉ๋๋ค.
// overloading class constructors
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
Rectangle ();
Rectangle (int,int);
int area (void) {return (width*height);}
};
Rectangle::Rectangle () {
width = 5;
height = 5;
}
Rectangle::Rectangle (int a, int b) {
width = a;
height = b;
}
int main () {
Rectangle rect (3,4);
Rectangle rectb;
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
์์ ์์์ ๋ ๊ฐ์ ํด๋์ค ๊ฐ์ฒด Rectangle๊ฐ ์์ฑ๋์์ต๋๋ค: rect๋ฐ rectb. rect์ด์ ์์ ์ ๊ฐ์ด ๋ ๊ฐ์ ์ธ์๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
๊ทธ๋ฌ๋ ์ด ์์ ๋ ๋ํ ํน๋ณํ ์ข
๋ฅ์ ์์ฑ์๋ฅผ ์๊ฐํฉ๋๋ค: ๊ธฐ๋ณธ ์์ฑ์ . ๊ธฐ๋ณธ ์์ฑ์ ๋ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ์ง ์๋ ์์ฑ์์ด๋ฉฐ ๊ฐ์ฒด๊ฐ ์ ์ธ๋์์ง๋ง ์ธ์๋ก ์ด๊ธฐํ๋์ง ์์ ๋ ํธ์ถ๋๊ธฐ ๋๋ฌธ์ ํน๋ณํฉ๋๋ค. ์์ ์์์ ๊ธฐ๋ณธ ์์ฑ์ ๋ ์ ๋ํด ํธ์ถ๋ฉ๋๋ค rectb. ๊ฐ ๋น ๊ดํธ ์ธํธ๋ก ๊ตฌ์ฑ๋์ง ์๋ ๋ฐฉ๋ฒ์ ์ ์ํ์ญ์์ค rectb. ์ฌ์ค, ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ํธ์ถํ๋ ๋ฐ ๋น ๊ดํธ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
Rectangle rectb; // ok, default constructor called
Rectangle rectc(); // oops, default constructor NOT called
์ด๋ ๋น ๊ดํธ ์ธํธ๊ฐ rectc๊ฐ์ฒด ์ ์ธ ๋์ ํจ์ ์ ์ธ์ ๋ง๋ค๊ธฐ ๋๋ฌธ์ ๋๋ค. ์ธ์๋ฅผ ์ฌ์ฉํ์ง ์๊ณ type ๊ฐ์ ๋ฐํํ๋ ํจ์๊ฐ ๋ ๊ฒ์ ๋๋ค Rectangle.
Uniform initialization
์์ ๊ฐ์ด ์ธ์๋ฅผ ๊ดํธ๋ก ๋ฌถ์ด ์์ฑ์๋ฅผ ํธ์ถํ๋ ๋ฐฉ์์ ํจ์ํ ์ด๋ผ๊ณ ํฉ๋๋ค . ๊ทธ๋ฌ๋ ์์ฑ์๋ ๋ค๋ฅธ ๊ตฌ๋ฌธ์ผ๋ก๋ ํธ์ถํ ์ ์์ต๋๋ค.
์ฒซ์งธ, ๋จ์ผ ๋งค๊ฐ๋ณ์๊ฐ ์๋ ์์ฑ์๋ ๋ณ์ ์ด๊ธฐํ ๊ตฌ๋ฌธ(๋ฑํธ ๋ค์ ์ธ์๊ฐ ์ด)์ ์ฌ์ฉํ์ฌ ํธ์ถํ ์ ์์ต๋๋ค. ๋ ์ต๊ทผ์๋ C++์์ ๊ท ์ผ ์ด๊ธฐํ
class_name object_name = initialization_value;
๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ์๋ฅผ ํธ์ถํ ์ ์๋ ๊ฐ๋ฅ์ฑ์ ๋์
ํ์ต๋๋ค. , ๋ณธ์ง์ ์ผ๋ก ๊ธฐ๋ฅ ํ์๊ณผ ๋์ผํ์ง๋ง ๊ดํธ( ) ๋์ ์ค๊ดํธ( ) ์ฌ์ฉ : ์ ํ์ ์ผ๋ก ์ด ๋ง์ง๋ง ๊ตฌ๋ฌธ์ ์ค๊ดํธ ์์ ๋ฑํธ๋ฅผ ํฌํจํ ์ ์์ต๋๋ค. ๋ค์์ ์์ฑ์๊ฐ ๋จ์ผ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ค ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ ์์
๋๋ค.{}()
class_name object_name { value, value, value, ... }
// classes and uniform initialization
#include <iostream>
using namespace std;
class Circle {
double radius;
public:
Circle(double r) { radius = r; }
double circum() {return 2*radius*3.14159265;}
};
int main () {
Circle foo (10.0); // functional form
Circle bar = 20.0; // assignment init.
Circle baz {30.0}; // uniform init.
Circle qux = {40.0}; // POD-like
cout << "foo's circumference: " << foo.circum() << '\n';
return 0;
}
ํจ์ํ์ ๋ํ ๊ท ์ผ ์ด๊ธฐํ์ ์ฅ์ ์ ๊ดํธ์ ๋ฌ๋ฆฌ ์ค๊ดํธ๊ฐ ํจ์ ์ ์ธ๊ณผ ํผ๋๋ ์ ์์ผ๋ฏ๋ก ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ๋ช ์์ ์ผ๋ก ํธ์ถํ๋ ๋ฐ ์ฌ์ฉํ ์ ์๋ค๋ ๊ฒ์ ๋๋ค.
Rectangle rectb; // default constructor called
Rectangle rectc(); // function declaration (default constructor NOT called)
Rectangle rectd{}; // default constructor called
์์ฑ์๋ฅผ ํธ์ถํ๋ ๊ตฌ๋ฌธ์ ์ ํ์ ์ฃผ๋ก ์คํ์ผ์ ๋ฌธ์ ์ ๋๋ค. ๋๋ถ๋ถ์ ๊ธฐ์กด ์ฝ๋๋ ํ์ฌ ๊ธฐ๋ฅ์ ํ์์ ์ฌ์ฉํ๊ณ ์ผ๋ถ ์๋ก์ด ์คํ์ผ ๊ฐ์ด๋์์๋ ์ ํ์ผ๋ก ์ ํธํ๋ ์ ์ฌ์ ์ธ ํจ์ ์ด ์์์๋ ๋ถ๊ตฌํ๊ณ ๋ค๋ฅธ ๊ฒ๋ณด๋ค ๊ท ์ผํ ์ด๊ธฐํ๋ฅผ ์ ํํ๋๋ก ์ ์ํฉ๋๋ค
์์ฑ์์ ๋ฉค๋ฒ ์ด๊ธฐํ
์์ฑ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ค๋ฅธ ๋ฉค๋ฒ๋ฅผ ์ด๊ธฐํํ๋ ๊ฒฝ์ฐ ๋ณธ๋ฌธ์ ๋ช ๋ น๋ฌธ์ ์์กดํ์ง ์๊ณ ์ด๋ฌํ ๋ค๋ฅธ ๋ฉค๋ฒ๋ฅผ ์ง์ ์ด๊ธฐํํ ์ ์์ต๋๋ค. ์ด๊ฒ์ ์์ฑ์์ ๋ณธ๋ฌธ ์์ ์ฝ๋ก ( :)๊ณผ ํด๋์ค ๋ฉค๋ฒ์ ์ด๊ธฐํ ๋ชฉ๋ก์ ์ฝ์ ํ์ฌ ์ํ๋ฉ๋๋ค. ์๋ฅผ ๋ค์ด ๋ค์ ์ ์ธ์ด ์๋ ํด๋์ค๋ฅผ ๊ณ ๋ คํ์ญ์์ค.
class Rectangle {
int width,height;
public:
Rectangle(int,int);
int area() {return width*height;}
};
์ด ํด๋์ค์ ์์ฑ์๋ ํ์์ ๊ฐ์ด ๋ค์๊ณผ ๊ฐ์ด ์ ์ํ ์ ์์ต๋๋ค.
Rectangle::Rectangle (int x, int y) { width=x; height=y; }
๊ทธ๋ฌ๋ ๋ฉค๋ฒ ์ด๊ธฐํ ๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ์ ์ํ ์๋ ์์ต๋๋ค .
Rectangle::Rectangle (int x, int y) : width(x) { height=y; }
๋๋:
Rectangle::Rectangle (int x, int y) : width(x), height(y) { }
์ด ๋ง์ง๋ง ๊ฒฝ์ฐ์ ์์ฑ์๋ ๋ฉค๋ฒ๋ฅผ ์ด๊ธฐํํ๋ ๊ฒ ์ธ์๋ ์๋ฌด ๊ฒ๋ ํ์ง ์์ผ๋ฏ๋ก ๋น ํจ์ ๋ณธ๋ฌธ์ด ์์ต๋๋ค.
๊ธฐ๋ณธ ์ ํ์ ๋ฉค๋ฒ์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ์ด๊ธฐํ๋์ง ์๊ธฐ ๋๋ฌธ์ ์์ฑ์ ์์ ๋ฐฉ๋ฒ ์ค ์ด๋ค ๊ฒ์ด ์ ์๋๋์ง ์ฐจ์ด๊ฐ โโ์์ง๋ง ๋ฉค๋ฒ ๊ฐ์ฒด(์ ํ์ด ํด๋์ค์ธ ๊ฐ์ฒด)์ ๊ฒฝ์ฐ ์ฝ๋ก ๋ค์ ์ด๊ธฐํ๋์ง ์์ผ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค.
ํด๋์ค์ ๋ชจ๋ ๋ฉค๋ฒ๋ฅผ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ๊ตฌ์ฑํ๋ ๊ฒ์ด ํธ๋ฆฌํ ์๋ ์๊ณ ํญ์ ๊ทธ๋ ์ง ์์ ์๋ ์์ต๋๋ค. ์ด๋ค ๊ฒฝ์ฐ์๋ ๋ญ๋น์ด์ง๋ง(๊ตฌ์ฑ์์์ ๋ฉค๋ฒ๊ฐ ๋ค์ ์ด๊ธฐํ๋๋ ๊ฒฝ์ฐ), ๋ค๋ฅธ ๊ฒฝ์ฐ์๋ ๊ธฐ๋ณธ ๊ตฌ์ฑ์ด ๋ถ๊ฐ๋ฅํฉ๋๋ค. (ํด๋์ค์ ๊ธฐ๋ณธ ์์ฑ์๊ฐ ์์ ๋). ์ด ๊ฒฝ์ฐ ๋ฉค๋ฒ ์ด๊ธฐํ ๋ชฉ๋ก์์ ๋ฉค๋ฒ๋ฅผ ์ด๊ธฐํํฉ๋๋ค. ์๋ฅผ ๋ค์ด:
// member initialization
#include <iostream>
using namespace std;
class Circle {
double radius;
public:
Circle(double r) : radius(r) { }
double area() {return radius*radius*3.14159265;}
};
class Cylinder {
Circle base;
double height;
public:
Cylinder(double r, double h) : base (r), height(h) {}
double volume() {return base.area() * height;}
};
int main () {
Cylinder foo (10,20);
cout << "foo's volume: " << foo.volume() << '\n';
return 0;
}
์ด ์์์ class ์๋ ์ ํ์ด ๋ค๋ฅธ ํด๋์ค( ์ ์ ํ์ ) Cylinder์ธ ๋ฉค๋ฒ ๊ฐ์ฒด๊ฐ ์์ต๋๋ค. ํด๋์ค์ ๊ฐ์ฒด๋ ๋งค๊ฐ๋ณ์๋ก๋ง ์์ฑํ ์ ์๊ธฐ ๋๋ฌธ์ ์ ์์ฑ์๋ ์ ์์ฑ์๋ฅผ ํธ์ถํด์ผ ํ๋ฉฐ ์ด๋ฅผ ์ํํ๋ ์ ์ผํ ๋ฐฉ๋ฒ์ ๋ฉค๋ฒ ์ด๋์ ๋ผ์ด์ ๋ชฉ๋ก ์ ์์ต๋๋ค. ์ด๋ฌํ ์ด๊ธฐํ๋ ๊ดํธ ๋์ ์ค๊ดํธ๋ฅผ ์ฌ์ฉํ์ฌ ๊ท ์ผํ ์ด๊ธฐํ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ ์๋ ์์ต๋๋ค .baseCircleCircleCylinderbase
Cylinder::Cylinder (double r, double h) : base{r}, height{h} { }
ํด๋์ค์ ๋ํ ํฌ์ธํฐ
๊ฐ์ฒด๋ ํฌ์ธํฐ๋ก ๊ฐ๋ฆฌํฌ ์๋ ์์ต๋๋ค. ์ผ๋จ ์ ์ธ๋๋ฉด ํด๋์ค๋ ์ ํจํ ์ ํ์ด ๋๋ฏ๋ก ํฌ์ธํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ์ ํ์ผ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด:
Rectangle * prect;
ํด๋์ค์ ๊ฐ์ฒด์ ๋ํ ํฌ์ธํฐ์
๋๋ค Rectangle.
์ผ๋ฐ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ์ ์ฌํ๊ฒ ํ์ดํ ์ฐ์ฐ์( )๋ฅผ ์ฌ์ฉํ์ฌ ํฌ์ธํฐ์์ ๊ฐ์ฒด์ ๋ฉค๋ฒ์ ์ง์ ์ก์ธ์คํ ์ ์์ต๋๋ค ->. ๋ค์์ ๋ช ๊ฐ์ง ๊ฐ๋ฅํ ์กฐํฉ์ ์์
๋๋ค.
// pointer to classes example
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
Rectangle(int x, int y) : width(x), height(y) {}
int area(void) { return width * height; }
};
int main() {
Rectangle obj (3, 4);
Rectangle * foo, * bar, * baz;
foo = &obj;
bar = new Rectangle (5, 6);
baz = new Rectangle[2] { {2,5}, {3,6} };
cout << "obj's area: " << obj.area() << '\n';
cout << "*foo's area: " << foo->area() << '\n';
cout << "*bar's area: " << bar->area() << '\n';
cout << "baz[0]'s area:" << baz[0].area() << '\n';
cout << "baz[1]'s area:" << baz[1].area() << '\n';
delete bar;
delete[] baz;
return 0;
}
์ด ์์ ์์๋ ์ฌ๋ฌ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด์ ํฌ์ธํฐ(์ฐ์ฐ์ *, &, ., ->, [])๋ฅผ ์ฒ๋ฆฌํฉ๋๋ค. ๋ค์๊ณผ ๊ฐ์ด ํด์ํ ์ ์์ต๋๋ค.
ํํ๋ก ์ฝ์ ์ ์์ต๋๋ค
expression | can be read as |
*x | pointed to by x |
&x | address of x |
x.y | member y of object x |
x->y | member y of object pointed to by x |
(*x).y | member y of object pointed to by x (equivalent to the previous one) |
x[0] | first object pointed to by x |
x[1] | second object pointed to by x |
x[n] | (n+1)th object pointed to by x |
์ด๋ฌํ ํํ์ ๋๋ถ๋ถ์ ์ด์ ์ฅ์์ ์๊ฐ๋์์ต๋๋ค. ํนํ ๋ฐฐ์ด์ ๋ํ ์ฅ์์๋ ์คํ์ ์ฐ์ฐ์( [])๋ฅผ ์๊ฐํ๊ณ ์ผ๋ฐ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ๋ํด์๋ ํ์ดํ ์ฐ์ฐ์( ->)๋ฅผ ์๊ฐํ์ต๋๋ค.
๊ตฌ์กฐ์ฒด ๋ฐ ๊ณต์ฉ์ฒด๋ก ์ ์๋ ํด๋์ค
ํด๋์ค๋ ํค์๋ class๋ฟ๋ง ์๋๋ผ ํค์๋ struct๋ฐ ๋ก๋ ์ ์ํ ์ ์์ต๋๋ค union.
์ผ๋ฐ์ ์ผ๋ก ์ผ๋ฐ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์ ์ธํ๋ ๋ฐ ์ฌ์ฉ๋๋ ํค์๋ struct๋ ํค์๋์ ๋์ผํ ๊ตฌ๋ฌธ์ผ๋ก ๋ฉค๋ฒ ํจ์๊ฐ ์๋ ํด๋์ค๋ฅผ ์ ์ธํ๋ ๋ฐ ์ฌ์ฉํ ์๋ ์์ต๋๋ค class. ๋ ์ฌ์ด์ ์ ์ผํ ์ฐจ์ด์ ์ ํค์๋๋ก ์ ์ธ๋ ํด๋์ค์ ๋ฉค๋ฒ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ก์ธ์ค ๊ถํ struct์ด public์๋ ๋ฐ๋ฉด ํค์๋๋ก ์ ์ธ๋ ํด๋์ค์ ๋ฉค๋ฒ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ก์ธ์ค ๊ถํ์ด class์๋ค๋ ๊ฒ private์
๋๋ค. ๋ค๋ฅธ ๋ชจ๋ ๋ชฉ์ ์ ๊ฒฝ์ฐ ๋ ํค์๋๋ ์ด ์ปจํ
์คํธ์์ ๋์ผํฉ๋๋ค.
๋ฐ๋๋ก ๊ณต์ฉ์ฒดstruct ์ ๊ฐ๋
์ and ๋ก ์ ์ธ๋ ํด๋์ค์ ๊ฐ๋
๊ณผ ๋ค๋ฆ
๋๋ค.class, ๊ณต์ฉ์ฒด๋ ํ ๋ฒ์ ํ๋์ ๋ฐ์ดํฐ ๋ฉค๋ฒ๋ง ์ ์ฅํ์ง๋ง ๊ทธ๋ผ์๋ ๋ถ๊ตฌํ๊ณ ํด๋์ค์ด๊ธฐ๋ ํ๋ฏ๋ก ๋ฉค๋ฒ ํจ์๋ ๋ณด์ ํ ์ ์์ต๋๋ค. ๊ณต์ฉ์ฒด ํด๋์ค์ ๊ธฐ๋ณธ ์ก์ธ์ค๋ ์
๋๋ค public.
'๐ Development Study > ๐จ๐พโ๐ป C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์์ฑ์/์๋ฉธ์ (0) | 2022.08.22 |
---|---|
ํด๋์ค(2) (0) | 2022.08.19 |
๊ธฐํ ๋ฐ์ดํฐ ํ์ (0) | 2022.08.19 |
๊ตฌ์กฐ์ฒด (0) | 2022.08.19 |
๋์ ๋ฉ๋ชจ๋ฆฌ (0) | 2022.08.19 |
๋๊ธ