์ด ์ฅ์ ๋ ๊น์ด ์ดํดํ๊ธฐ ์ ์ ํฌ์ธํฐ์ ํด๋์ค ์์์ ๋ํด ์ ๋๋ก ์ดํดํ๊ณ ์์ด์ผ ํฉ๋๋ค. ๋ค์ ํํ์ ์๋ฏธ๊ฐ ํ์คํ์ง ์์ ๊ฒฝ์ฐ ํ์๋ ์น์ ์ ๊ฒํ ํด์ผ ํฉ๋๋ค.
Statement | :Explained in: |
int A::b(int c) { } | Classes |
a->b | Data structures |
class A: public B {}; | Friendship and inheritance |
๋ถ๋ชจ(base) ํด๋์ค์ ๋ํ ํฌ์ธํฐ
ํด๋์ค ์์์ ์ฃผ์ ๊ธฐ๋ฅ ์ค ํ๋๋ ํ์ ํด๋์ค์ ๋ํ ํฌ์ธํฐ๊ฐ ๊ธฐ๋ณธ ํด๋์ค์ ๋ํ ํฌ์ธํฐ์ ํ์ ํธํ๋๋ค๋ ๊ฒ์
๋๋ค. ๋คํ์ฑ ์ ์ด ๋จ์ํ์ง๋ง ๊ฐ๋ ฅํ๊ณ ๋ค์ํ ๊ธฐ๋ฅ์ ํ์ฉํ๋ ๊ธฐ์ ์
๋๋ค.
์ฌ๊ฐํ ๋ฐ ์ผ๊ฐํ ํด๋์ค์ ๋ํ ์์ ๋ ์ด ๊ธฐ๋ฅ์ ๊ณ ๋ คํ์ฌ ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์ ์์ฑํ ์ ์์ต๋๋ค.
// pointers to base class
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
};
class Rectangle: public Polygon {
public:
int area()
{ return width*height; }
};
class Triangle: public Polygon {
public:
int area()
{ return width*height/2; }
};
int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = ▭
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << rect.area() << '\n';
cout << trgl.area() << '\n';
return 0;
}
ํจ์ ๋ (์ด๋ฆ ๋ฐ ) main์ ๋ํ ๋ ๊ฐ์ ํฌ์ธํฐ๋ฅผ ์ ์ธํฉ๋๋ค . ์ด๋ค์๋ ๊ฐ๊ฐ ๋ฐ ์ ํ์ ๊ฐ์ฒด์ธ ๋ฐ ์ ์ฃผ์๊ฐ ํ ๋น๋ฉ๋๋ค . ์ ๋ ๋ชจ๋ ์์ ํ์๋ ํด๋์ค ์ด๋ฏ๋ก ์ด๋ฌํ ํ ๋น์ ์ ํจํฉ๋๋ค . ์ญ์ฐธ์กฐ ๋ฐ ( ์ ํจ๊ป ๋ฐ )๋ ์ ํจํ๋ฉฐ ์ฐ๋ฆฌ๊ฐ ์ง์ ๊ฐ์ฒด์ ๋ฉค๋ฒ์ ์ก์ธ์คํ ์ ์๋๋ก ํฉ๋๋ค. ์๋ฅผ ๋ค์ด ๋ค์ ๋ ๋ช
๋ น๋ฌธ์ ์ด์ ์์์ ๋์ผํฉ๋๋ค.Polygonppoly1ppoly2recttrglRectangleTriangleRectangleTrianglePolygon
ppoly1ppoly2ppoly1->ppoly2->
ppoly1->set_values (4,5);
rect.set_values (4,5);
๊ทธ๋ฌ๋ ๋ ๋ค ppoly1๋ฐ ์ ํ์์ด ์ ๋ํ ppoly2ํฌ์ธํฐ ์ด๊ธฐ ๋๋ฌธ์ Polygon(์ ๋ํ ํฌ์ธํฐ Rectangle๋ ํฌ์ธํฐ๊ฐ Triangle์๋) ์์ ์์๋ ๋ฉค๋ฒ๋ง Polygon์ก์ธ์คํ ์ ์๊ณ ํ์ ํด๋์ค Rectangle๋ฐ ์ ๋ฉค๋ฒ๋ ์ก์ธ์คํ ์ ์์ต๋๋ค Triangle. ์ด๊ฒ์ด ์์ ํ๋ก๊ทธ๋จ ์ด ํฌ์ธํฐ ๋์ ๋ฐ ์ง์ area๋ ๊ฐ์ฒด์ ๋ฉค๋ฒ์ ์ก์ธ์คํ๋ ์ด์ ์
๋๋ค. ๊ธฐ๋ณธ ํด๋์ค์ ๋ํ ํฌ์ธํฐ๋ ๋ฉค๋ฒ์ ์ก์ธ์คํ ์ ์์ต๋๋ค. ๋ฉค๋ฒ ๋ ํ์ ํด๋์ค์ ๋ฉค๋ฒ ๋์ ์ ๋ฉค๋ฒ์ธ ๊ฒฝ์ฐ ์ ๋ํ ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ์ฌ ์ก์ธ์คํ ์ ์์์ง๋ง ๋ฌธ์ ๋ ๋ค๋ฅธ ๋ฒ์ ์recttrglarea
areaPolygonareaPolygonRectangleTrianglearea, ๋ฐ๋ผ์ ๊ธฐ๋ณธ ํด๋์ค์์ ๊ตฌํํ ์ ์๋ ๋จ์ผ ๊ณตํต ๋ฒ์ ์ด ์์ต๋๋ค.
๊ฐ์ ํจ์
๊ฐ์ ๋ฉค๋ฒ๋ ์ฐธ์กฐ๋ฅผ ํตํด ํธ์ถ ์์ฑ์ ์ ์งํ๋ฉด์ ํ์ ํด๋์ค์์ ์ฌ์ ์ํ ์ ์๋ ๋ฉค๋ฒ ํจ์์ ๋๋ค. ํจ์๊ฐ ๊ฐ์์ด ๋๋ ๊ตฌ๋ฌธ์ ์ ์ธ ์์ virtualํค์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค.
// virtual members
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area ()
{ return 0; }
};
class Rectangle: public Polygon {
public:
int area ()
{ return width * height; }
};
class Triangle: public Polygon {
public:
int area ()
{ return (width * height / 2); }
};
int main () {
Rectangle rect;
Triangle trgl;
Polygon poly;
Polygon * ppoly1 = ▭
Polygon * ppoly2 = &trgl;
Polygon * ppoly3 = &poly;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly3->set_values (4,5);
cout << ppoly1->area() << '\n';
cout << ppoly2->area() << '\n';
cout << ppoly3->area() << '\n';
return 0;
}
์ด ์์์ ์ธ ๊ฐ์ง ํด๋์ค( Polygon, Rectangle๋ฐ Triangle)๋ ๋ชจ๋ ๋์ผํ ๋ฉค๋ฒ ( width, height, ๋ฐ functions set_values๋ฐ area.
๋ฉค๋ฒ ํจ์ ๋ ๋์ค์ ๊ฐ ํ์ ํด๋์ค์์ ์ฌ์ ์๋๊ธฐ ๋๋ฌธ์ ๊ธฐ๋ณธ ํด๋์ค์์์ area๊ฐ์ด ์ ์ธ๋์์ต๋๋ค . virtual๋น๊ฐ์ ๋ฉค๋ฒ๋ ํ์ ํด๋์ค์์๋ ์ฌ์ ์ํ ์ ์์ง๋ง ํ์ ํด๋์ค์ ๋น๊ฐ์ ๋ฉค๋ฒ๋ ๊ธฐ๋ณธ ํด๋์ค์ ์ฐธ์กฐ๋ฅผ ํตํด ์ก์ธ์คํ virtual์ area์์ต๋๋ค area. ๋ชจ๋ ๊ฒฝ์ฐ์ ๊ธฐ๋ณธ ํด๋์ค์ ๋ฒ์ ์ด ๋์ ํธ์ถ๋์์ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ 0์ ๋ฐํํฉ๋๋ค.
๋ฐ๋ผ์ ๋ณธ์ง์ ์ผ๋ก ๋ฌด์์virtualํค์๋๋ ๊ธฐ๋ณธ ํด๋์ค์ ์๋ ๊ฒ๊ณผ ๊ฐ์ ์ด๋ฆ์ ๊ฐ์ง ํ์ ํด๋์ค์ ๋ฉค๋ฒ๊ฐ ํฌ์ธํฐ์์ ์ ์ ํ๊ฒ ํธ์ถ๋๋๋ก ํ๋ ๊ฒ์ด๋ฉฐ, ๋ ์ ํํ๊ฒ๋ ํฌ์ธํฐ์ ์ ํ์ด ํด๋น ํฌ์ธํฐ๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ธฐ๋ณธ ํด๋์ค์ ๋ํ ํฌ์ธํฐ์ผ ๋ ์์ ์์์์ ๊ฐ์ด ํ์ ํด๋์ค์ ๊ฐ์ฒด์
๋๋ค.
๊ฐ์ ํจ์๋ฅผ ์ ์ธํ๊ฑฐ๋ ์์ํ๋ ํด๋์ค๋ฅผ ๋คํ์ฑ ํด๋์ค ๋ผ๊ณ ํฉ๋๋ค .
๋ฉค๋ฒ ์ค ํ๋์ ๊ฐ์์ฑ์๋ ๋ถ๊ตฌํ๊ณ ์ (๋) ํญ์ 0์ ๋ฐํ ํ๋ ๋ฉค๋ฒ์ ๋ํ ์์ฒด ์ ์์ ํจ๊ป Polygon๊ฐ์ฒด์กฐ์ฐจ ์ธ์คํด์คํ( )๋ ์ผ๋ฐ ํด๋์ค์์ต๋๋ค .polyarea
์ถ์ ํด๋์ค
Polygon์ถ์ ๊ธฐ๋ณธ ํด๋์ค๋ ์ด์ ์์ ์ ํด๋์ค์ ๋งค์ฐ ์ ์ฌํฉ๋๋ค . ๊ธฐ๋ณธ ํด๋์ค๋ก๋ง ์ฌ์ฉํ ์ ์๋ ํด๋์ค์ด๋ฏ๋ก ์ ์ ์์ด ๊ฐ์ ๋ฉค๋ฒ ํจ์๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค(์์ ๊ฐ์ ํจ์๋ผ๊ณ ํจ). ๊ตฌ๋ฌธ์ ์ ์๋ฅผ =0(๋ฑํธ ๋ฐ 0)๋ก ๋ฐ๊พธ๋ ๊ฒ์
๋๋ค.
์ถ์ ๊ธฐ๋ณธ Polygonํด๋์ค๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
// abstract class CPolygon
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area () =0;
};
์๋ ์ ์ area๊ฐ ์์ต๋๋ค. ์ด๊ฒ์ ์์ํ ๊ฐ์ ๊ธฐ๋ฅ=0 ์ด ๋๋ ์ผ๋ก ๋์ฒด๋์์ต๋๋ค . ์ต์ํ ํ๋์ ์์ ๊ฐ์ ํจ์ ๋ฅผ ํฌํจํ๋ ํด๋์ค ๋ฅผ ์ถ์ ๊ธฐ๋ณธ ํด๋์ค ๋ผ๊ณ ํฉ๋๋ค. ์ถ์ ๊ธฐ๋ณธ ํด๋์ค๋ ๊ฐ์ฒด๋ฅผ ์ธ์คํด์คํํ๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ฐ๋ผ์ ์ด ๋ง์ง๋ง ์ถ์ ๊ธฐ๋ณธ ํด๋์ค ๋ฒ์ ์ ๋ค์๊ณผ ๊ฐ์ ๊ฐ์ฒด๋ฅผ ์ ์ธํ๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค.
Polygon mypolygon; // not working if Polygon is abstract base class
๊ทธ๋ฌ๋ ์ถ์ ๊ธฐ๋ณธ ํด๋์ค ๊ฐ ์์ ํ ์ธ๋ชจ์๋ ๊ฒ์ ์๋๋๋ค. ํฌ์ธํฐ๋ฅผ ์์ฑํ๊ณ ๋ชจ๋ ๋คํ์ฑ ๊ธฐ๋ฅ์ ํ์ฉํ๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ๋ค์ ํฌ์ธํฐ ์ ์ธ์ด ์ ํจํฉ๋๋ค.
Polygon * ppoly1;
Polygon * ppoly2;
๊ทธ๋ฆฌ๊ณ ํ์(๋น์ถ์) ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํฌ ๋ ์ค์ ๋ก ์ญ์ฐธ์กฐ๋ ์ ์์ต๋๋ค. ์ ์ฒด ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
// abstract base class
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area (void) =0;
};
class Rectangle: public Polygon {
public:
int area (void)
{ return (width * height); }
};
class Triangle: public Polygon {
public:
int area (void)
{ return (width * height / 2); }
};
int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = ▭
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << ppoly1->area() << '\n';
cout << ppoly2->area() << '\n';
return 0;
}
์ด ์์์ ์๋ก ๋ค๋ฅด์ง๋ง ๊ด๋ จ ์ ํ์ ๊ฐ์ฒด๋ ๊ณ ์ ํ ์ ํ์ ํฌ์ธํฐ( Polygon*)๋ฅผ ์ฌ์ฉํ์ฌ ์ฐธ์กฐ๋๋ฉฐ ๊ฐ์์ด๋ผ๋ ์ด์ ๋ง์ผ๋ก ์ ์ ํ ๋ฉค๋ฒ ํจ์๊ฐ ๋งค๋ฒ ํธ์ถ๋ฉ๋๋ค. ์ด๊ฒ์ ์ด๋ค ์ํฉ์์๋ ์ ๋ง ์ ์ฉํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ์ถ์ ๊ธฐ๋ณธ ํด๋์ค์ ๋ฉค๋ฒ ๊ฐ ์ด ํจ์์ ๋ํ ๊ตฌํ์ด ์๋ ๊ฒฝ์ฐ์๋ ์ ์ ํ ๊ฐ์ ๋ฉค๋ฒ์ ์ก์ธ์คํ๊ธฐ Polygon์ํด ํน์ ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๋ ๊ฐ๋ฅํฉ๋๋ค.thisPolygon
// pure virtual members can be called
// from the abstract base class
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area() =0;
void printarea()
{ cout << this->area() << '\n'; }
};
class Rectangle: public Polygon {
public:
int area (void)
{ return (width * height); }
};
class Triangle: public Polygon {
public:
int area (void)
{ return (width * height / 2); }
};
int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = ▭
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly1->printarea();
ppoly2->printarea();
return 0;
}
๊ฐ์ ๋ฉค๋ฒ์ ์ถ์ ํด๋์ค๋ ๊ฐ์ฒด ์งํฅ ํ๋ก์ ํธ์ ๊ฐ์ฅ ์ ์ฉํ C++ ๋คํ์ฑ ํน์ฑ์ ๋ถ์ฌํฉ๋๋ค. ๋ฌผ๋ก ์์ ์๋ ๋งค์ฐ ๊ฐ๋จํ ์ฌ์ฉ ์ฌ๋ก์ด์ง๋ง ์ด๋ฌํ ๊ธฐ๋ฅ์ ๊ฐ์ฒด ๋ฐฐ์ด ๋๋ ๋์ ์ผ๋ก ํ ๋น๋ ๊ฐ์ฒด์ ์ ์ฉํ ์ ์์ต๋๋ค.
๋ค์์ ๋์ ๋ฉ๋ชจ๋ฆฌ, ์์ฑ์ ์ด๋์
๋ผ์ด์ ๋ฐ ๋คํ์ฑ๊ณผ ๊ฐ์ ์ต์ ์ฅ์ ์ผ๋ถ ๊ธฐ๋ฅ์ ๊ฒฐํฉํ ์์
๋๋ค.
// dynamic allocation and polymorphism
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
Polygon (int a, int b) : width(a), height(b) {}
virtual int area (void) =0;
void printarea()
{ cout << this->area() << '\n'; }
};
class Rectangle: public Polygon {
public:
Rectangle(int a,int b) : Polygon(a,b) {}
int area()
{ return width*height; }
};
class Triangle: public Polygon {
public:
Triangle(int a,int b) : Polygon(a,b) {}
int area()
{ return width*height/2; }
};
int main () {
Polygon * ppoly1 = new Rectangle (4,5);
Polygon * ppoly2 = new Triangle (4,5);
ppoly1->printarea();
ppoly2->printarea();
delete ppoly1;
delete ppoly2;
return 0;
}
ppolyํฌ์ธํฐ ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
Polygon * ppoly1 = new Rectangle (4,5);
Polygon * ppoly2 = new Triangle (4,5);
"์ ๋ํ ํฌ์ธํฐ" ์ ํ ์ผ๋ก ์ ์ธ๋์์ง๋ง ํ ๋น๋ ๊ฐ์ฒด๋ ํ์ ํด๋์ค ์ ํ( ๋ฐ ) Polygon์ ์ง์ ๊ฐ๋ ๊ฒ์ผ๋ก ์ ์ธ๋์์ต๋๋ค .RectangleTriangle
'๐ Development Study > ๐จ๐พโ๐ป C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
exception (0) | 2022.08.22 |
---|---|
ํ๋ณํ (0) | 2022.08.22 |
์์ / ํ๋ ๋ (0) | 2022.08.22 |
์์ฑ์/์๋ฉธ์ (0) | 2022.08.22 |
ํด๋์ค(2) (0) | 2022.08.19 |
๋๊ธ