SW/C++

C++11 : 멤버 함수 선언과 정의 분리에 대해 알아볼까요? (개념 및 예제)

얇은생각 2019. 1. 5. 07:30
반응형

클래스의 멤버 변수는 클래스 내부에 선언한 변수이며, 클래스의 멤버 함수는 클래스 내부에 선언한 함수입니다. 객체를 생성하면 클래스의 멤버 변수는 객체의 속성이 되고, 클래스 멤버 함수는 객체의 기능이 됩니다. 이를 통해서 추상화를 구현할 수 있습니다.


C에서 선언문은 보통 헤더 파일에 작성하고, 정의는 소스 파일에 작성해보았던 경험이 있을 것입니다. 마찬가지로 C++ 에서도 보통 헤더 파일에 클래스 멤버 변수와 함수를 선언하고, 소스 파일에 멤버 함수를 정의하는 방식을 따릅니다.


#include <iostream>
using namespace std;
#define NAME_LEN 20
#define SEX_LEN 10
#define JOB_LEN 20
#define CHARACTER_LEN 20
class Chulsoo {
    private : char name[NAME_LEN];
    char sex[SEX_LEN];
    char job[JOB_LEN];
    char character[CHARACTER_LEN];
    int age;
    bool marriageStatus;
    public : void introduce();
    void eat(char * food);
    void sleep();
    void drive(char * destination);
    void write();
    void read();
    void constructor(char * name, char * sex, char * job, char * character, int age, bool marriageStatus);
};
class Younghee {
    private : char name[NAME_LEN];
    char sex[SEX_LEN];
    char job[JOB_LEN];
    char character[CHARACTER_LEN];
    int age;
    bool marriageStatus;
    public : void introduce();
    void eat(char * food);
    void sleep();
    void shopping();
    void constructor(char * name, char * sex, char * job, char * character, int age, bool marriageStatus);
};
int main(void) {
    Chulsoo chulsoo;
    Younghee younghee;
    chulsoo.constructor("철수""남성""작가""diligent"32true);
    younghee.constructor("영희""여성""주부""impatient"32true);
    chulsoo.drive("레스토랑");
    chulsoo.eat("스테이크");
    younghee.eat("스테이크");
    chulsoo.drive("집");
    younghee.sleep();
    chulsoo.write();
    chulsoo.read();
    chulsoo.sleep();
    cout << endl;
    chulsoo.introduce();
    cout << endl;
    younghee.introduce();
    return 0;
}
void Chulsoo::introduce() {
    cout << "이름: " << name << endl;
    cout << "성별: " << sex << endl;
    cout << "직업: " << job << endl;
    cout << "성격: " << character << endl;
    cout << "나이: " << age << endl;
    cout << "결혼여부: " << (
        marriageStatus
            ? "YES"
            : "NO"
    ) << endl;
}
void Chulsoo::eat(char * food) {
    cout << "철수는 " << food << "를 먹는다" << endl;
}
void Chulsoo::sleep() {
    cout << "철수는 잔다" << endl;
}
void Chulsoo::drive(char * destination) {
    cout << "철수는 " << destination << "으로 운전한다" << endl;
}
void Chulsoo::write() {
    cout << "철수는 책을 쓴다" << endl;
}
void Chulsoo::read() {
    cout << "철수는 책을 읽는다" << endl;
}
void Chulsoo::constructor(char * name, char * sex, char * job, char * character, int age, bool marriageStatus) {
    strcpy_s(this -> name, name);
    strcpy_s(this -> sex, sex);
    strcpy_s(this -> job, job);
    strcpy_s(this -> character, character);
    this -> age = age;
    this -> marriageStatus = marriageStatus;
}
void Younghee::introduce() {
    cout << "이름: " << name << endl;
    cout << "성별: " << sex << endl;
    cout << "직업: " << job << endl;
    cout << "성격: " << character << endl;
    cout << "나이: " << age << endl;
    cout << "결혼여부: " << (
        marriageStatus
            ? "YES"
            : "NO"
    ) << endl;
}
void Younghee::eat(char * food) {
    cout << "영희는 " << food << "를 먹는다" << endl;
}
void Younghee::sleep() {
    cout << "영희는 잔다" << endl;
}
void Younghee::shopping() {
    cout << "영희는 쇼핑을 한다" << endl;
}
void Younghee::constructor(char * name, char * sex, char * job, char * character, int age, bool marriageStatus) {
    strcpy_s(this -> name, name);
    strcpy_s(this -> sex, sex);
    strcpy_s(this -> job, job);
    strcpy_s(this -> character, character);
    this -> age = age;
    this -> marriageStatus = marriageStatus;
}
cs


핵심은 멤버 함수를 클래스 외부에 정의하는 것입니다. 정의는 다음과 같은 형식을 따릅니다.


반환값 클래스명 :: 함수명() { … }


기존에 클래스 내부에 정의할 때는 클래스명이 없었는데 클래스 외부에 정의하므로 클래스명을 추가합니다. Name 변수를 선언하지 않고서 사용하는데 Chulso:: 코드에 의해 Chulsoo 클래스 내에 선언된 멤버 변수에 대한 접근을 허용하기 때문입니다. 마찬가지로 this 포인터를 사용할 때도 Chulsoo:: 코드에 의해서 Chulsoo 클래스 자신을 가리키는 것으로 판단합니다. 


클래스 선언부이므로 헤더 파일에 있을 것이고, main() 함수에 남을 것이고, 헤더 파일의 클래스 선언부를 정의했으므로 소스 파일로 분할될 것입니다.

반응형