Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance (2).

Similar presentations


Presentation on theme: "Inheritance (2)."— Presentation transcript:

1 Inheritance (2)

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

3 #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(); }

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

5 #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)."

Similar presentations


Ads by Google