interfaces ( ์ธํฐํ์ด์ค )
"ํน์ ๊ธฐ๋ฅ์ ๊ตฌํํ ๊ฒ์ ์ฝ์ํ ์ถ์ ํ์”์ด๋ผ๊ณ ๋ช ์๊ฐ ๋์ด์๋ค.
#include <iostream>
class Ihello
{
public:
virtual void print() = 0;
};
class korean : public Ihello
{
public:
virtual void print() {
std::cout << "์๋
ํ์ธ์." << std::endl;
}
};
class english : public Ihello
{
public:
virtual void print() {
std::cout << "hello" << std::endl;
}
};
int main(void)
{
korean ko;
english eng;
ko.print();
eng.print();
}
interfaces์ ๊ฐ๋ ์ ์ฝ๋๋ฅผ ํตํด ๋จผ์ ์ดํด๋ณด๋ฉด ์์ ๊ฐ๋ค.
์์ ์ฝ๋์ ๊ฒฝ์ฐ Ihello ๋ผ๋ ํด๋์ค๋ฅผ ๋ง๋ค์ด์ ๊ฐ ๋๋ผ๋ง๋ค ๋ค์ํ ์ธ์ฌ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅํ๊ณ ์ถ์ ์ํฉ์ผ๋
Ihello๋ฅผ ์์๋ฐ๋ korean๊ณผ english๊ฐ ์๋ค.
์ด๋ด๊ฒฝ์ฐ์ korean๊ณผ english๋ print๋ผ๋ ํจ์๋ฅผ ํตํด์ ์ธ์ฌ๋ง์ ์ถ๋ ฅ์ ํด์ผํ๋
Ihello๋ผ๋ ์ด๋ฆ์ผ๋ก ์ถ์ํด๋์ค๋ฅผ ๋ง๋ค์ด๋๋ ๋ฐฉ์์ interfaces๋ผ๊ณ ๋ถ๋ฅธ๋ค.
interfaces
class ICharacter
{
public:
virtual ~ICharacter() {}
virtual std::string const & getName() const = 0;
virtual void equip(AMateria* m) = 0;
virtual void unequip(int idx) = 0;
virtual void use(int idx, ICharacter& target) = 0;
};
interfaces์ ๊ฒฝ์ฐ ์ถ์ ํด๋์ค์๋ ๋ค๋ฅด๊ฒ ๊ฐ์ ์๋ฉธ์์ ์์ ๊ฐ์ํจ์๋ง ํฌํจํ๊ณ ์์ด์ผํ๋ค.
class Ihello;
class MateriaSource;
class ICharacter
์์ธ์ง๋ ๋ชจ๋ฅด๊ฒ ์ผ๋ interfaces๋ก ์ฌ์ฉํ ํด๋์ค๋ค์ ๋ง๋ค๋ ํด๋์ค๋ช ๋งจ ์์ I๋ฅผ ๋ถ์ด๋ ๊ท์น์ด ์๋๊ฑฐ ๊ฐ๋ค.
'42seoul > projects' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[CPP Module] 05 (0) | 2024.03.13 |
---|---|
[C++] Repetition and Exceptions (0) | 2024.03.13 |
[C++] abstract class (์ถ์ ํด๋์ค) (0) | 2023.09.13 |
[C++] ์๋ธ ํ์ ๋คํ์ฑ(Subtype Polymorphism) (0) | 2023.09.13 |
[CPP Module] 04 (0) | 2023.09.13 |