1 / 16

Inheritance lanjutan Pertemuan 8

Inheritance lanjutan Pertemuan 8. Matakuliah : 0454T / Algoritma dan Metode Object Oriented Programming Tahun : 2009. Learning Outcomes. Pada akhir pertemuan ini, diharapkan: Mahasiswa dapat menghasilkan program yang menggunakan konsep inheritance. Buku Referensi:

gala
Download Presentation

Inheritance lanjutan Pertemuan 8

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. Inheritance lanjutanPertemuan 8 Matakuliah : 0454T / Algoritma dan Metode Object Oriented Programming Tahun : 2009

  2. Learning Outcomes Pada akhir pertemuan ini, diharapkan: Mahasiswa dapat menghasilkan program yang menggunakan konsep inheritance. Buku Referensi: • C++ - How to program, Deitel & Deitel, Prentice Hall, 2001. Websites: • http://www.deitel.com 3

  3. Outline Materi • Constructor dan Destructor pada inheritance • Modifikasi class turunan • Hirarki class inheritance • Contoh program 4

  4. Constructor dan Destructor dalam Inheritance • Constructor based class dijalankan terlebih dahulu, kemudian constructor derived class. • Destructor derived class dijalankan terlebih dahulu dengan urutan berbalikan dengan urutan constructor. • Urutan juga berlaku untuk multiple inheritance. • Jika konstruktor based class memiliki parameter, maka parameter constructor based class tersebut harus dimasukkan dalam constructor derived class. • Jika parameter constructor derived class ada yang tidak sama dengan constructor based class, maka constructor based class yang akan dijalankan adalah constructor yang memiliki jumlah dan parameter yang paling mendekati.

  5. Constructor dan Destructor dalam Inheritance Example class Based1{ Base1(){ cout<<“1”; }; ~Base1(){ cout<<“A”; }; } class Based2:public Based1{ Base2(){ cout<<“2”; }; ~Base2(){ cout<<“B”; }; } class Derived:public Based2 { Derived(){ cout<<“3”; }; ~Derived(){ cout<<“C”; }; } Void main(){ Derived MyObj; } Menghasilkan keluaran: 123CBA

  6. Modifikasi Fungsi ClassTurunan Class turunan (derived class) memiliki member functions yang sama dengan klas utama (base class) Member functions tersebut dapat dimodifikasi disesuaikan kebutuhan kelas tersebut

  7. Contoh Program Contoh Program // Program C++ -Overload functions Klas turunan #include <iostream.h> #include <process.h> const int MAX =100; class Stack { protected : int st[MAX}; int top; public : Stack() { top = o ; } void push( int var ) { st[ ++top ] = vbar ;} int pop() { return st[ top -- ] ; } };

  8. Contoh Program class Stack 2 : public Stack { public : void push( int var ) { if ( top < MAX ) Stack :: push( var ); else { cout << “ \nError : stack is full ”; exit(1); } } int pop() { if (top > 0 ) return(Stack :: pop()); else { cout<< “\nError : stack is empty ” ; exit(1); } } };

  9. Contoh Program void main() { Stack2 s1; s1.push(11); s1.push(22); s1.push(33); cout << endl << s1.pop(); cout << endi << s1.pop(); cout << endl << s1.pop(); cout << endi << s1.pop(); }

  10. Employee Laborer Scientist Manager Name Number Pubs Title Dues GetData() PutData() GetData() PutData() GetData() PutData() Hirarki Klas Satu klas utama dapat memiliki beberapa klas turunan yang masing-masing berbeda.

  11. Contoh Program #include <iostream.h> const int LEN = 80; class employee { private : char name[LEN]; unsigned long number; public : void getdata() { cout << “ \nEnter Last Name : “ ; cin >> name; cout << “\nEnter number : “ ; cin >> number; } void putdata() { cout << “ \n Name : “ << name; cout << “ \n Number : “ << number; } };

  12. Contoh Program class manager : public employee { private : char title[LEN]; double dues; public : void getdata() { employee :: getdata(); cout << “ \nEnter Title : “; cin >> title; cout << “ \n Golf club dues : “ << dues; cin >> dues; } void putdata() { employee :: putdata(); cout << “ \nTitle : “; << dues; } };

  13. Contoh Program class scientist : public employee { private : int pubs; public : void getdata() { employee :: getdata(); cout << “\nEnter # pubs: “; cin >> pubs; } void putdata() { employee :: putdata(); cout << “ \n# publications : “; << pubs; } }; class laborer : public employee{ };

  14. Contoh Program void main() { manager m1, m2; scientist s1; laborer l1; cout << “ \nEnter data for manager 1 ” ; m1.getdata(); cout << “ \nEnter data for manager 2 ” ; m2.getdat(); cout << “ \nEnter data for scientist 1 ” ; s1.getdata(); cout << “ \nEnter data for laborer 1 ” ; l1.getdata(); cout << “\nData on manager 1’; m1.putdata(); cout << “\nData on manager 2”; m2.putdata(); cout << “\nData on scientist 1”; s1.putdata(); cout << “\nData on laborer 1”; l1.putdata(); }

  15. Diskusi dan Tanya JawabLatihan soal 16

More Related