C++ 演算子のオーバーロード
C++ 演算子のオーバーロード
このチュートリアルでは、例を使用して演算子のオーバーロードについて学習します。
C++ では、オブジェクトや構造体などのユーザー定義型に対する演算子の動作方法を変更できます。これは、演算子のオーバーロードとして知られています .たとえば、
3 つのオブジェクト c1 を作成したとします。 , c2 そして結果 Complex
という名前のクラスから 複素数を表す
演算子のオーバーロードにより、演算子の動作方法を変更できるため、+
の方法を再定義できます。 演算子が機能し、それを使用して c1 の複素数を追加します と c2 次のコードを記述してください:
result = c1 + c2;
のようなものの代わりに
result = c1.addNumbers(c2);
これにより、コードが直感的で理解しやすくなります。
注: int
のような基本的なデータ型に演算子のオーバーロードを使用することはできません 、 float
、 char
など。
C++ 演算子のオーバーロードの構文
演算子をオーバーロードするには、特別な operator
を使用します 関数。オーバーロードされた演算子で動作させたいオブジェクト/変数を持つクラスまたは構造内に関数を定義します。
class className {
... .. ...
public
returnType operator symbol (arguments) {
... .. ...
}
... .. ...
};
ここで、
returnType
関数の戻り値の型です。operator
はキーワードです。symbol
オーバーロードしたい演算子です。いいね:+
、<
、-
、++
などarguments
関数に渡される引数です。
単項演算子での演算子のオーバーロード
単項演算子は、1 つのオペランドのみを操作します。インクリメント演算子 ++
およびデクリメント演算子 --
単項演算子の例です。
例 1:++ 演算子 (単項演算子) のオーバーロード
// Overload ++ when used as prefix
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
void operator ++ () {
++value;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
// Call the "void operator ++ ()" function
++count1;
count1.display();
return 0;
}
出力
Count: 6
ここで ++count1;
を使うと 、void operator ++ ()
と呼ばれます。これにより、値が増加します オブジェクト count1 の属性 1.
注: 演算子をオーバーロードすると、それを使用して好きなように動作させることができます。たとえば、 ++
を使用できます 値を増やす 100ずつ。
ただし、これにより、コードが混乱し、理解しにくくなります。プログラマーとしての私たちの仕事は、オペレーターのオーバーロードを適切に、一貫性のある直感的な方法で使用することです。
上記の例は ++
の場合のみ機能します 接頭辞として使用されます。 ++
にする この構文を使用して後置として機能します。
void operator ++ (int) {
// code
}
int
に注意してください 括弧内。これは、単項演算子を接尾辞として使用するために使用される構文です。関数パラメーターではありません。
例 2:++ 演算子 (単項演算子) のオーバーロード
// Overload ++ when used as prefix and postfix
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
void operator ++ () {
++value;
}
// Overload ++ when used as postfix
void operator ++ (int) {
value++;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
// Call the "void operator ++ (int)" function
count1++;
count1.display();
// Call the "void operator ++ ()" function
++count1;
count1.display();
return 0;
}
出力
Count: 6 Count: 7
例 2 ++
の場合に機能します 接頭辞と接尾辞の両方として使用されます。ただし、次のようなことをしようとしてもうまくいきません:
Count count1, result;
// Error
result = ++count1;
これは、演算子関数の戻り値の型が void
であるためです。 .この問題は Count
にすることで解決できます 演算子関数の戻り型として。
// return Count when ++ used as prefix
Count operator ++ () {
// code
}
// return Count when ++ used as postfix
Count operator ++ (int) {
// code
}
例 3:演算子関数 (++ 演算子) からの戻り値
#include <iostream>
using namespace std;
class Count {
private:
int value;
public
:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
Count operator ++ () {
Count temp;
// Here, value is the value attribute of the calling object
temp.value = ++value;
return temp;
}
// Overload ++ when used as postfix
Count operator ++ (int) {
Count temp;
// Here, value is the value attribute of the calling object
temp.value = value++;
return temp;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1, result;
// Call the "Count operator ++ ()" function
result = ++count1;
result.display();
// Call the "Count operator ++ (int)" function
result = count1++;
result.display();
return 0;
}
出力
Count: 6 Count: 6
ここでは、前置演算子のオーバーロードに次のコードを使用しています:
// Overload ++ when used as prefix
Count operator ++ () {
Count temp;
// Here, value is the value attribute of the calling object
temp.value = ++value;
return temp;
}
後置演算子のオーバーロードのコードも同様です。オブジェクト temp を作成したことに注意してください その値を演算子関数に返しました。
また、コードに注意してください
temp.value = ++value;
変数 値 count1 に属します main()
のオブジェクト なぜなら count1 temp.value が関数を呼び出している間 一時に属します オブジェクト。
二項演算子での演算子のオーバーロード
二項演算子は 2 つのオペランドで機能します。たとえば、
result = num + 9;
ここでは、+
オペランド num で機能する二項演算子です と 9
.
コードを使用してユーザー定義型の二項演算子をオーバーロードすると:
obj3 = obj1 + obj2;
演算子関数は、obj1 を使用して呼び出されます object と obj2 関数に引数として渡されます。
例 4:C++ 二項演算子のオーバーロード
// C++ program to overload the binary operator +
// This program adds two complex numbers
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
// Constructor to initialize real and imag to 0
Complex() : real(0), imag(0) {}
void input() {
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}
// Overload the + operator
Complex operator + (const Complex& obj) {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
void output() {
if (imag < 0)
cout << "Output Complex number: " << real << imag << "i";
else
cout << "Output Complex number: " << real << "+" << imag << "i";
}
};
int main() {
Complex complex1, complex2, result;
cout << "Enter first complex number:\n";
complex1.input();
cout << "Enter second complex number:\n";
complex2.input();
// complex1 calls the operator function
// complex2 is passed as an argument to the function
result = complex1 + complex2;
result.output();
return 0;
}
出力
Enter first complex number: Enter real and imaginary parts respectively: 9 5 Enter second complex number: Enter real and imaginary parts respectively: 7 6 Output Complex number: 16+11i
このプログラムでは、演算子関数は次のとおりです:
Complex operator + (const Complex& obj) {
// code
}
これの代わりに、この関数を次のように書くこともできます:
Complex operator + (Complex obj) {
// code
}
ただし、
&
を使用 complex2 を参照することでコードを効率的にします 演算子関数内で複製オブジェクトを作成する代わりに、オブジェクトを作成します。const
を使用 演算子関数が complex2 を変更するのを防ぐため、良い習慣と見なされます .
C++ 演算子のオーバーロードで覚えておくべきこと
<オール>=
と &
C++ ではデフォルトですでにオーバーロードされています。たとえば、同じクラスのオブジェクトをコピーするには、 =
を直接使用できます オペレーター。演算子関数を作成する必要はありません。::
(範囲の解決).
(メンバーの選択).*
(関数へのポインターによるメンバー選択)?:
(三項演算子)
詳細については、次のページをご覧ください:
- インクリメント演算子を正しい方法でオーバーロードする方法
- 二項演算子をオーバーロードする方法 - 複素数を減算するには?
C言語