Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Inheritance (2).

Presentasi serupa


Presentasi berjudul: "Inheritance (2)."— Transcript presentasi:

1 Inheritance (2)

2 --relaksasi--

3 Siapkan… Alat tulis (pulpen, pensil, dll)
Media Tulis (kertas, buku, laptop, HP, dll) Siap-siap untuk menulis…

4 Tulislah:

5 (1) Nama Hewan yang paling anda suka,
beserta sifat-sifatnya (minimal 5 sifat), [alasan anda menyukai hewan itu]

6 (2) Nama Hewan yang juga anda suka,
beserta sifat-sifatnya (minimal 5 sifat), [alasan anda menyukai hewan itu]

7 (3) Nama Hewan yang anda suka juga,
beserta sifat-sifatnya (minimal 5 sifat), [alasan anda menyukai hewan itu]

8

9 Hybrid Inheritance Apply two or more types of inheritance. Mahasiswa
Ujian Tulis Ujian OR Hasil

10 #include “iostream.h” // Hybrid Inheritance
class student { protected : int roll_number; public : void get_number(int a) { roll_number = a; }; void put_number(void) { cout << “Roll Number = “ << roll_number << “\n”; }; }; class test : public student { protected : float sub1; float sub2; public : void get_marks(float x, float y) { sub1=x; sub2=y; }; void put_marks(void) { cout << “Sub1 = “ << sub1 << “\nSub2 = “ << sub2; class result : public test { public : void display(void) { put_number(); put_marks(); cout << “Total = “ << sub1 + sub2 << “\n”; } void main() { result student; student.get_number(123); student.get_marks(85.6, 77.9); student.display(); }

11 Constructors in Derived Classes
As long as no base class constructor takes any arguments, the derived class need not have a constructor function. But, if any base class contains a constructor with one or more arguments, then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructors. When both the derived class and base classes contain constructors, the base constructor is executed first and then the constructor in the derived class is executed. The constructors will be executed in the order in which they appear in the declaration of the derived class or in the order of inheritance.

12 #include "iostream.h" class a { public: a() {cout<<"menjalankan constructor a default\n";}; a(int i) {cout<<"constructor a 1 parameter " <<i<<endl;}; }; class c c() {cout<<"menjalankan constructor c default \n";}; c(int i) {cout<<"constructor c 1 parameter " <<i<<endl;}; class b : public a, public c b(int j, int k) : a(j), c(k) {cout<<"constructor b\n\n";}; b() : a(100), c(200) {cout<<"constructor b default\n\n";}; ~b() {cout<<"desrtuctor b\n";} void main() { b bebek(10,20); b baba; }


Download ppt "Inheritance (2)."

Presentasi serupa


Iklan oleh Google