Header File
์ ์ฌ์ฉํ๋๊ฐ?
C++ ์ฝ๋๋ฅผ ์์ฑํ๋ค๊ฐ ๋ณด๋ฉด, Class ๋ด์ ๋ฉค๋ฒ๋ณ์, ๋ฉค๋ฒํจ์, ๋ ๋ด๊ฐ ๋ง๋ค์ด์ ์ฌ์ฉํ๋ ์ฌ์ฉ์ ์ ์ ํจ์, main ํจ์ ๋ฑ ๊ฒฐ๊ตญ ์ด๋ค ํ๋ก๊ทธ๋จ์ ๋์ํ๊ณ ์ถ์ ๊ฑด์ง ์ ์ฒด์ ๊ตฌ์กฐ๋ฅผ ์๊ธฐ ์ด๋ ต๋ค๋ ์ ์ด ์๋ค.
๋ฐ๋ผ์ ์ฐ๋ฆฌ๋ class๋ฅผ ์ ์ํ๋ ๊ฒ์ hpp ํ์ฅ์์, ๊ทธ๋ฆฌ๊ณ ๊ทธ ํด๋์ค์ ํด๋น๋๋ ๋ฉค๋ฒํจ์๋ค์ cpp ํ์ฅ์์, ํ๋ก๊ทธ๋จ์ด ๋์ํ๋ main ์๋ ์ง์ ๋ถํ ๊ฒ์ ์์ ๊ณ ํฐํ๋ง ๋๋ ๊ฒ์ด๋ค.
์ด ๋, ๋ด๊ฐ ๋ง๋ค์ด ๋์ hpp ๋ฅผย includeย ํ๊ณ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
์ฌํ๊ปย <>
ย ์์ ์ ์๋ ๊ฒ์, ๊ธฐ๋ณธ์ ์ผ๋ก c++์์ ์ ๊ณตํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ด๋ฉฐ, ์ด์ ๋ถํฐ ๋ด๊ฐ ๋ง๋ค์ด ์ฌ์ฉํ๋ ๋
์๋ค์ย ""
ย ์์ ์ ์ด์ ์ฌ์ฉํ๋ค.
main()
//
// main.cpp
// last_example
//
// Created by ์ต์์ on 13/05/2019.
// Copyright ยฉ 2019 ์ต์์. All rights reserved.
//
#include <iostream>
#include <vector>
#include <string>
#include "CText.hpp"
#include "CFancyText.hpp"
#include "CFixedText.hpp"
using namespace std;
int main() {
vector<Text*> v{ new Text("Plain"),
new FancyText("Fancy1", "<", ">", "-"),
new FixedText };
Text* pText;
pText = new FancyText("Fancy2", "[", "]", "*");
v.push_back(pText);
pText = new Text("Plain2");
v.push_back(pText);
for (auto elem : v)
elem->append("A");
for (unsigned i = 0; i < v.size(); i++)
cout << i << " : " << v.at(i)->get() << endl;
return 0;
}
์ฐ๋ฆฌ๋, v๋ผ๋ ๋ฒกํฐ์์ Text ๊ฐ์ฒด, FanctText ๊ฐ์ฒด, FixedText ๊ฐ์ฒด๋ฅผ ๋ฃ์ ๊ฒ์ด๋ค.
FancyText ํด๋์ค๋ Text๋ฅผ ์์๋ฐ์ ์ถ๊ฐ์ ์ธ ํ ์คํธ๋ฅผ ์ถ๊ฐํ๋ค. FixedText ํด๋์ค๋ Text๋ฅผ ์์๋ฐ์ง๋ง ์ผ๊ด๋ ์ถ๋ ฅ์ ํ๋ค. ๋ง์ง๋ง์ผ๋ก ๋ชจ๋ ๊ฐ์ฒด๋ Text ํด๋์ค๋ก ๋ถํฐ append ๋ผ๋ ๋ฉค๋ฒํจ์๋ฅผ ์์๋ฐ์ผ๋, for๋ฌธ์ ๋๋ฉด์ A๋ผ๋ ๋ฌธ์๋ฅผ ๋ค ์ถ๊ฐํ๋ค.
CText.hpp
#pragma once
#include <string>
using namespace std;
// Text.h
class Text {
private:
string text;
public:
Text(string _t);
virtual string get();
virtual void append(string _extra);
};
pragma once๋ ํ ๋ฒinclude ๋ ํ์ผ์ ๋ค์ ์ด์ง ์๋๋ก ํ๋ ๋ช ๋ น์ด์ด๋ค.
CText.cpp
#include "CText.hpp"
// Text.cpp
Text::Text(string _t) : text(_t) {}
string Text::get() { return text; }
void Text::append(string _extra) { text += _extra; }
๊ฐ์ ์ด๋ฆ์ ๊ฐ์ง ํค๋ํ์ผ์ cpp์์ include ํ๊ณ ๋ฉค๋ฒํจ์๋ฅผ ์์ฑํ๋ค.
CFancyText.hpp
#pragma once
#include <string>
#include "CText.hpp"
using namespace std;
class FancyText : public Text {
private:
string left_brac;
string right_brac;
string connector;
public:
FancyText(string _t, string _lb, string _rb, string _con);
string get() override;
void append(string _extra) override;
};
CFancyText.cpp
#include "CFancyText.hpp"
FancyText::FancyText(string _t, string _lb, string _rb, string _con) :
Text::Text(_t), left_brac(_lb), right_brac(_rb), connector(_con) {}
string FancyText::get() { return left_brac + Text::get() + right_brac; }
void FancyText::append(string _extra) {
Text::append(connector + _extra);
}
CFixedText.hpp
#include "CText.hpp"
using namespace std;
class FixedText : public Text {
public:
FixedText();
void append(string _extra) override;
};
CFixedText.cpp
#include "CFixedText.hpp"
FixedText::FixedText() : Text::Text("FIXED") {}
void FixedText::append(string _extra) {
//NOOP
}
๋ค์ main()
#include <iostream>
#include <vector>
#include <string>
#include "CText.hpp"
#include "CFancyText.hpp"
#include "CFixedText.hpp"
using namespace std;
int main() {
vector<Text*> v{ new Text("Plain"),
new FancyText("Fancy1", "<", ">", "-"),
new FixedText };
Text* pText;
pText = new FancyText("Fancy2", "[", "]", "*");
v.push_back(pText);
pText = new Text("Plain2");
v.push_back(pText);
for (auto elem : v)
elem->append("A");
for (unsigned i = 0; i < v.size(); i++)
cout << i << " : " << v.at(i)->get() << endl;
return 0;
}
Output
0 : PlainA
1 : <Fancy1-A>
2 : FIXED
3 : [Fancy2*A]
4 : Plain2A
Program ended with exit code: 0
Binding ์ ์๊ฐํ
vtable
ย ์ด๋ ๋ฉค๋ฒํจ์๋ฅผ ๋์ ์ผ๋ก ํ ๋นํ๋ ๋ณ์์ด๋ค. ์ด๋ค ๊ฐ์ฒด๋ฅผ ๋ฃ๋๋์ ๋ฐ๋ผ ๋ค๋ฅธ ๋ฉค๋ฒํจ์๊ฐ ํ ๋น๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค.