Document 15057516

advertisement
Course Name: High Level Programming Language
Year
: 2010
Inheritance
Lecture 9
Learning Outcomes
At the end of this lecture, students are capable of:
Understanding the concept of Inheritance in C++
programming.
3
Outline Materi
•
•
•
•
Introduction to the conceptual of Inheritance
Declaration of Derived Class
Types access of Derived Class
Single Inheritance
4
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.
5
Forms of Class Hierarcy
Single Inheritance
Multiple Inheritance
6
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.
7
C++ Syntax for Inheritance
• class Nama_Turunan : jenis_akses Nama_Induk
• { <Anggota_klas> }
•
•
•
•
•
•
•
Contoh :
class Barang
{ protected: int A;
Nama_Turunan
};
class Meja : public Barang
Jenis_akses
{ protected: int B;
8
};
Example: Base Class
class base {
int x;
public:
void setx(int n) { x = n; }
void showx() { cout << x << ‘\n’ }
};
Bina Nusantara University
9
Example: Derived Class
// Inherit as public
class derived : public base {
int y;
public:
void sety(int n) { y = n; }
void showy() { cout << y << ‘\n’;}
};
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.
11
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.
12
#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 );
}
Program1 - Single
Inheritance
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();
}
13
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;
}
};
14
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 data
cout << “\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 data
cout << “\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();
}
15
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.
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.
Example: Constructor of base
class base {
int i;
public:
base(int n) {
cout << “constructing base \n”;
i = n; }
~base() { cout << “destructing base \n”; }
};
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”;}
};
Example: main()
int main() {
derived o(10,20);
return 0;
}
constructing base
constructing derived
destructing derived
destructing base
Multiple Inheritance
• Type 1:
base
1
derived
1
derived
2
Multiple Inheritance
• Type 2:
base
1
base
2
derived
Example: Type 2
// Create first base class
class B1 {
int a;
public:
B1(int x) { a = x; }
int geta() { return a; }
};
Example: Type 2
// Create second base class
class B2 {
int b;
public:
B2(int x) { b = x; }
int getb() { return b; }
};
Example: Type 2
// 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;}
};
Potential Problem
Base
Base
Derived
2
Derived
1
Derived
3
• Base is inherited twice by Derived 3!
Virtual Base Class
• To resolve this problem, virtual base class can be used.
class base {
public:
int i;
};
Virtual Base Class
// Inherit base as virtual
class D1 : virtual public base {
public:
int j;
};
class D2 : virtual public base {
public:
int k;
};
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; }
};
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.
Example
base *p;
// base class pointer
base B_obj;
derived D_obj;
p = &B_obj; // p can point to base object
p = &D_obj; // p can also point to derived
object
//
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
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?
33
Download