1 / 20

Operator overloading

Operator overloading. Konsep overloading terhadap operator didasarkan pada operasi yang terjadi sehari-hari misalkan operator + biasa digunakan untuk menjumlahkan bilangan bulat ataupun bilangan pecahan . 4 + 5 3.56 + 3.08

lucky
Download Presentation

Operator overloading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operator overloading

  2. Konsep overloading terhadap operator didasarkanpadaoperasi yang terjadisehari-harimisalkan operator + biasadigunakanuntukmenjumlahkanbilanganbulatataupunbilanganpecahan. 4 + 5 3.56 + 3.08 Bagaimanajika operator + digunakanuntukpenjumlahanbilangankompleks?

  3. Untukmenjumlahkanbilangankompleks, perlusedikitmodifikasidalampenggunaan operator + • Penambahanbilangankompleksmelibatkanpenambahansisi real danimajiner (3 - 5i) + (6 + 7i) = (3 + 6) + (-5 + 7)i = 9 + 2i

  4. Operator overloading artinyamelakukanmodifikasi / overloading pada operator (+, -, *, dsb) sesuaikebutuhan Syntax operator overloading: Tipeoperator simbol_operator ( argument )

  5. Operator yang dapatdi-overloading

  6. Operator yang tidakdapatdi-overloading

  7. Overloading operator biner Operator binermerupakan operator dengandua operand, contoh operator+. Bentukumum overloading operator bineradalah: TipeDataoperator simbol_operator (object )

  8. #include <iostream> using namespace std; class complex { float riil; float imajiner; public: complex() { riil=0; imajiner=0; }; complex(float real, float imaginer) { riil=real; imajiner=imaginer; }; void display(void) { cout<<"["<<riil<<"]+["<<imajiner<<"i]\n"; }; complex operator +(complex c) { complex temp; temp.riil = riil+c.riil; temp.imajiner=imajiner+c.imajiner; return(temp); }; }; main() { complex c1(2.5, 6.1); complex c2(3.3, 4.5); complex c3; c3=c1+c2; cout<<"\nC1="; c1.display(); cout<<"\nC2="; c2.display(); cout<<"\nC3="; c3.display(); system("pause"); }

  9. Lengkapi program diatasuntukoperasiberikut • Addition (a + bi) + (c + di) = (a + c) + (b + d)i. • Substraction (a + bi) - (c + di) = (a - c) + (b - d)i. • Multiplication (a + bi) * (c + di) = (ac - bd) + (ad + bc)i. c* (a + bi) = c*a + c*bi • Addition dengan bilangan integer (a + bi) + x= (a+x) + bi

  10. Lengkapi program diatasuntukoperasi berikut • Increment (a + bi)++ = (a ++) + (b++)i • Operator << (operator keluaransetelahcout) tampil : Riil = a, Imajiner=b; • Operator >> (operator masukansetelahcin) tampil : Masukkan bilangan riil : Masukkan bilangan imajiner :

  11. Operator increment adaduamacamyaitu: Pre increment, dengan format: Nama_kelasoperator++(); Post increment dengan format: Nama_kelasoperator++(int); Operator decrement jugaadaduayaitu: Predecrement, dengan format: Nama_kelasoperator--(); Post decrement, dengan format: Nama_kelasoperator--(int); Overloading operator increment dan decrement

  12. Contoh Program // Program c++dengan operator decrement &increment #include <constream.h> class Date { private: intmonth, day, year; static const intdays[ ] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; void helpIncrement(); public: Date(intm=1, intd=1, inty=1990); void setDate(intm, intd, inty); Date operator++(); Date operator++( int); const Date operator+= ( int); intleapYear( int); intendOfMonth( int); };

  13. // 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; }

  14. else if ( endOfMonth(day) ) { day = 1; ++month; } else ++day; } intDate::endOfMonth( intd) { if ( month ==2 && leapYear( year ) ) return (d == 29); else return (d == days[ moth ]); } intDate::leapYear( intyr){ if ( (yr%40 == 0) || (yr%100 != 0 && yr%4 == 0) ) return 1; else

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

  16. Operator overloading yang seringmenggunakanfriend adalah << padacoutdan >> padacin Dengan overloading inidimungkinkanmenuliskan statement: cin>> object; cout<< object; Operator overloading dengan friend

  17. Bentukumumdari overloading terhadap operator << adalah: ostream& operator << (ostream& output, object) { // tubuhfungsi return (output); } Bentukumumdari overloading terhadap operator >> adalah: istream& operator >> (istream& input, object &) { // tubuhfungsi return (input); } Operator overloading dengan friend

  18. Contoh Program // program C++ operator >> dan << dengan friend #include <constream.h> #include <string.h> class Buku { private: intNomor; char Judul[30]; char Pengarang[10]; public: Buku(); friend ostream& operator << (ostream& o, Buku b1); friend istream& operator >> (istream& i, Buku &b1); };

  19. // definisifungsi Buku::Buku() { Nomor = 0; strcpy(Judul, ""); strcpy(Pengarang, ""); } //definisifungsi friend ostream& operator<<(ostream& out, Buku b1){ out << "Nomorbuku: " << b1.Nomor << endl; out << "Judulbuku: " << b1.Judul << endl; out << "Pengarang: " << b1.Pengarang << endl; return (out); } istream& operator>> (istream& in, Buku& b1){ cout<< "inputkanNomorbuku "; in >> b1.Nomor; cout<< "inputkanJudulbuku ";

  20. in >> b1.Judul; cout<< "inputkanPengarang "; in >> b1.Pengarang; return (in); } void main() { Bukubuku_C; cin >> buku_C; cout << buku_C; }

More Related