はじめに
この実験では、C++ プログラミング言語における +
演算子のオーバーロードの概念をどのように示すか学びます。演算子のオーバーロードは、C++ の機能であり、使用されるコンテキストに応じて、単一の演算子またはシンボルを異なる意味で使用できるようにします。この実験では、+
演算子を使用して 2 つの直方体オブジェクトを加算する方法を示します。
この実験では、C++ プログラミング言語における +
演算子のオーバーロードの概念をどのように示すか学びます。演算子のオーバーロードは、C++ の機能であり、使用されるコンテキストに応じて、単一の演算子またはシンボルを異なる意味で使用できるようにします。この実験では、+
演算子を使用して 2 つの直方体オブジェクトを加算する方法を示します。
~/project
ディレクトリに main.cpp
という名前の新しい C++ ファイルを作成します。
cd ~/project
touch main.cpp
+
演算子のオーバーロードを示すコードを記述する3 次元の直方体を表す Cuboid
という名前のクラスを作成するために、以下のコードを main.cpp
に追加します。
#include <iostream>
using namespace std;
//CPP におけるプラス演算子のオーバーロードの概念を示すために、Cuboid クラスを定義する
class Cuboid {
//クラスの外部からアクセスするために、クラスメンバ変数を public に宣言する
public:
double length; // 直方体の長さ
double breadth; // 直方体の幅
double height; // 直方体の高さ
public:
double getVolume(void) {
return length * breadth * height;
}
void setLength(double l) {
length = l;
}
void setBreadth(double b) {
breadth = b;
}
void setHeight(double h) {
height = h;
}
// 2 つの Cuboid オブジェクトを互いに加算するために、+演算子をオーバーロードする。
Cuboid operator + (const Cuboid & c) {
Cuboid cuboid;
cuboid.length = this -> length + c.length;
cuboid.breadth = this -> breadth + c.breadth;
cuboid.height = this -> height + c.height;
return cuboid;
}
};
main
関数を定義する3 つの Cuboid
オブジェクトを作成し、それらの寸法を設定し、体積を計算し、2 つのオブジェクトを加算し、結果の直方体オブジェクトの寸法と体積を表示する main
関数を実装するために、以下のコードを main.cpp
に追加します。
//クラスのメンバにアクセスするための main メソッドを定義する
int main() {
cout << "\n\nWelcome to LabEx :-)\n\n\n";
cout << " ===== Program to demonstrate the Plus Operator Overloading, in CPP ===== \n\n";
//クラスメンバにアクセスするためのクラスオブジェクトを宣言する
Cuboid c1;
Cuboid c2;
Cuboid c3;
//直方体の体積を格納する
double volume = 0.0;
//最初の直方体オブジェクト c1 の長さ、幅、高さを設定する
c1.setLength(3.0);
c1.setBreadth(4.0);
c1.setHeight(5.0);
//2 番目の直方体オブジェクト c2 の長さ、幅、高さを設定する
c2.setLength(2.0);
c2.setBreadth(5.0);
c2.setHeight(8.0);
//最初の直方体 c1 の体積を求める
cout << "Calling the getVolume() method to find the volume of Cuboid c1\n";
volume = c1.getVolume();
cout << "Volume of the Cuboid c1 is : " << volume << "\n\n\n";
//最初の直方体 c1 の体積を求める
cout << "Calling the getVolume() method to find the volume of Cuboid c2\n";
volume = c2.getVolume();
cout << "Volume of the Cuboid c2 is : " << volume << "\n\n\n";
//2 つの直方体オブジェクト c1 と c2 を加算して、3 番目のオブジェクト c3 を作成する
c3 = c1 + c2;
//3 番目の直方体 c3 の寸法を表示する
cout << "Length of the Cuboid c3 is : " << c3.length << endl;
cout << "Breadth of the Cuboid c3 is : " << c3.breadth << endl;
cout << "Height of the Cuboid c3 is : " << c3.height << endl;
//3 番目の直方体 c3 の体積を求める
cout << "\n\nCalling the getVolume() method to find the volume of Cuboid c3\n";
volume = c3.getVolume();
cout << "Volume of the Cuboid c3 is : " << volume << endl;
cout << "\n\n\n";
return 0;
}
コードをコンパイルして実行するには、次のコマンドを使用します。
g++ main.cpp -o main && ./main
次の出力が表示されます。
Welcome to LabEx :-)
===== Program to demonstrate the Plus Operator Overloading, in CPP =====
Calling the getVolume() method to find the volume of Cuboid c1
Volume of the Cuboid c1 is : 60
Calling the getVolume() method to find the volume of Cuboid c2
Volume of the Cuboid c2 is : 80
Length of the Cuboid c3 is : 5
Breadth of the Cuboid c3 is : 9
Height of the Cuboid c3 is : 13
Calling the getVolume() method to find the volume of Cuboid c3
Volume of the Cuboid c3 is : 585
この実験では、C++ プログラミング言語における +
演算子のオーバーロードの概念を示す方法を学びました。演算子のオーバーロードは、C++ の強力で便利な機能であり、異なるコンテキストで異なる意味で演算子を使用できるようにします。+
演算子をオーバーロードすることで、2 つの Cuboid オブジェクトを互いに加算できます。