Cplusplus-cpp-overloading
C ++のオーバーロード(演算子と関数)
C ++では、同じスコープ内の*関数*名または*演算子*に対して複数の定義を指定できます。これらは、それぞれ*関数のオーバーロード*および*演算子のオーバーロード*と呼ばれます。
オーバーロードされた宣言は、同じスコープ内で以前に宣言された宣言と同じ名前で宣言された宣言です。ただし、両方の宣言に異なる引数と明らかに異なる定義(実装)があります。
オーバーロードされた function または operator を呼び出すと、コンパイラは、関数または演算子の呼び出しに使用した引数の型を定義で指定されたパラメーターの型と比較することにより、使用する最も適切な定義を決定します。 最も適切なオーバーロード関数または演算子を選択するプロセスは、*オーバーロード解決*と呼ばれます。
C ++での関数のオーバーロード
同じスコープ内で同じ関数名に複数の定義を設定できます。 関数の定義は、引数リスト内の引数のタイプまたは数、あるいはその両方によって互いに異なる必要があります。 戻り型のみが異なる関数宣言をオーバーロードすることはできません。
以下は、同じ関数* print()*を使用して異なるデータ型を印刷する例です-
#include <iostream>
using namespace std;
class printData {
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(char* c) {
cout << "Printing character: " << c << endl;
}
};
int main(void) {
printData pd;
//Call print to print integer
pd.print(5);
//Call print to print float
pd.print(500.263);
//Call print to print character
pd.print("Hello C++");
return 0;
}
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Printing int: 5
Printing float: 500.263
Printing character: Hello C++
C ++でオーバーロードする演算子
C ++で利用可能なほとんどの組み込み演算子を再定義またはオーバーロードできます。 したがって、プログラマはユーザー定義型の演算子も使用できます。
オーバーロードされた演算子は、特別な名前を持つ関数です。キーワード「演算子」の後に、定義される演算子の記号が続きます。 他の関数と同様に、オーバーロードされた演算子には戻り値の型とパラメーターリストがあります。
Box operator+(const Box&);
2つのBoxオブジェクトを*追加*するために使用できる加算演算子を宣言し、最終的なBoxオブジェクトを返します。 ほとんどのオーバーロード演算子は、通常の非メンバー関数またはクラスメンバー関数として定義できます。 上記の関数をクラスの非メンバー関数として定義する場合、次のように各オペランドに2つの引数を渡す必要があります-
Box operator+(const Box&, const Box&);
以下は、メンバー関数を使用してオーバーロードする演算子の概念を示す例です。 ここでは、オブジェクトが引数として渡され、そのプロパティはこのオブジェクトを使用してアクセスされます。この演算子を呼び出すオブジェクトは、以下で説明するように this 演算子を使用してアクセスできます-
#include <iostream>
using namespace std;
class Box {
public:
double getVolume(void) {
return length *breadth* height;
}
void setLength( double len ) {
length = len;
}
void setBreadth( double bre ) {
breadth = bre;
}
void setHeight( double hei ) {
height = hei;
}
//Overload + operator to add two Box objects.
Box operator+(const Box& b) {
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length; //Length of a box
double breadth; //Breadth of a box
double height; //Height of a box
};
//Main function for the program
int main() {
Box Box1; //Declare Box1 of type Box
Box Box2; //Declare Box2 of type Box
Box Box3; //Declare Box3 of type Box
double volume = 0.0; //Store the volume of a box here
//box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
//box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
//volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
//volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
//Add two object as follows:
Box3 = Box1 + Box2;
//volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
オーバーロード可能/非オーバーロード可能演算子
以下は、オーバーロードできる演算子のリストです-
+ | - | * | / | % | ^ |
& | ~ | ! | , | ||
= | < | > | ⇐ | >= | ++ |
— | << | >> | == | != | && |
+= | -= | /= | |||
%= | ^= | &= | = | *= | |
<⇐ | >>= | [] | () | → | →* |
以下は、オーバーロードすることはできません演算子のリストです-
:: | .* | . | ?: |
演算子のオーバーロードの例
ここに、概念を理解するのに役立つさまざまな演算子オーバーロードの例を示します。
Sr.No | Operators & Example |
---|---|
1 | Unary Operators Overloading |
2 | Binary Operators Overloading |
3 | Relational Operators Overloading |
4 | Input/Output Operators Overloading |
5 | ++ and — Operators Overloading |
6 | Assignment Operators Overloading |
7 | Function call () Operator Overloading |
8 | Subscripting [ Operator Overloading] |
9 | Class Member Access Operator → Overloading |