C++ クラスの実装

C++Beginner

はじめに

この実験では、C++ プログラミングにおけるクラスとそのメンバーの概念を実装する方法を案内します。クラスは、オブジェクトのブループリントとして機能するユーザー定義データ型です。クラスのメンバーは、変数または関数であり、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;
}

このコードは、2 つのメンバー関数 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;
}