0% found this document useful (0 votes)
347 views19 pages

F3031 Object Oriented Programming: Introduction To OOP Classes and Objects Exception Handling

oop
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
347 views19 pages

F3031 Object Oriented Programming: Introduction To OOP Classes and Objects Exception Handling

oop
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1

F3031 OBJECT ORIENTED PROGRAMMING

F3031 Object Oriented Programming

1.0 ●
Introduction To OOP

2.0 ●
Classes And Objects

3.0 Inheritance and Polymorphism


4.0 ●
Exception handling
© 2009 | PN NORHASLIZA BT MUHAMAD NOR
2

1.0INTRODUCTION TO
OBJECT – ORIENTED
PROGRAMMING (OOP)

Understand the concepts of © 2009 | PN NORHASLIZA BT MUHAMAD NOR


Object-oriented Programming
3

F3031 OBJECT ORIENTED PROGRAMMING

LEARNING OUTCOMES
1 Define OOP

2 History Of OOP

3 Advantages Of Using OOP


4 Basic Terminologies Of OOP


5 Abstraction & Encapsulation


6 Structured & OOP Approach


© 2009 | PN NORHASLIZA BT MUHAMAD NOR


4

F3031 OBJECT ORIENTED PROGRAMMING

Define OOP
• Programming paradigm that uses “objects" – data structures consisting of datafields
and methods – and their interactions to design applications and computer programs.

• A type of programming where the programmer can define data types and types of
operations, which will be performed on the data structure.

Example:
Data data types
Data Structure Student No. Integer
Mark float
 
Types of operation:
Student No. :
Sort student list based on Student No.
Mark :
Calculate mark.

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


5

F3031 OBJECT ORIENTED PROGRAMMING


Cont…..
• By using this method, data structure will become an object such as Student object,
which consists of data and functions.

• The programmer can create relationship between one object with another object such
as an object inherits characteristics from another object

• Object-oriented programming is a method used to write programs where data and


behaviours are packaged together in one class. Object is a member of a class.

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


6

F3031 OBJECT ORIENTED PROGRAMMING

History Of OOP
• ASSGMNT 1

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


7

F3031 OBJECT ORIENTED PROGRAMMING

Advantages Of Using OOP

• Code extensibility / kod boleh ditambah


 Ability to add codes, which are already existing and consistent with needs.

• Code reusability / kod boleh diguna berulangkali


 Existing code can be tested first before it is inherited from previous module.
 This will save cost and time needed to develop software.
 With this method, it will help to increase programmer’s productivity.

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


8

F3031 OBJECT ORIENTED PROGRAMMING


Cont…..

• Represent real world / menggambarkan dunia sebenar


 Using object-oriented concept, it will assist the programmer in creating a module because
data used represents real situations.

• Data security / keselamatan data


 The use of encapsulation concept has made data to be protected from misuse by an
unauthorized person.

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


9

F3031 OBJECT ORIENTED PROGRAMMING

Terminologies Of OOP

1. Object
 Object is the term used to explain many things.
 Example: student, chair and circle.
 An Object consists of data and method
 Properties of an object are called data. In the real world, characteristics of an object can be
divided into two types:
 Data that can be seen such as a human with two hands.
 Data that cannot be seen such as a human with a name.
 Method is a set of function that manipulates data, such as method DetermineStatus() can
determine exam result for object student.
Objek : Pelajar
Data : nama, alamat, nokadpengenalan, sid,
markah, status
Method: MenentukanStatus()

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


10

F3031 OBJECT ORIENTED PROGRAMMING


Cont…..
2. Class
 A set of objects that have similar attributes(characteristics, which can be seen.) and methods.
 Attributes and methods of a class can be used by each object from that class.

class Student class Box


{ String name, address, status; { double width, height, depth; data
int icno, sid; data
double marks; double ComputeVolume()
method { return( width * height * depth ); }
char DetermineStatus()
{ if marks >= 40 method
double ComputeArea()
status = “Pass”; { return( width * height ); }
else }
status = “Fail”; Kelas
}
} Kelas

Contoh 2 : Mendefinisikan Kelas Kotak


Contoh 1 : Mendefinisikan Kelas Pelajar

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


11

F3031 OBJECT ORIENTED PROGRAMMING


Cont…..
3. Encapsulation
 Encapsulation is a process of tying together all data and methods that form a class and
control the access to data by hiding its information.
 It enables access to object just by using methods of that object.
 It is one of the security features in object-oriented programming (OOP).

 Attributes and methods of a class can be used by each object from that class.

Class Student
Name, Student ID, Address,
IC No

Calculate_result()

Determine_grade()

Print_result()

Figure above Explains the concept of encapsulation in OOP for class Student

Based from the example given, data and methods are combined in one class. If the college management wants to
get the status whether a student “pass” or “fail”, they only have to know the status without knowing how to
determine or calculate the grade. So, this is a way of implementing encapsulation where the code in the program
is hidden thus to prevent from being modified.

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


12

F3031 OBJECT ORIENTED PROGRAMMING


Cont…..
4. Data Abstraction
 Data abstraction is a process to delete all unnecessary attributes and remain the necessary
attributes to describe an object
 Attributes and methods of a class can be used by each object from that class.

Objek Pelajar Kelas Pelajar

Sifat/(Attribute)
Pengabstrakan
Nama, ID pelajar,
Alamat, No KP

Kelakuan/ (Behaviors)

Kira_markah (),
Tentu_gred (),
Cetak_keputusan ()

Rajah Menerangkan mengenai konsep


pengabstrakan

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


13

F3031 OBJECT ORIENTED PROGRAMMING


Cont…..
5. Inheritance
 Create a new class from an existing class together with new attributes and behaviours.
 The new class will have the same ability as the base class.
 Use the concept of code reusability.

Class A Kelas asas (base class)


kepada B

Kelas terbitan (derived class)


Class B kepada A
Kelas asas kepada C, D and E

Class D Class E
Class C

Kelas terbitan kepada B

Rajah : Hubungan di antara satu kelas dengan


kelas lain. Kelas terbitan boleh
mewarisi ciri-ciri Kelas asas.

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


14

F3031 OBJECT ORIENTED PROGRAMMING


Cont…..
6. Polymorphism
 Polymorphism is a process of giving the same message to two or more different objects and
produce different behaviours depending on how the objects receive the message.

 It is said to be “One interface, many methods”.

Contoh 1:
Mesej: Keluarkan wang anda dari bank:

Objek Tindakan
Pelajar 1 : Menggunakan mesin ATM yang disediakan oleh pihak
bank di mana akaun dibuka

Pelajar 2 : Menggunakan mesin ATM (MEPS) dari bank lain

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


15

F3031 OBJECT ORIENTED PROGRAMMING

Differences Between Abstraction &


Encapsulation
• ASSGMT 1

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


16

F3031 OBJECT ORIENTED PROGRAMMING

Differences Between Structured


& OOP Approach
Pengaturcaraan Berstruktur Pengaturcaraan Berorientasikan Objek

Berasaskan Fungsi Yang didefinisikan Teknik Pengaturcaraan Berasaskan Objek


1
Pengaturcara
Penggunaan konsep pengkapsulan yang
Memecahkan satu program yang besar pada
menggabungkan data dan fungsi dalam
beberapa fungsi. Setiap fungsi akan
satu komponen (kelas)
melaksanakan tugas yang lebih spesifik
Contoh:
Contoh: class Individu{
void main( ){
……….. // data
………..
void DataPeribadi( )
Peribadi (nama,umur);
};
}
main( ){
void Peribadi(char *a, int b)
Individu a;
{ //Definisi fungsi
/*Objek a capai data dan method
DataPeribadi }
dari
kelas Individu*/
}
© 2009 | PN NORHASLIZA BT MUHAMAD NOR
17

F3031 OBJECT ORIENTED PROGRAMMING


Cont…..
Kod aturcara tidak boleh digunakan Membenarkan penggunaan kod yang
2
berulangkali berulang kali

Setiap fungsi ditugaskan untuk satu tugas Ia boleh dilakukan melalui teknik pewarisan.
yang spesifik. Kita mesti menerima Teknik ini membolehkan objek untuk mewarisi
fungsi itu sebagaimana ia ditulis. ciri-ciri (fungsi dan data) objek lain.
Untuk mengubahsuainya, kod itu  
haruslah disalin semula dan diubah untuk Contoh:
memenuhi keperluan. Kita menggunakan radio kegunaan rumah
Contoh: yang telah dicipta untuk digunakan pada
Kita telah merekacipta satu radio untuk kereta dan di pantai
kegunaan rumah.
Teknologi yang digunakan itu boleh
digunakan untuk mencipta radio kereta
ataupun radio yang digunakan di tepi
pantai.

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


18

F3031 OBJECT ORIENTED PROGRAMMING


Cont…..
Fungsi Memanipulasikan Data Menggunakan Pengkapsulan Untuk Bertindak
3
Data (cth: nama, umur) akan dihantar ke Ke atas Data
fungsi-fungsi tertentu (cth: cetak, papar, Pengkapsulan digunakan untuk mempakej
tambah, dan padam) untuk data bersama fungsi yang akan bertindak ke
dimanipulasikan. Fungsi yang akan atas data. Ia mengenalpasti fungsi yang akan
memanipulasikan data tidak ditetapkan dilaksanakan ke atas setiap objek. Kelas
terlebih dahulu. akan mengawal sebarang operasi keatas data
dan fungsi yang berada didalamnya.

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


19

F3031 OBJECT ORIENTED PROGRAMMING


Cont…..
4 Tiada Kawalan Capaian Data Kekangan Dalam Mencapai Data
Fungsi main ( ) boleh capai semua data Wujudnya kawalan capaian terhadap data di
dan fungsi yang terdapat pada program dalam kelas. Contohnya katakunci private
dan protected. Untuk mencapai private data
ia mesti dilakukan melalui method ataupun
mekanisma pengkapsulan.
5 Tidak menyokong polimorfisma Penggunaan Polimorfisma (polymorphism)
Setiap data mesti diisytiharkan terlebih Polimorfisma membolehkan satu fungsi
dahulu sebelum operasi ke atasnya dilaksanakan dengan pelbagai kaedah.
dilaksanakan.

© 2009 | PN NORHASLIZA BT MUHAMAD NOR

You might also like