Anda di halaman 1dari 27

Pemrograman Berbasis Objek

C ++ Overloading (Operator dan Fungsi)

Asep Ramdhani Mahbub


Intro

1. C++ membolehkan kita untuk mendefinisikan satu ataulebih nama fungsi


atau operator dalam cakupan yg sama, yang dikenal dengan fungsi
overloading ataupun operator overloading.
2. Deklarasi overloading adalah deklarasi yang dideklarasikan dengan nama
yang sama dengan deklarasi yang dideklarasikan sebelumnya dalam
cakupan yang sama, kecuali kedua deklarasi tersebut memiliki argumen yang
berbeda dan definisi (implementasi) yang jelas berbeda.
3. Saat kita memanggil fungsi atau operator overloading, kompiler
menentukan definisi yang paling tepat untuk digunakan, dengan
membandingkan tipe argumen yang digunakan untuk memanggil fungsi
atau operator dengan tipe parameter yang ditentukan dalam definisi.
4. Proses pemilihan fungsi overloading atau operator overloading disebut
overload resolution.
Function Overloading di C++

 Kita dapat memiliki beberapa definisi untuk nama fungsi yang sama dalam
cakupan yang sama. Definisi fungsi harus berbeda satu sama lain dengan
jenis dan / atau jumlah argumen dalam daftar argumen. kita tidak dapat
membebani deklarasi fungsi yang hanya berbeda berdasarkan tipe
pengembalian.

 Berikut ini adalah contoh di mana fungsi cetak yang sama () digunakan
untuk mencetak berbagai tipe data.
Contoh fungsi overload
#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;
}
};
Contoh fungsi overload
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;
}

Ketika kode di atas dikompilasi dan dieksekusi, itu menghasilkan hasil sebagai berikut
Contoh fungsi overload
Operators Overloading di C++

Kita dapat mendefinisikan ulang atau membebani sebagian besar


operator bawaan yang tersedia di C++. Dengan demikian, seorang
programmer dapat menggunakan operator dengan tipe yang
ditentukan pengguna sendiri.
Operator overload adalah fungsi dengan nama khusus: kata kunci
"operator" diikuti dengan simbol untuk operator yang ditentukan.
Seperti fungsi lainnya, operator yang overload memiliki tipe kembali
dan daftar parameter.
Box operator+(const Box&);
Operators Overloading di C++

 Mendeklarasikan operator penambahan yang dapat digunakan


untuk menambahkan dua objek Box dan mengembalikan objek
Box final.
 Sebagian besar operator overload dapat didefinisikan sebagai
fungsi bukan anggota biasa atau sebagai fungsi anggota kelas.
 Jika kita mendefinisikan fungsi di atas sebagai fungsi non-
anggota kelas maka kita harus melewati dua argumen untuk
setiap operan sebagai berikut
Box operator+(const Box&, const Box&);
Operators Overloading di C++

 Berikut ini adalah contoh untuk menunjukkan konsep operator


overload menggunakan fungsi anggota. Di sini objek dilewatkan
sebagai argumen yang propertinya akan diakses menggunakan
objek ini, objek yang akan memanggil operator ini dapat diakses
menggunakan operator this seperti yang dijelaskan di bawah ini
Contoh operator overload (1)
#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;
}
Contoh operator overload (2)
// 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
};
Contoh operator overload (3)
// 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);
Contoh operator overload (4)
// 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;
}
Ketika kode di atas dikompilasi dan dieksekusi, itu menghasilkan hasil sebagai berikut

Volume of Box1 : 210


Volume of Box2 : 1560
Volume of Box3 : 5400
Overloadable/Non-overloadable Operators

Berikut ini adalah daftar operator yang dapat di overload


Overloadable/Non-overloadable Operators

Berikut ini adalah daftar operator yang tidak dapat di overload


Contoh operator unary (1)
#include <iostream>
using namespace std;

class Distance {
private:
int feet; // 0 to infinite
int inches; // 0 to 12

public:
// required constructors
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}
Contoh operator unary (2)
// method to display distance
void displayDistance() {
cout << "F: " << feet << " I:" << inches <<endl;
}

// overloaded minus (-) operator


Distance operator- () {
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
};
Contoh operator unary (3)
int main() {
Distance D1(11, 10), D2(-5, 11);

-D1; // lakukan pengurangan


D1.displayDistance(); // display D1

-D2; // lakukan pengurangan


D2.displayDistance(); // display D2

return 0;
}
Contoh binary operator (1)
Live Demo
#include <iostream>
using namespace std;

class Box {
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a 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;
}
Contoh binary operator (2)
// 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;
}
};

// 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);
Contoh binary operator (3)
// 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;
}
Contoh relational operator (1)
#include <iostream>
using namespace std;

class Distance {
private:
int feet; // 0 to infinite
int inches; // 0 to 12

public:
// required constructors
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}
// method to display distance
void displayDistance() {
cout << "F: " << feet << " I:" << inches <<endl;
}
Contoh relational operator (2)
// overloaded minus (-) operator
Distance operator- () {
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}

// overloaded < operator


bool operator <(const Distance& d) {
if(feet < d.feet) {
return true;
}
if(feet == d.feet && inches < d.inches) {
return true;
}

return false;
}
};
Contoh relational operator (3)
int main() {
Distance D1(11, 10), D2(5, 11);

if( D1 < D2 ) {
cout << "D1 is less than D2 " << endl;
} else {
cout << "D2 is less than D1 " << endl;
}

return 0;
}

Ketika kode di atas dikompilasi dan dieksekusi, itu menghasilkan hasil sebagai berikut

D2 is less than D1
Tugas Kelompok

Cari Referensi dan contoh program untuk operator-operator berikut ini:

1. Input/Output Operators Overloading


2. ++ and -- Operators Overloading
3. Assignment Operators Overloading
4. Function call () Operator Overloading
5. Subscripting [] Operator Overloading
6. Class Member Access Operator -> Overloading
Thank You

Anda mungkin juga menyukai