Matakuliah
Tahun
: T0456 / Algoritma dan Metode Object
Oriented Programming
: 2007
Operator Overloading
Pertemuan 20
Learning Outcomes
Pada akhir pertemuan ini, diharapkan:
Mahasiswa dapat menerapkan operator overloading pada pembuatan
class .
Buku Referensi:
C++ - How to program, Deitel & Deitel,
Prentice Hall, 2001.
Websites:
http://www.deitel.com
3
Bina Nusantara
Outline Materi
•
•
•
•
•
Manfaat overloading operator
Operator yang dapat dioverloading
Operator yang tidak dapat dioverloading
Overloading operator dengan friend
Contoh program overloading
4
Bina Nusantara
Operator Overloading
Konsep overloading terhadap operator didasarkan pada
operasi yang terjadi sehari-hari misalkan operator + dapat
digunakan untuk menjumlahkan bilangan bulat ataupun
bilangan pecahan.
4+5
3.56 + 3.08
Bina Nusantara
Operator Overloading
Sehingga dengan menggunakan operator overloading ini
dimungkinkan terjadi operasi sbb:
VectorA + VectorB
Dimana VectorA dan VectorB adalah object Vector.
Syntax operator overloading:
Tipe operator simbol_operator ( argument )
Bina Nusantara
Operator yang dapat dioverloading
+
-
*
/
%
^
&
|
~
!
=
>
<
+=
-=
*=
/=
%=
^=
&=
|=
>=
<=
&&
||
==
!=
<<=
>>=
<<
>>
++
--
->*
,
->
[]
()
new
New[]
delete
Delete[]
Bina Nusantara
Operator yang tidak dapat dioverloading
Operator
Kegunaan
Contoh
.
Operator anggota struct atau class
Cin.getline()
.*
Operator pointer ke anggota
Object.*anggota
::
Operator lingkup
Class::anggota
?:
Ungkapan kondisi
C=(a>b)?a:b
Bina Nusantara
Overloading operator biner
Operator biner merupakan operator dengan dua operand,
contoh operator+.
Bentuk umum overloading operator biner adalah:
TipeData operator simbol_operator (object )
Bina Nusantara
Contoh Program
// program C++ dengan fungsi friend
#include <constream.h>
class Complex {
private:
double r, i;
public:
Complex() {r = 0; i = 0;}
Complex(double x){ r = x; i=0; }
Complex(double a, double b) {this->r = a; this->i =
b;}
Complex(Complex &c) {r = c.r; i = c.i; }
void setReal(double x) { r=x;}
void setIm(double y) { i=y;}
double getReal(){ return r; }
double getIm() { return i;}
Complex operator +(Complex c1);
Complex operator -(Complex c1);
Bina Nusantara
Complex operator *(Complex c1);
Complex operator *(int A1);
Complex operator *(double A1);
friend istream& operator >> (istream& in, Complex& c1);
friend ostream& operator << (ostream& out, Complex c1);
};
void main() {
clrscr();
Complex A(2,3), B, C;
cout << "Inputkan bil komplek: "; cin >> B;
cout << "Bil komplek I: "; cout << A << endl;
cout << "Bil komplek II: "; cout << B << endl;
C = B+A;
cout << "Bil komplek III: "; cout << C << endl;
C = B-A;
Bina Nusantara
cout << "Bil
C = B*A;
cout << "Bil
cout << "Bil
cout << "Bil
komplek IV: "; cout << C << endl;
komplek V: "; cout << C << endl;
komplek VI: "; cout << C*2 << endl;
komplek VI: "; cout << C*2.5 << endl;
}
Complex Complex::operator +(Complex c1) {
this->r += c1.r;
this->i += c1.i;
return (*this);
}
Complex Complex::operator -(Complex c1) {
this->r -= c1.r;
this->i -= c1.i;
return (*this);
}
Bina Nusantara
Complex Complex::operator *(Complex c1) {
Complex temp;
temp.r = r*c1.r-i*c1.i;
temp.i = r*c1.i+c1.r*i;
return(temp);
}
Complex Complex::operator *(int c1){
r = r*c1; i = c1*i;
return(*this);
}
Complex Complex::operator *(double c1){
r = c1*r; i = c1*i;
return(*this);
}
Bina Nusantara
istream& operator >> (istream& in, Complex& c1) {
cout << "\ninputkan bagian real: ";in >> c1.r;
cout << "inputkan bag imaginer: ";in >> c1.i;
return in;
}
ostream& operator << (ostream& out, Complex c1) {
if (c1.i!=0.0 && c1.r!=0.0)
out << c1.r <<" + "<<c1.i<<"i";
else if (c1.i ! = 0.0)
out << c1.i;
else
out << c1.r <<"i";
return out;
}
Bina Nusantara
Operator overloading dengan friend
•
•
Bina Nusantara
Operator overloading yang sering menngunakan friend
adalah << pada cout dan >> pada cin
Dengan overloading ini dimungkinkan menuliskan
statement:
cin >> object;
cout << object;
Operator overloading dengan friend
•
Bentuk umum dari overloading terhadap operator << adalah:
ostream& operator << (ostream& output, object) {
// tubuh fungsi
return (output);
}
•
Bentuk umum dari overloading terhadap operator >> adalah:
istream& operator >> (istream& input, object &) {
// tubuh fungsi
return (input);
}
Bina Nusantara
Contoh Program
// program C++ operator >> dan << dengan friend
#include <constream.h>
#include <string.h>
class Buku {
private:
int Nomor;
char Judul[30];
char Pengarang[10];
public:
Buku();
friend ostream& operator << (ostream& o, Buku b1);
friend istream& operator >> (istream& i, Buku &b1);
};
Bina Nusantara
// definisi fungsi
Buku::Buku() {
Nomor = 0;
strcpy(Judul, "");
strcpy (Pengarang, "");
}
//definisi fungsi friend
ostream& operator<<(ostream& out, Buku b1){
out << "Nomor buku: " << b1.Nomor << endl;
out << "Judul buku: " << b1.Judul << endl;
out << "Pengarang: " << b1.Pengarang << endl;
return (out);
}
istream& operator>> (istream& in, Buku& b1){
cout << "inputkan Nomor buku ";
in >> b1.Nomor;
cout << "inputkan Judul buku ";
Bina Nusantara
in >> b1.Judul;
cout << "inputkan Pengarang ";
in >> b1.Pengarang;
return (in);
}
void main() {
Buku buku_C;
cin >> buku_C;
cout << buku_C;
}
Bina Nusantara
Overloading operator increment dan decrement
•
Operator increment ada dua macam yaitu:
– Pre increment, dengan format:
Nama_kelas operator++();
– Post increment dengan format:
Nama_kelas operator++(int);
•
Operator decrement juga ada dua yaitu:
– Predecrement, dengan format:
Nama_kelas operator--();
– Post decrement, dengan format:
Nama_kelas operator--(int);
Bina Nusantara
Contoh Program
// Program c++ dengan operator decrement &increment
#include <constream.h>
class Date {
private:
int month, day, year;
static const int days[ ] = {0, 31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31 };
void helpIncrement();
public:
Date(int m=1, int d=1, int y=1990);
void setDate(int m, int d, int y);
Date operator++();
Date operator++( int );
const Date operator+= ( int );
int leapYear( int );
int endOfMonth( int );
};
Bina Nusantara
// pre increment
Date Date::operator++() {
helpIncrement();
return (*this)
}
// post increment
Date Date::operator++(int){
Date temp = *this
helpIncrement();
return (*this)
}
void Date::helpIncrement(){
if (endOfMonth( day ) && month == 12) {
day = 1; month = 1;
++year;
}
else if ( endOfMonth(day) ) {
day = 1; ++month;
}
else
++day;
}
Bina Nusantara
int Date::endOfMonth( int d) {
if ( month ==2 && leapYear( year ) )
return (d == 29);
else
return (d == days[ moth ]);
}
int Date::leapYear( int yr){
if ( (yr%40 == 0) || (yr%100 != 0 && yr%4 == 0) )
return 1;
else
return 0;
}
const Date operator+= ( int addays) {
for (int I=0; I < addays; I++)
helpIncrement();
return *this;
}
Bina Nusantara
Diskusi dan Tanya Jawab
Latihan soal
24
Bina Nusantara
© Copyright 2026 Paperzz