C++ 클래스 구현

C++Beginner
지금 연습하기

소개

이 랩에서는 C++ 프로그래밍에서 클래스 (Class) 와 그 멤버 (member) 의 개념을 구현하는 과정을 안내합니다. 클래스는 객체를 위한 청사진 역할을 하는 사용자 정의 데이터 타입입니다. 클래스의 멤버는 변수 또는 함수가 될 수 있으며, public, private 또는 protected 로 정의될 수 있습니다.

메인 소스 파일 생성 및 편집

먼저, 메인 소스 파일을 생성하고 편집해야 합니다. 터미널을 열고 cd 명령을 사용하여 프로젝트 디렉토리로 이동합니다.

cd ~/project

메인 소스 파일을 생성하고 엽니다.

touch main.cpp

다음 코드를 파일에 추가합니다.

#include <iostream>
using namespace std;

class LabEx {

    private:
        int value;

    public:
        void input() {
            cout << "Entering the input() function\n";
            cout << "Enter an integer you want to display: ";
            cin >> value;
            cout << "Exiting the input() function\n\n";
        }

        void display() {
            cout << "\nEntering the display() function\n";
            cout << "The value entered is: " << value << endl;
            cout << "Exiting the display() function\n\n";
        }
};

int main() {
    cout << "\n\nWelcome to LabEx :-)\n\n\n";
    cout << " =====  Program to demonstrate the concept of Class, in CPP  ===== \n\n";

    LabEx object;

    cout << "\n\nCalling the input() function from the main() method\n\n\n";
    object.input();

    cout << "\nCalling the display() function from the main() method\n\n\n";
    object.display();

    cout << "\n\nExiting the main() method\n\n\n";

    return 0;
}

이 코드는 input()display() 두 개의 멤버 함수를 가진 LabEx 클래스를 정의합니다. input() 함수는 사용자로부터 입력을 받아 value에 저장하고, display() 함수는 저장된 값을 화면에 출력합니다.

프로그램 컴파일 및 실행

터미널에서 다음 명령을 실행하여 프로그램을 컴파일합니다.

g++ main.cpp -o main && ./main

컴파일 및 실행이 성공적으로 완료되면 다음과 같은 출력을 볼 수 있습니다.

Welcome to LabEx :-)


 =====  Program to demonstrate the concept of Class, in CPP  =====


Calling the input() function from the main() method


Entering the input() function
Enter an integer you want to display: 5
Exiting the input() function


Calling the display() function from the main() method


Entering the display() function
The value entered is: 5
Exiting the display() function


Exiting the main() method

요약

이 랩에서는 클래스와 멤버를 정의하는 방법, 클래스의 객체를 선언하고 초기화하는 방법, 그리고 생성자를 사용하여 클래스 멤버에 접근하는 방법을 배웠습니다.

이제 C++ 클래스와 멤버의 개념을 사용하여 사용자 정의 데이터 형식이 필요한 고급 프로그램을 작성할 수 있습니다. 또한 이 개념은 객체 지향 프로그래밍 (OOP) 기법을 적용하여 더 나은 코드 구성과 가독성 향상을 달성하는 데 도움이 될 수 있습니다.

전체 코드

구현에 따라 경로와 파일 이름을 수정하는 것을 잊지 마세요.

#include <iostream>
using namespace std;

class LabEx {

    private:
        int value;

    public:
        void input() {
            cout << "Entering the input() function\n";
            cout << "Enter an integer you want to display: ";
            cin >> value;
            cout << "Exiting the input() function\n\n";
        }

        void display() {
            cout << "\nEntering the display() function\n";
            cout << "The value entered is: " << value << endl;
            cout << "Exiting the display() function\n\n";
        }
};

int main() {
    cout << "\n\nWelcome to LabEx :-)\n\n\n";
    cout << " =====  Program to demonstrate the concept of Class, in CPP  ===== \n\n";

    LabEx object;

    cout << "\n\nCalling the input() function from the main() method\n\n\n";
    object.input();

    cout << "\nCalling the display() function from the main() method\n\n\n";
    object.display();

    cout << "\n\nExiting the main() method\n\n\n";

    return 0;
}