friend function
์์น์ ์ผ๋ก ํด๋์ค์ private ๋ฐ protected ๋ฉค๋ฒ๋ ์ ์ธ๋ ๋์ผํ ํด๋์ค ์ธ๋ถ์์ ์ก์ธ์คํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ด ๊ท์น์ "์น๊ตฌ" ์๊ฒ๋ ์ ์ฉ๋์ง ์์ต๋๋ค .
์น๊ตฌ ๋ friendํค์๋๋ก ์ ์ธ๋ ํจ์ ๋๋ ํด๋์ค์
๋๋ค.
๋น๋ฉค๋ฒ ํจ์๋ ํด๋น ํด๋์ค์ ์น๊ตฌ ๋ก ์ ์ธ๋ ๊ฒฝ์ฐ ํด๋์ค์ private ๋ฐ protected ๋ฉค๋ฒ์ ์ก์ธ์คํ ์ ์์ต๋๋ค . ์ด๋ ํด๋์ค ๋ด์ ์ด ์ธ๋ถ ํจ์์ ์ ์ธ์ ํฌํจํ๊ณ ํค์๋๋ฅผ ์์ ๋ ์ผ๋ก์จ ์ํ๋ฉ๋๋ค friend.
// friend functions
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
Rectangle() {}
Rectangle (int x, int y) : width(x), height(y) {}
int area() {return width * height;}
friend Rectangle duplicate (const Rectangle&);
};
Rectangle duplicate (const Rectangle& param)
{
Rectangle res;
res.width = param.width*2;
res.height = param.height*2;
return res;
}
int main () {
Rectangle foo;
Rectangle bar (2,3);
foo = duplicate (bar);
cout << foo.area() << '\n';
return 0;
}
duplicateํจ์๋ ํด๋์ค ์ ์น๊ตฌRectangle ์ ๋๋ค . ๋ฐ๋ผ์ ํจ์ ๋ ์ ํ์ ์๋ก ๋ค๋ฅธ ๊ฐ์ฒด์ duplicate๋ฉค๋ฒ width๋ฐ (๋น๊ณต๊ฐ)์ ์ก์ธ์คํ ์ ์์ต๋๋ค . ์ ์ ์ธ ์ด๋ ๋์ค์ ์ฌ์ฉํ ๋ function ์ class ์ ๋ฉค๋ฒ๋ก ๊ฐ์ฃผ ๋์ง ์์ต๋๋ค . ๊ทธ๋ ์ง ์๋ค! ํ์์ด ์๋๋๋ผ๋ ๋น๊ณต๊ฐ ๋ฐ ๋ณดํธ๋ ํ์์ ์ก์ธ์คํ ์ ์์ต๋๋ค. friend ํจ์์ ์ผ๋ฐ์ ์ธ ์ฌ์ฉ ์ฌ๋ก๋ ๋ ํด๋์ค์ private ๋๋ protected ๋ฉค๋ฒ์ ์ก์ธ์คํ๋ ๋ ๊ฐ์ ์๋ก ๋ค๋ฅธ ํด๋์ค ๊ฐ์ ์ํ๋๋ ์์ ์ ๋๋ค.heightRectangleduplicatemainduplicateRectangle
friend class
friend ํจ์์ ์ ์ฌํ๊ฒ friend ํด๋์ค๋ ๋ฉค๋ฒ๊ฐ ๋ค๋ฅธ ํด๋์ค์ private ๋๋ protected ๋ฉค๋ฒ์ ์ก์ธ์คํ ์ ์๋ ํด๋์ค์ ๋๋ค.
// friend class
#include <iostream>
using namespace std;
class Square;
class Rectangle {
int width, height;
public:
int area ()
{return (width * height);}
void convert (Square a);
};
class Square {
friend class Rectangle;
private:
int side;
public:
Square (int a) : side(a) {}
};
void Rectangle::convert (Square a) {
width = a.side;
height = a.side;
}
int main () {
Rectangle rect;
Square sqr (4);
rect.convert(sqr);
cout << rect.area();
return 0;
}
์ด ์์ ์์ class ๋ ์ ๋ฉค๋ฒ ํจ์๊ฐ ์ private ๋ฐ protected ๋ฉค๋ฒ์ ์ก์ธ์ค ํ ์ ์๋๋ก Rectangleํ๋ ํด๋์ค์ ์น๊ตฌ ์
๋๋ค . ๋ณด๋ค ๊ตฌ์ฒด์ ์ผ๋ก, ์ ์ฌ๊ฐํ์ ์ธก๋ฉด์ ์ค๋ช
ํ๋ ๋ฉค๋ฒ ๋ณ์์ ์ก์ธ์คํฉ๋๋ค . ์ด ์์ ์๋ ๋ค๋ฅธ ์๋ก์ด ๊ฒ์ด ์์ต๋๋ค. ํ๋ก๊ทธ๋จ ์์ ๋ถ๋ถ์ class ์ ๋น ์ ์ธ์ด ์์ต๋๋ค. ์ด๊ฒ์ ํด๋์ค ๊ฐ (๋ฉค๋ฒ์ ๋งค๊ฐ๋ณ์๋ก ) ๋ฅผ ์ฌ์ฉ ํ๊ณ (์น๊ตฌ๋ก ์ ์ธ) ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ํ์ํฉ๋๋ค. ์ง์ ํ์ง ์๋ ํ ์ฐ์ ์ ์ ๋ ์ผ์นํ์ง ์์ต๋๋ค. ์ด ์์์ ๋ ์ ์ํด ์น๊ตฌ ํด๋์ค๋ก ๊ฐ์ฃผ ๋์ง๋ง Square๋ ์ ์ํด ์น๊ตฌ๋ก ๊ฐ์ฃผ๋์ง ์์ต๋๋ค . ๋ฐ๋ผ์ ์ ๋ฉค๋ฒ ํจ์๋SquareRectangleSquareRectangleSquare::side
SquareRectangleSquareconvertSquareRectangle
RectangleSquareRectangleRectangle์ protected ๋ฐ private ๋ฉค๋ฒ์ ์ก์ธ์คํ ์ Square์์ง๋ง ๊ทธ ๋ฐ๋๋ ๋ถ๊ฐ๋ฅํฉ๋๋ค. ๋ฌผ๋ก ํ์ํ ๊ฒฝ์ฐ Square์ ์น๊ตฌ๋ก ์ ์ธ๋ ์๋ ์์ผ๋ฉฐ Rectangle์ด๋ฌํ ์ก์ธ์ค ๊ถํ์ ๋ถ์ฌํ ์๋ ์์ต๋๋ค.
์ฐ์ ์ ๋ ๋ค๋ฅธ ์์ฑ์ ์ ์ด์ ์ด์ง ์๋ค๋ ๊ฒ์
๋๋ค. ์น๊ตฌ์ ์น๊ตฌ๋ ๋ช
์์ ์ผ๋ก ์ง์ ๋์ง ์๋ ํ ์น๊ตฌ๋ก ๊ฐ์ฃผ๋์ง ์์ต๋๋ค.
ํด๋์ค ๊ฐ์ ์์
C++์ ํด๋์ค๋ฅผ ํ์ฅํ์ฌ ๊ธฐ๋ณธ ํด๋์ค์ ํน์ฑ์ ์ ์งํ๋ ์ ํด๋์ค๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค. ์์์ด๋ผ๊ณ ํ๋ ์ด ํ๋ก์ธ์ค์๋ ๊ธฐ๋ณธ ํด๋์ค ์ ํ์ ํด๋์ค ๊ฐ ํฌํจ๋ฉ๋๋ค . ํ์ ํด๋์ค ๋ ๊ธฐ๋ณธ ํด๋์ค ์ ๋ฉค๋ฒ๋ฅผ ์์ํ๋ฉฐ ๊ทธ ์์ ์์ฒด ๋ฉค๋ฒ๋ฅผ ์ถ๊ฐํ ์ ์์ต๋๋ค.
์๋ฅผ ๋ค์ด, ์ง์ฌ๊ฐํ๊ณผ ์ผ๊ฐํ์ด๋ผ๋ ๋ ๊ฐ์ง ์ข
๋ฅ์ ๋ค๊ฐํ์ ์ค๋ช
ํ๋ ์ผ๋ จ์ ํด๋์ค๋ฅผ ์์ํด ๋ณด๊ฒ ์ต๋๋ค. ์ด ๋ ํด๋ฆฌ๊ณค์ ๋ฉด์ ์ ๊ณ์ฐํ๋ ๋ฐ ํ์ํ ๊ฐ๊ณผ ๊ฐ์ ํน์ ๊ณตํต ์์ฑ์ ๊ฐ์ง๊ณ ์์ต๋๋ค. ๋ ๋ค ๋์ด์ ๋๋น(๋๋ ๋ฐ๋ณ)๋ก ๊ฐ๋จํ ์ค๋ช
ํ ์ ์์ต๋๋ค. ์ด๊ฒ์ ์ฐ๋ฆฌ๊ฐ ๋ ๊ฐ์ ๋ค๋ฅธ ํด๋์ค๋ฅผ ํ์ํ
ํด๋์ค๋ก ํด๋์ค์ ์ธ๊ณ์์ ๋ํ๋ผ ์ ์์ต๋๋ค .PolygonRectangleTriangle: ํด๋์ค์๋
๋ Polygon๊ฐ์ง ์ ํ์ ํด๋ฆฌ๊ณค์ ๊ณตํต์ ์ธ ๋ฉค๋ฒ๊ฐ ํฌํจ๋ฉ๋๋ค. ์ฐ๋ฆฌ์ ๊ฒฝ์ฐ: width๊ทธ๋ฆฌ๊ณ height. ๊ทธ๋ฆฌ๊ณ ํด๋ฆฌ๊ณค์ ํ ์ ํ์์ ๋ค๋ฅธ ์ ํ์ผ๋ก ๋ค๋ฅธ ํน์ ๊ธฐ๋ฅ์ ๊ฐ์ง ํ์ ํด๋์ค๊ฐ ๋ ๊ฒ์
๋๋ค Rectangle. ๋ค๋ฅธ ํด๋์ค์์ ํ์๋ ํด๋์ค๋ ๊ธฐ๋ณธ ํด๋์ค์ ์ก์ธ์ค ๊ฐ๋ฅํ ๋ชจ๋ ๋ฉค๋ฒ๋ฅผ ์์ํฉ๋๋ค. ์ฆ, ๊ธฐ๋ณธ ํด๋์ค์ ๋ฉค๋ฒ๊ฐ ํฌํจ๋์ด ์๊ณ ์ด๋ผ๋ ๋ค๋ฅธ ๋ฉค๋ฒ๊ฐ ์๋ ํด๋์ค๋ฅผ ํ์ํ๋ฉด ํ์ ํด๋์ค์ ๋ฉค๋ฒ ์ ๋ฉค๋ฒ ๊ฐ ๋ชจ๋ ํฌํจ๋ฉ๋๋ค . ๋ ํด๋์ค์ ์์ ๊ด๊ณ๋ ํ์ ํด๋์ค์์ ์ ์ธ๋ฉ๋๋ค. ํ์ ํด๋์ค ์ ์๋ ๋ค์ ๊ตฌ๋ฌธ์ ์ฌ์ฉํฉ๋๋ค. ์ฌ๊ธฐ์ ํ์ ํด๋์ค์ ์ด๋ฆ์Triangle
ABAB
class derived_class_name: public base_class_name
{ /*...*/ };
derived_class_namebase_class_name๊ธฐ๋ฐ์ด ๋๋ ํด๋์ค์ ์ด๋ฆ์
๋๋ค. ์ก์ธ์ค ์ง์ ์๋ ๋ค๋ฅธ ์ก์ธ์ค ์ง์ ์( ๋๋ ) public์ค ํ๋๋ก ๋์ฒด๋ ์ ์์ต๋๋ค . ์ด ์ก์ธ์ค ์ง์ ์๋ ๊ธฐ๋ณธ ํด๋์ค์์ ์์๋ ๋ฉค๋ฒ์ ๋ํด ๊ฐ์ฅ ์ก์ธ์ค ๊ฐ๋ฅํ ์์ค์ ์ ํํฉ๋๋ค. ์ก์ธ์ค ์์ค์ด ๋ ๋์ ๋ฉค๋ฒ๋ ๋์ ์ด ์์ค์ผ๋ก ์์๋๋ ๋ฐ๋ฉด, ๋์ผํ๊ฑฐ๋ ๋ ์ ํ์ ์ธ ์ก์ธ์ค ์์ค์ ๊ฐ์ง ๋ฉค๋ฒ๋ ํ์ ํด๋์ค์์ ์ ํ์ ์ธ ์์ค์ ์ ์งํฉ๋๋ค. .protectedprivate
// derived classes
#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;
rect.set_values (4,5);
trgl.set_values (4,5);
cout << rect.area() << '\n';
cout << trgl.area() << '\n';
return 0;
}
ํด๋์ค Rectangle๋ฐ Triangle๊ฐ๊ฐ์ ๊ฐ์ฒด์๋ ์์ ์์๋ ๋ฉค๋ฒ๊ฐ ํฌํจ๋์ด ์์ต๋๋ค Polygon. width, height๋ฐ set_values. _
ํด๋์ค์์ ์ฌ์ฉ ๋๋ protected์ก์ธ์ค ์ง์ Polygon์๋ ์ ์ ์ฌํฉ๋๋ค private. ์ค์ ๋ก ์์์์ ์ ์ผํ ์ฐจ์ด์ ์ด ๋ฐ์ํฉ๋๋ค. ํด๋์ค๊ฐ ๋ค๋ฅธ ํด๋์ค๋ฅผ ์์ํ ๋ ํ์ ํด๋์ค์ ๋ฉค๋ฒ๋ ๊ธฐ๋ณธ ํด๋์ค์์ ์์๋ ๋ณดํธ๋ ๋ฉค๋ฒ์ ์ก์ธ์คํ ์ ์์ง๋ง ๋น๊ณต๊ฐ ๋ฉค๋ฒ๋ ์ก์ธ์คํ ์ ์์ต๋๋ค. ๋ฐ ๋์ ์
์ ์ธํจ์ผ๋ก์จ ์ด๋ฌํ ๋ฉค๋ฒ๋ ์ ๋ฉค๋ฒ๊ฐ ์๋ ํ์ ํด๋์ค ๋ฐ ์์๋ ์ก์ธ์คํ ์ ์์ต๋๋ค . ๊ณต๊ฐ๋ ๊ฒฝ์ฐ ์ด๋์์๋ ์ก์ธ์คํ ์ ์์ต๋๋ค.widthheightprotectedprivateRectangleTrianglePolygon
๋ค์๊ณผ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ก์ธ์คํ ์ ์๋ ๊ธฐ๋ฅ์ ๋ฐ๋ผ ๋ค์ํ ์ก์ธ์ค ์ ํ์ ์์ฝํ ์ ์์ต๋๋ค.
์ ์ฅpublicprotectedprivate
Access | public | protected | private |
members of the same class | ์ | ์ | ์ |
members of derived class | ์ | ์ | ์๋์ |
not members | ์ | ์๋์ | ์๋์ |
main์ฌ๊ธฐ์ "๋นํ์"์ ๋ค๋ฅธ ํด๋์ค ๋๋ ํจ์์์ ์ ๊ฐ์ด ํด๋์ค ์ธ๋ถ์์ ์ก์ธ์ค๋ฅผ ๋ํ๋
๋๋ค .
์์ ์์์ ๋ฉค๋ฒ๋ ์ ์ํด ์์๋๊ณ ๊ธฐ๋ณธ ํด๋์ค์์ Rectangle์ Triangle๋์ผํ ์ก์ธ์ค ๊ถํ์ ๊ฐ์ง๋๋ค Polygon.
Polygon::width // protected access
Rectangle::width // protected access
Polygon::set_values() // public access
Rectangle::set_values() // public access
์ด๋ ์์ ๊ด๊ณ๊ฐ publicํ์๋ ๊ฐ ํด๋์ค์์ ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ธ๋์๊ธฐ ๋๋ฌธ์ ๋๋ค.
class Rectangle: public Polygon { /* ... */ }
์ฝ๋ก ( ) ๋ค์ ์ด publicํค์๋๋ ๋ค์ ์ค๋ ํด๋์ค( ์ด ๊ฒฝ์ฐ )์์ ์์๋ ๋ฉค๋ฒ ๊ฐ ํ์ ํด๋์ค( ์ด ๊ฒฝ์ฐ )์์ ๊ฐ์ง :์ ์๋ ๊ฐ์ฅ ์ก์ธ์ค ๊ฐ๋ฅํ ์์ค์ ๋ํ๋
๋๋ค . ๊ฐ ๊ฐ์ฅ ์ก์ธ์ค ๊ฐ๋ฅํ ์์ค์ด๋ฏ๋ก ์ด ํค์๋๋ฅผ ์ง์ ํ๋ฉด ํ์ ํด๋์ค๋ ๊ธฐ๋ณธ ํด๋์ค์์์ ๋์ผํ ์์ค์ ๊ฐ์ง ๋ชจ๋ ๋ฉค๋ฒ๋ฅผ ์์ํฉ๋๋ค . ๋ฅผ ์ฌ์ฉํ๋ฉด ๊ธฐ๋ณธ ํด๋์ค์ ๋ชจ๋ ๊ณต์ฉ ๋ฉค๋ฒ๊ฐ ํ์ ํด๋์ค์์์ ๊ฐ์ด ์์๋ฉ๋๋ค. ๋ฐ๋๋ก ๊ฐ์ฅ ์ ํ์ ์ธ ์ก์ธ์ค ์์ค์ ์ง์ ํ๋ฉด( ) ๋ชจ๋ ๊ธฐ๋ณธ ํด๋์ค ๋ฉค๋ฒ๊ฐ ๋ก ์์๋ฉ๋๋ค . ์๋ฅผ ๋ค์ด ๋ธ์ด ์ด๋จธ๋๋ก๋ถํฐ ํ์๋ ํด๋์ค์ธ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ด ์ ์ํฉ๋๋ค.PolygonRectanglepublic
protectedprotectedprivateprivate
class Daughter: protected Mother;
์ด๊ฒ์ ์ด๋จธ๋๋ก๋ถํฐ ์์๋ฐ์ protected๊ตฌ์ฑ์์ ๋ํด ๋ ์ ํ์ ์ธ ์ก์ธ์ค ์์ค์ผ๋ก ์ค์ ๋ฉ๋๋ค . Daughter์ฆ, ์ ์๋ ๋ชจ๋ ๋ฉค๋ฒ ๊ฐ ๊ฐ public๋ฉ๋๋ค . ๋ฌผ๋ก ์ด๊ฒ์ ์์ ์ public ๋ฉค๋ฒ๋ฅผ ์ ์ธํ๋ ๋ฐ ์ ํ์ ๋์ง ์์ต๋๋ค. ๋ ์ ํ์ ์ธ ์ก์ธ์ค ์์ค ์ ์์ ์์๋ ๊ตฌ์ฑ์์ ๋ํด์๋ง ์ค์ ๋ฉ๋๋ค . ์์์ ๋ํ ์ก์ธ์ค ์์ค์ด ์ง์ ๋์ง ์์ ๊ฒฝ์ฐ ์ปดํ์ผ๋ฌ๋ ํค์๋๋ก ์ ์ธ๋ ํด๋์ค์ ๋ํด private๋ก ๊ฐ์ ํ๊ณ ๋ก ์ ์ธ๋ ํด๋์ค์ ๋ํด public์ผ๋ก ๊ฐ์ ํฉ๋๋ค .MotherprotectedDaughterDaughterMother
classstruct
์ค์ ๋ก C++์์ ์์์ ๋๋ถ๋ถ์ ์ฌ์ฉ ์ฌ๋ก๋ ๊ณต๊ฐ ์์์ ์ฌ์ฉํด์ผ ํฉ๋๋ค. ๊ธฐ๋ณธ ํด๋์ค์ ๋ค๋ฅธ ์ก์ธ์ค ์์ค์ด ํ์ํ ๊ฒฝ์ฐ ์ผ๋ฐ์ ์ผ๋ก ๋์ ๋ฉค๋ฒ ๋ณ์๋ก ๋ ์ ํํํ ์ ์์ต๋๋ค.
๊ธฐ๋ณธ ํด๋์ค์์ ์์๋๋ ๊ฒ์ ๋ฌด์์ ๋๊น?
์์น์ ์ผ๋ก ๊ณต๊ฐ์ ์ผ๋ก ํ์๋ ํด๋์ค๋ ๋ค์์ ์ ์ธํ ๊ธฐ๋ณธ ํด๋์ค์ ๋ชจ๋ ๋ฉค๋ฒ์ ๋ํ ์ก์ธ์ค๋ฅผ ์์ํฉ๋๋ค.
- ์์ฑ์์ ์๋ฉธ์
- ํ ๋น ์ฐ์ฐ์ ๋ฉค๋ฒ(operator=)
- ๊ทธ ์น๊ตฌ๋ค
- ๊ฐ์ธ ํ์
๊ธฐ๋ณธ ํด๋์ค์ ์์ฑ์ ๋ฐ ์๋ฉธ์์ ๋ํ ์ก์ธ์ค๋ ๊ทธ๋๋ก ์์๋์ง ์์ง๋ง ํ์ ํด๋์ค์ ์์ฑ์ ๋ฐ ์๋ฉธ์์ ์ํด ์๋์ผ๋ก ํธ์ถ๋ฉ๋๋ค.
๋ฌ๋ฆฌ ์ง์ ํ์ง ์๋ ํ ํ์ ํด๋์ค์ ์์ฑ์๋ ๊ธฐ๋ณธ ํด๋์ค์ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ํธ์ถํฉ๋๋ค(์ฆ, ์ธ์๋ฅผ ์ฌ์ฉํ์ง ์๋ ์์ฑ์). ์ด๊ธฐํ ๋ชฉ๋ก์์ ๋ฉค๋ฒ ๋ณ์๋ฅผ ์ด๊ธฐํํ๋ ๋ฐ ์ฌ์ฉ๋ ๊ฒ๊ณผ ๋์ผํ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ ๊ธฐ๋ณธ ํด๋์ค์ ๋ค๋ฅธ ์์ฑ์๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
derived_constructor_name (parameters) : base_constructor_name (parameters) {...}
// constructors and derived classes
#include <iostream>
using namespace std;
class Mother {
public:
Mother ()
{ cout << "Mother: no parameters\n"; }
Mother (int a)
{ cout << "Mother: int parameter\n"; }
};
class Daughter : public Mother {
public:
Daughter (int a)
{ cout << "Daughter: int parameter\n\n"; }
};
class Son : public Mother {
public:
Son (int a) : Mother (a)
{ cout << "Son: int parameter\n\n"; }
};
int main () {
Daughter kelly(0);
Son bud(0);
return 0;
}
Mother์๋ก์ด Daughter๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ํธ์ถ๋๋ ์ ์์ฑ์์ ๊ฐ์ฒด ์ผ ๋ ํธ์ถ๋๋ ์์ฑ์ ์ ์ฐจ์ด์ ์ ์ฃผ๋ชฉํ์ญ์์ค Son. Daughter์ฐจ์ด์ ์ ๋ฐ ์ ์์ฑ์ ์ ์ธ์ด ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ๋๋ค Son.
Daughter (int a) // nothing specified: call default constructor
Son (int a) : Mother (a) // constructor specified: call this specific constructor
๋ค์ค ์์
ํด๋์ค๋ ํด๋์ค์ ๊ธฐ๋ณธ ํด๋์ค ๋ชฉ๋ก(์ฆ, ์ฝ๋ก ๋ค)์์ ์ผํ๋ก ๊ตฌ๋ถ๋ ๋ ๋ง์ ๊ธฐ๋ณธ ํด๋์ค๋ฅผ ๋จ์ํ ์ง์ ํ์ฌ ๋ ์ด์์ ํด๋์ค์์ ์์ํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ํ๋ก๊ทธ๋จ์ ๋ผ๋ ํ๋ฉด์ ์ธ์ํ ํน์ ํด๋์ค๊ฐ ์๊ณ ํด๋์ค๋ฅผ Output์ํ๊ณ ์ฐ๋ฆฌ๊ฐ ์์ฑํ ์ ์๋ ํด๋์ค ์ธ์ ํด๋น ๋ฉค๋ฒ๋ ์์๋ฐ๊ธฐ๋ฅผ ์ํ๋ฉด ๋ค์ Rectangle๊ณผ ๊ฐ์ด ์์ฑํ ์ ์์ต๋๋ค.TrianglePolygon
class Rectangle: public Polygon, public Output;
class Triangle: public Polygon, public Output;
// multiple inheritance
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
Polygon (int a, int b) : width(a), height(b) {}
};
class Output {
public:
static void print (int i);
};
void Output::print (int i) {
cout << i << '\n';
}
class Rectangle: public Polygon, public Output {
public:
Rectangle (int a, int b) : Polygon(a,b) {}
int area ()
{ return width*height; }
};
class Triangle: public Polygon, public Output {
public:
Triangle (int a, int b) : Polygon(a,b) {}
int area ()
{ return width*height/2; }
};
int main () {
Rectangle rect (4,5);
Triangle trgl (4,5);
rect.print (rect.area());
Triangle::print (trgl.area());
return 0;
}
'๐ Development Study > ๐จ๐พโ๐ป C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ณํ (0) | 2022.08.22 |
---|---|
๋คํ์ฑ (0) | 2022.08.22 |
์์ฑ์/์๋ฉธ์ (0) | 2022.08.22 |
ํด๋์ค(2) (0) | 2022.08.19 |
ํด๋์ค(1) (0) | 2022.08.19 |
๋๊ธ