Operator Overloading Pada C++

Operator Overloading Pada C++ -- Operator Overloading ini berperan sangat penting karena membuat operator standar pada C++ seperti +,-,*, dan yang lainnya yang dapat bekerja dari tipe data yang telah ditentukan.

Overloading operator berfungsi untuk memudahkan operasi berbagai type data sesuai dengan kebutuhan kita misalkan yang melibatkan operasi yang melibatkan suatu object class. Dengan adanya operator overloading ini kita dapat membuat sebuah fungsi yang mendefinisikan ulang operator sehingga dapat melakukan perintah khusus dengan objek dari sebuah class.

Berikut Operator yang dapat di Overload :

Operator
Name
Type
,
Comma
Binary
!
Logical NOT
Unary
!=
Inequality
Binary
%
Modulus
Binary
%=
Modulus assignment
Binary
&
Bitwise AND
Binary
&
Address-of
Unary
&&
Logical AND
Binary
&=
Bitwise AND assignment
Binary
( )
Function call
( )
Cast Operator
Unary
*
Multiplication
Binary
*
Pointer dereference
Unary
*=
Multiplication assignment
Binary
+
Addition
Binary
+
Unary Plus
Unary
++
Increment1
Unary
+=
Addition assignment
Binary
Subtraction
Binary
Unary negation
Unary
––
Decrement1
Unary
–=
Subtraction assignment
Binary
–>
Member selection
Binary
–>*
Pointer-to-member selection
Binary
/
Division
Binary
/=
Division assignment
Binary
< 
Less than
Binary
<< 
Left shift
Binary
<<=
Left shift assignment
Binary
<=
Less than or equal to
Binary
=
Assignment
Binary
==
Equality
Binary
> 
Greater than
Binary
>=
Greater than or equal to
Binary
>> 
Right shift
Binary
>>=
Right shift assignment
Binary
[ ]
Array subscript
^
Exclusive OR
Binary
^=
Exclusive OR assignment
Binary
|
Bitwise inclusive OR
Binary
|=
Bitwise inclusive OR assignment
Binary
||
Logical OR
Binary
~
One’s complement
Unary
delete
Delete
new
New
conversion operators
conversion operators
Unary




















































Berikut merupakan Operator Overloading yang tidak dapat di overload :

Operator
Definition
::
Operator Cakupan Resolusi
?:
Operator Kondisi
.
Operator Pemilihan Member
Sizeof
Operator Size-of
.*
De-Reference Pointer Untuk Class Member








Untuk lebih jelas perhatikan contoh program berikut ini, program ini seperti biasa menggunakan program Microsoft Office Studio 2015 :

// OperatorOverloading.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream> // For stream I/O
using namespace std;

class CBox // Class definition at global scope
{
public:
explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0) : m_Length(lv), m_Width(wv), m_Height(hv)
{
cout << endl << "Constructor called.";
}
double Volume() const
{
return m_Length*m_Width*m_Height;
}
bool operator<(const CBox& aBox) const;
bool operator==(const CBox& aBox) const
{
return this->Volume() == aBox.Volume();
}

~CBox()
{
cout << "Destructor Called." << endl;
}

private:
double m_Length;
double m_Width;
double m_Height;
};

inline bool CBox::operator<(const CBox& aBox) const
{
return this->Volume() < aBox.Volume();
}

int main()
{
const CBox smallBox(4.0, 2.0, 1.0);
const CBox mediumBox(10.0, 4.0, 2.0);
CBox bigBox (30.0, 20.0, 40.0);
CBox thatBox (4.0, 2.0, 10.0);

if (mediumBox < smallBox)
cout << endl << "mediumBox lebih kecil dari smallBox";
if (mediumBox < bigBox)
cout << endl << "mediumBox lebih kecil dari bigBox";
else
cout << endl << "mediumBox tidak lebih kecil dari bigBox";
if (thatBox == mediumBox)
cout << endl << "thatBox sama dengan mediumBox";
else
cout << endl << "thatBox tidak sama dengan mediumBox";
cout << endl;
return 0;
}

Lalu Klik Debug => Start Without Debugging atau Kombinasi keyboard Ctrl + F5 dan hasilnya akan seperti ini :

Operator Overloading Pada C++

Untuk menggunakan Operator Overloading, kita harus menambahkan sintaks operator agar dapat berjalan, pada kode program diatas ini adalah contohnya :

inline bool CBox::operator<(const CBox& aBox) const
{
return this->Volume() < aBox.Volume();
}

Itulah penjelasan mengenai Operator Overloading Pada C++. Semoga bermanfaat.