# Operator Overloading Pertemuan 20 Matakuliah: T0456 / Algoritma dan Metode Object Oriented Programming Tahun: 2007.

## Presentasi berjudul: "Operator Overloading Pertemuan 20 Matakuliah: T0456 / Algoritma dan Metode Object Oriented Programming Tahun: 2007."— Transcript presentasi:

Operator Overloading Pertemuan 20 Matakuliah: T0456 / Algoritma dan Metode Object Oriented Programming Tahun: 2007

Bina Nusantara 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 Operator yang dapat dioverloading +-*/%^& |~!=><+= -=*=/=%=^=&=|= >=<=&&||==!=<<= >>=<<>>++--->*, ->[]()newNew[]deleteDelete[]

Bina Nusantara OperatorKegunaanContoh.Operator anggota struct atau classCin.getline().*Operator pointer ke anggotaObject.*anggota : Operator lingkupClass::anggota ? :Ungkapan kondisiC=(a>b)?a:b Operator yang tidak dapat dioverloading

Bina Nusantara 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);

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

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 > 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/3721672/slides/slide_14.jpg", "name": "Bina Nusantara 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 << + <

Bina Nusantara 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); };

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

Bina Nusantara 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 ); };

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 24 Diskusi dan Tanya Jawab Latihan soal

Presentasi serupa