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ย ์ด๋ž€ ๋ฉค๋ฒ„ํ•จ์ˆ˜๋ฅผ ๋™์ ์œผ๋กœ ํ• ๋‹นํ•˜๋Š” ๋ณ€์ˆ˜์ด๋‹ค. ์–ด๋–ค ๊ฐ์ฒด๋ฅผ ๋„ฃ๋Š๋ƒ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋ฉค๋ฒ„ํ•จ์ˆ˜๊ฐ€ ํ• ๋‹น๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.