inheritance lecture 9

33

Upload: deva

Post on 17-Jan-2016

34 views

Category:

Documents


0 download

DESCRIPTION

Inheritance Lecture 9. Course Name: High Level Programming Language Year : 2010. Learning Outcomes. At the end of this lecture, students are capable of: Understanding the concept of Inheritance in C++ programming. Outline Materi. Introduction to the conceptual of Inheritance - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Inheritance Lecture 9
Page 2: Inheritance Lecture 9

InheritanceLecture 9

Course Name: High Level Programming LanguageYear : 2010

Page 3: Inheritance Lecture 9

3

Learning Outcomes

At the end of this lecture, students are capable of:

Understanding the concept of Inheritance in C++ programming.

Page 4: Inheritance Lecture 9

4

Outline Materi• Introduction to the conceptual of Inheritance• Declaration of Derived Class• Types access of Derived Class• Single Inheritance

Page 5: Inheritance Lecture 9

5

Introduction to the conceptual of Inheritance

• The mechanism by which one class can inherit the properties of another.

• It allows a hierarchy of classes to be built, moving from the most general to the most specific.

Page 6: Inheritance Lecture 9

Forms of Class Hierarcy

6

Single Inheritance

Multiple Inheritance

Page 7: Inheritance Lecture 9

7

Class Hierarchy

• In the design of class hierarchy, based class is a class that has the most common properties or the properties that are needed by its derived class.

• While specific behaviors are belonged to its derived classes, where those specific behaviors are used to improve the based class.

Page 8: Inheritance Lecture 9

8

C++ Syntax for Inheritance• class Nama_Turunan : jenis_akses

Nama_Induk• { <Anggota_klas> }

• Contoh :• class Barang • { protected: int A;• };• class Meja : public Barang• { protected: int B;• };

Nama_Turunan

Jenis_akses

Page 9: Inheritance Lecture 9

Example: Base Class

class base {int x;

public:void setx(int n) { x = n; }void showx() { cout << x <<

‘\n’ }};

Bina Nusantara University 9

Page 10: Inheritance Lecture 9

Example: Derived Class

// Inherit as publicclass derived : public base {

int y;public:

void sety(int n) { y = n; }void showy() { cout << y << ‘\

n’;}};

Page 11: Inheritance Lecture 9

11

Access member of Base class from Derived class

• Member of based class can be accessed by its derivative class, if access-member of the based class is public or protected.

• Protected member of based class can be accessed by its derived classes, as it is declared inside its derived classes.

• Protected member from a class can not be accessed by other functions or classes in a program, that are not derived from the based class.

Page 12: Inheritance Lecture 9

12

Access Type of Derived Class• class Nama_Turunan : jenis_akses Nama_Induk

{ <Anggota_klas> }• Access type from the above derived class

can be of type public/protected/private.

Page 13: Inheritance Lecture 9

13

Program1 - Single Inheritance

#include <iostream.h>#include <stdio.h>#include <conio.h>class Mahasiswa{private: char NIM[11], Nama[40];public:void Masuk_Mhs (){cout << “Masukkan NIM : “; cin >> NIM;cout << “Masukkan Nama : “; gets( Nama );}void Tampil_Mhs () {cout << “NIM = “ << NIM << endl; cout << “Nama = “ << Nama < endl;}};class Mata_Kuliah : public Mahasiswa {protected:char Kd_Kuliah[6], Nm_MataKuliah[30];public:void Masuk_Kuliah () {cout << “Masukkan Kode Kuliah : “; cin >>

Kd_Kuliah;cout << “Masukkan Nama Mata Kuliah : “;gets( Nm_MataKuliah );}

void Tampil_Kuliah () {cout << “Kode Kuliah : “ << Kd_Kuliah << endl;cout << “Nama Mata Kuliah : “ << Nm_MataKuliah << endl;}};void main( ){ Mata_Kuliah M1; M1.Masuk_Mhs(); M1.Masuk_Kuliah(); M1.Tampil_Mhs(); M1.Tampil_Kuliah(); getch();}

Page 14: Inheritance Lecture 9

14

Program 2 - single Inheritance• #include <iostream.h>• #include <stdio.h>• #include <conio.h>• class Pegawai• { protected:• char NIP[10], Nama[30];• public: • void Masuk_Data () {• cout << “Masukkan Nomor Induk

Pegawai : “;• cin >> NIP;• cout << “Masukkan Nama Pegawai : “;• cin >> Nama;• }• void Tampil_Data () {• cout << “Nomor Induk Pegawai = “ << NIP• << endl;• cout << “Nama Pegawai = “ << Nama• << endl;• }• };

class Manager : public Pegawai {protected:char Titel[20], Telpon[20];public:void Masuk_Data () {Pegawai :: Masuk_Data();cout << “Masukkan titel dari manager : “;cin >> Titel;cout<< “Masukkan No Telpon : “;cin >> Telpon;}void Tampil_Data () {Pegawai :: Tampil_Data();cout << “Titel dari manager : “ << Titel<< endl;cout << “No Telpon manager : “ << Telpon<< endl;}};

Page 15: Inheritance Lecture 9

15

Program2 (Cont.)• class Peneliti : public Pegawai• {protected:• int Publikasi;• public:• void Masuk_Data () {• Pegawai :: Masuk_Data();• cout << “Masukkan No Publikasi : “;• cin >> Publikasi;• }• void Tampil_Data () {• Pegawai :: Tampil_Data();• cout << “No Publikasi Peneliti : “ << Publikasi• << endl;• }• };

• class Satpam : public Pegawai• { };

• void main () • { Manager m1, m2;• Peneliti p1;• Satpam s1;

// Bagian Entry datacout << “\nMasukkan data untuk manager 1 : “;m1.Masuk_Data();cout << “\nMasukkan data untuk manager 2 : “;m2.Masuk_Data();cout << “\nMasukkan data untuk peneliti 1 : “;p1.Masuk_Data();cout << “\nMasukkan data untuk satpam 1 : “;s1.Masuk_Data();

// Bagian Display datacout << “\nData dari manager 1 : “;m1.Tampil_Data();cout << “\nData dari manager 2 : “;m2.Tampil_Data();cout << “\nData dari peneliti 1 : “;p1.Tampil_Data();cout << “\nData dari satpam 1 : “;s1.Tampil_Data(); getch();}

Page 16: Inheritance Lecture 9

Constructor and Destructor

• It is possible for both the base class and the derived class to have constructor and/or destructor functions.

• The constructor functions are executed in order of derivation.– i.e.the base class constructor is executed first.

• The destructor functions are executed in reverse order.

Page 17: Inheritance Lecture 9

Passing arguments

What if the constructor functions of both the base class and derived class take arguments?

1. Pass all necessary arguments to the derived class’s constructor.

2. Then pass the appropriate arguments along to the base class.

Page 18: Inheritance Lecture 9

Example: Constructor of baseclass base {

int i;public:

base(int n) {cout << “constructing base \n”; i = n; }

~base() { cout << “destructing base \n”; }};

Page 19: Inheritance Lecture 9

Example: Constructor of derived

class derived : public base {int j;

public:derived (int n, int m) : base (m) {

cout << “constructing derived\n”;j = n; }

~derived() { cout << “destructing derived\n”;}

};

Page 20: Inheritance Lecture 9

Example: main()

int main() {derived o(10,20);return 0;

}constructing base

constructing derived

destructing derived

destructing base

Page 21: Inheritance Lecture 9

Multiple Inheritance• Type 1:

base 1

derived 1

derived 2

Page 22: Inheritance Lecture 9

Multiple Inheritance

• Type 2:

base 1

base 2

derived

Page 23: Inheritance Lecture 9

Example: Type 2

// Create first base classclass B1 {

int a;public:

B1(int x) { a = x; }int geta() { return a; }

};

Page 24: Inheritance Lecture 9

Example: Type 2

// Create second base classclass B2 {

int b;public:

B2(int x) { b = x; }int getb() { return b; }

};

Page 25: Inheritance Lecture 9

// Directly inherit two base classes.class D : public B1, public B2 {

int c;public:

D(int x, int y, int z) : B1(z), B2(y) {c = x; }

void show() {cout << geta() << getb() << c;}

} ;

Example: Type 2

Page 26: Inheritance Lecture 9

Potential Problem

• Base is inherited twice by Derived 3!

Base Base

Derived 1

Derived 2

Derived 3

Page 27: Inheritance Lecture 9

Virtual Base Class• To resolve this problem, virtual base class can be

used.

class base {public:

int i;};

Page 28: Inheritance Lecture 9

Virtual Base Class

// Inherit base as virtualclass D1 : virtual public base {public:

int j; };class D2 : virtual public base {public:

int k; };

Page 29: Inheritance Lecture 9

Virtual Base Class

/* Here, D3 inherits both D1 and D2. However, only one copy of base is present */

class D3 : public D1, public D2 {public:

int product () { return i * j * k; }};

Page 30: Inheritance Lecture 9

Pointers to Derived Classes• A pointer declared as a pointer to base class can

also be used to point to any class derived from that base.

• However, only those members of the derived object that were inherited from the base can be accessed.

Page 31: Inheritance Lecture 9

Example

base *p; // base class pointerbase B_obj;derived D_obj;

p = &B_obj; // p can point to base object p = &D_obj; // p can also point to derived

// object

Page 32: Inheritance Lecture 9

Conclusions• Inheritance promotes the reusability of coding.• Member-access specifier relates base class with

its derived class.• The constructor functions are executed in order of

derivation.• Virtual Base Class is a way to solve problem of

multiple base class being derived in multiple inheritance.

Bina Nusantara University 32

Page 33: Inheritance Lecture 9

33

Topic For Next Week• Polymorphism• Assignment:

In order to answer the following questions below, please read C++ Begineer’s Guide CH10.pdf. Questions are taken from page 37!

– What is a virtual function?– Why are virtual functions important?– When an overridden virtual function is called through a

base class pointer, which version of the function is executed?