1 Pertemuan 16 Multiple Inheritance Matakuliah: T0456 ~ Algoritma dan Metode Object Oriented Programming Tahun: 2005 Versi: 5
2 Learning Outcomes Pada akhir pertemuan ini, diharapkan: Mahasiswa dapat Menghasilkan sebuah class hasil turunan (multiple inheritance) dari beberapa base class
3 Outline Materi Pengertian dari multple inheritance Fungsi anggota kelas turunan berganda Hirarkti class multiple inheritance Kerancuan dari multiple inheritance
4 Multiple Inheritance Pewarisan ganda (Multiple Inheritance), penurunan suatu class (derived class) dari beberapa class utama (based class) Code: class turunan: public utama1, private utama2 { -- class content -- };
5 Hirarki Multiple Inheritance BapakIbu Anak
6 Multiple Inheritance BC A Int data1 Method1 Method2 Method3 Method4 Method5 Int data1 Method1 Method2 Method3 Method4 Method5
7 Multiple Inheritance B C A Int data1 Method1 Method2 Method3 Method4 Method5
8 Contoh Program //Program C++ - pewarisan ganda #include const int LEN = 80; class student{ private : char school[LEN]; char degree[LEN]; public : void getedu(){ cout << “ \nEnter name of school or university : “; cin >> school; cout << “\nEnter highest degree earned\n”; cout << “(Highschool, Bachelor’s, Master’s, PhD): “; cin >> degree; }
9 void putedu(){ cout << “\nSchool or Unversity: “ << school; cout << “Highest degree earned: “ << degree; } }; class employee{ private: char name[LEN]; unsigned long number; public: void getdata(){ cout << “\nEnter Last name: “; cin >> name; cout << “\nEnter number: “; cin >> number; }
10 void putdata(){ cout << “\n Name: “ << name; cout << “\n Number: “ << number; } }; class manager : private employee, private student{ private: char title[LEN]; double dues; public: void getdata(){ employee :: getdata(); cout > title; cout << “ \nEnter golf club dues : “ ; cin >> dues; student :: getedu(); }
11 void putdata (){ employee :: putdata(); cout << “ \nTitle : “; << title; cout << “ \nGolf club dues: “ << dues; student :: putedu(); } }; class scientist : public employee, public student{ private: int pubs; public: void getdata(){ employee::getdata(); cout > pubs; student :: getedu(); }
12 void putdata(){ employee :: putdata(); cout << “ \n# publications: “; << pubs; student :: putedu(); } }; class laborer : public employee{}; void main(){ manager m1, m2; scientist s1; laborer l1; cout << “\nEnter data for manager 1”; m1.getdata(); cout << “\nEnter data for manager 2”; m2.getdata(); cout << “\nEnter data for scientist 1”;
13 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(); }
14 Contoh Program // Program C++ pewarisan berganda #include class KelasA { public: void info(){ cout << “Informasi kelas A” << endl; } }; Kerancuan pada pewarisan berganda
15 class KelasB { public: void info(){ cout << “Informasi kelas B” << endl; } }; class KelasC : public KelasA, public KelasB { // tidak terdapat anggota kelas }; void main(){ KelasC Anak; Anak.info(); // menimbulkan kerancuan // tidak menimbulkan kerancuan Anak.KelasA::info() Anak.KelasB::info() }
16 Penutup Untuk dapat Menghasilkan sebuah class hasil turunan (multiple inheritance) dari beberapa base class, mahasiswa membahas tugas pertemuan 16 no 1, 2, dan 3. (diskusikan dalam kelompok)