フレンド関数を示す

C++C++Beginner
今すぐ練習

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、C++ プログラミング言語における「フレンド関数」の概念をどのように示すかを学びます。プライベートなメンバ変数を持つ Volume クラスを作成し、クラスの外からこれらのプライベート変数にアクセスするために「フレンド関数」を使用します。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("C++")) -.-> cpp/OOPGroup(["OOP"]) cpp(("C++")) -.-> cpp/FunctionsGroup(["Functions"]) cpp/FunctionsGroup -.-> cpp/function_parameters("Function Parameters") cpp/OOPGroup -.-> cpp/classes_objects("Classes/Objects") cpp/OOPGroup -.-> cpp/access_specifiers("Access Specifiers") cpp/OOPGroup -.-> cpp/constructors("Constructors") cpp/OOPGroup -.-> cpp/encapsulation("Encapsulation") subgraph Lab Skills cpp/function_parameters -.-> lab-96140{{"フレンド関数を示す"}} cpp/classes_objects -.-> lab-96140{{"フレンド関数を示す"}} cpp/access_specifiers -.-> lab-96140{{"フレンド関数を示す"}} cpp/constructors -.-> lab-96140{{"フレンド関数を示す"}} cpp/encapsulation -.-> lab-96140{{"フレンド関数を示す"}} end

Volume クラスを作成する

まず、~/project ディレクトリに新しい C++ ファイルを作成し、その名前を main.cpp とします。そして、Volume クラスを定義するために次のコードを追加します。

#include <iostream>

using namespace std;

//Class Volume to demonstrate the concept of Friend Function in CPP
class Volume {
    //Member variables are declared as private and hence cannot be simply accessed from outside the class
    private:
        int liter;

    //Initializing the value of variable liter to 2 using the default constructor
    public:
        Volume(): liter(2) {}
};

クラスの外から直接アクセスできないプライベートなメンバ変数 liter を持つ Volume クラスを定義します。

Volume クラスに対する「フレンド関数」を宣言する

次に、Volume クラスに対する「フレンド関数」を宣言します。これにより、クラスの外からこのクラスのプライベート変数にアクセスできるようになります。

//Declaring the Friend Function for the class Volume
friend int mulFive(Volume);

mulFive() という名前の「フレンド関数」を宣言します。この関数は Volume オブジェクトを入力として受け取り、整数値を返します。

「フレンド関数」を定義する

次に、mulFive() 関数を定義します。この関数は、「フレンド関数」の機能を使って Volume クラスのプライベート変数にアクセスします。mulFive() 関数を定義するには、次のコードを追加します。

// Defining the Friend Function to be used by the Volume Class
int mulFive(Volume v) {
    //Friend function enables accessing private data from non-member function
    v.liter *= 5;
    return v.liter;
}

「フレンド関数」の機能を使って Volume のプライベート変数 liter にアクセスできる mulFive() 関数を定義します。この関数は、liter 変数を 5 倍にして返します。

main() 関数で「フレンド関数」を呼び出す

main() 関数では、Volume クラスのオブジェクトを作成し、mulFive() 関数を呼び出して Volume クラスのプライベートデータにアクセスして操作します。これが最後で最も重要なステップです。

//Defining the main method to access the members of the class
int main() {

    cout << "\n\nWelcome to LabEx :-)\n\n\n";
    cout << " =====  Program to demonstrate the working of a Friend Function in CPP  ===== \n\n";

    //Declaring the Class objects to access the class members
    Volume vol;

    cout << "Volume after calling the Friend Function = " << mulFive(vol);

    cout << "\n\n\n";

    return 0;
}

main() 関数を定義します。この関数は、Volume クラスのオブジェクトを作成し、Volume クラスのプライベートデータにアクセスするために mulFive() 関数を呼び出します。

まとめ

この実験では、Volume クラスを作成し、このクラスのプライベート変数にクラスの外からアクセスするために「フレンド関数」を使用することで、C++ プログラミング言語における「フレンド関数」の概念を示す方法を学びました。「フレンド関数」は、クラスのプライベートメンバにアクセスできる非メンバ関数であり、クラスのプライベートデータを操作またはアクセスするために、いくつかのケースで役立つことができます。