# 1 Pertemuan 14 Operator Overloading Matakuliah: T0456 ~ Algoritma dan Metode Object Oriented Programming Tahun: 2005 Versi: 5.

## Presentasi berjudul: "1 Pertemuan 14 Operator Overloading Matakuliah: T0456 ~ Algoritma dan Metode Object Oriented Programming Tahun: 2005 Versi: 5."— Transcript presentasi:

1 Pertemuan 14 Operator Overloading Matakuliah: T0456 ~ Algoritma dan Metode Object Oriented Programming Tahun: 2005 Versi: 5

2 Learning Outcomes Pada akhir pertemuan ini, diharapkan : Mahasiswa dapat Mendemonstrasikan operator overloading sebagai pengganti member function

7 Operato r KegunaanContoh. Operator anggota struct atau class Cin.getline().* Operator pointer ke anggotaObject.*anggot a : Operator lingkupClass::anggota ? : Ungkapan kondisiC=(a>b)?a:b Operator yang tidak dapat dioverloading

9 Contoh Program // program C++ dengan fungsi friend #include 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);

10 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 > 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;

11 cout << "Bil komplek IV: "; cout << C << endl; C = B*A; cout << "Bil komplek V: "; cout << C << endl; cout << "Bil komplek VI: "; cout << C*2 << endl; cout << "Bil 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); }

12 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); }

13 istream& operator >> (istream& in, Complex& c1) { cout > c1.r; cout > c1.i; return in; } ostream& operator << (ostream& out, Complex c1) { if (c1.i!=0.0 && c1.r!=0.0) out << c1.r <<" + "< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.info/12/3721641/slides/slide_13.jpg", "name": "13 istream& operator >> (istream& in, Complex& c1) { cout > c1.r; cout > c1.i; return in; } ostream& operator << (ostream& out, Complex c1) { if (c1.i!=0.0 && c1.r!=0.0) out << c1.r << + <

16 Contoh Program // program C++ operator >> dan << dengan friend #include 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); };

17 // 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 ";

18 in >> b1.Judul; cout << "inputkan Pengarang "; in >> b1.Pengarang; return (in); } void main() { Buku buku_C; cin >> buku_C; cout << buku_C; }

19 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); Overloading operator increment dan decrement

20 Contoh Program // Program c++ dengan operator decrement &increment #include 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 ); };

21 // 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; }

22 else if ( endOfMonth(day) ) { day = 1; ++month; } else ++day; } 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

23 return 0; } const Date operator+= ( int addays) { for (int I=0; I < addays; I++) helpIncrement(); return *this; }

24 Penutup Untuk dapat menggunakan operator overloading sebagai pengganti member function, mahasiswa membahas tugas pertemuan 14 no 1, 2, dan 3. (diskusikan dalam kelompok)

Presentasi serupa