Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
-
Upload
jocansino4496 -
Category
Documents
-
view
214 -
download
0
Transcript of Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
1/40
1
If you prefer to save your printer ink,follow the steps below to print the lecturesin black text on white background.
1. In PowerPoint, select File > Print.
2. In the ettings section, locate an ite!with value "#olor", change it to "Pure$lack and %hile".
&ip to ave Printer Ink &ip to ave Printer Ink
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
2/40
Introduction to
Object-Oriented
Programming
TCP1201 OOPDS 2
Lecture 1Lecture 1
http://fit.mmu.edu.my/
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
3/40
3
&o understand ob'ect ( class &o understand abstraction in #)) &o understand encapsulation in #))
&o categori*e si!ilar ob'ects &o categori*e ob'ects by #o!position &o understand ob'ect behaviors &o construct +- #lass iagra! &o di/erentiate between Procedural
Progra!!ing and 0b'ect0rientedProgra!!ing
-earning 0b'ectives-earning 0b'ectives
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
4/40
What is an “What is an “object object ”?”?
n object is a co!puter representationof realworld person, place, event oranything in the proble! that we aresolving.
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
5/40
5
What is an “What is an “object object ”?”?
n ob'ect consists of34 attributes /data/states/felds/variables 5typically
noun6, e.g. na!e, date, balance, si*e, !ark, etc.
4 behaviors /procedures/methods/operations/unction
s 5typically verb6, e.g. eat, drive, set, get, push, etc.%e refer to a group o similar objects with the
sa!e attributes and behaviors as a class.a!e attribute does not !ean sa!e value for
the attribute, e.g. 2 students have attribute7na!e7, but the na!e of each student can bedi/erent.
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
6/40
Object and ClassObject and Class#lass3 tudent.
ttributes3 na!e, student8I.$ehavior9!ethod3 do8ho!eworks56.
0b'ects of tudent class3 steve, victor.
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
7/40
Object and ClassObject and Classteve and victor share the sa!e attributes 5na!e (
student8id6, but each ob'ect has its own value for theattributes.
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
8/40
8
Object and ClassObject and Class
%e refer to a group o similar objects withthe sa!e attributes and behaviors as aclass.
&hus, we can de:ne a class as3
4 ;a set of objects comprised of thesame attributes and behaviors
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
9/40
9
Object-OrientedObject-Oriented
ProgrammingProgramming &he idea is to design a progra! to solve a proble! by co!bining both the data and
procedures 5!e!ber functions6 that operate on that data into a singe unit called ob'ect.
Popular 00P languages3 #)), =ava, #, ?$, etc.#)) in P12@1 focuses on 00P.
Data
Member
Function
Data
MemberFunction
Data
MemberFunction
Object1 Object3
Object2
Messages
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
10/40
10
Steps to Use OOPSteps to Use OOP
1. Identify the ob'ects in the proble!.2. Identify the data9attributes and
operations9!ethods in each ob'ect.A. eter!ine how the ob'ects interact with
one another 5!essaging6.
Data
Member
Function
Data
Member
Function
Data
Member
Function
Object1 Object3
Object2
Messages
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
11/40
11
Principles o! OOP Principles o! OOP
1. Abstraction 4 identify the properties that arei!portant to the user in the proble!, andcreate representations that are si!ilar to itsoriginal !eaning.
2. Encapsulation 4 co!bine data and itsoperations in a single unit, and hide thei!ple!entation fro! user.
3. Inheritance 4 create new classes fro! existing
classes 5-ecture A6.4. ol!morphism 4 use the sa!e expression to
denote di/erent operations 5-ecture B6.
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
12/40
12
OOP Principle "OOP Principle "
#bstraction#bstraction bstraction is the process of identifying
i!portant logical properties 5ob'ect,attributes, !ethods6 that si!pli:es the!odeling and working of the proble!.
If you are designing a !obile phone,exa!ple properties that are i!portant tothe user are the screen, keypad, +I,
cover, etc., but not the internal workingof the processor, how the screen isactually rendered, etc.
OOP P tt
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
13/40
13
OOP n Pract ce "n ract ce "#bstraction#bstraction
ssu!e that we are developing si!ple syste! to
keep track of sub'ects registered by student. Itwon7t be diCcult to identify the following tudentclass, its attributes and behaviors9!ethods.4 %e keep the class si!ple for the purpose of discussion.
Attributesid
sub'ects
"ethodsshow8sub'ectsregister8sub'ectwithdraw8sub'ect
#lass
tudent
OOP P tt
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
14/40
14
OOP n Pract ce "n ract ce "#bstraction#bstraction
$ased on what we collected and consider appropriate data type, the following class de:nition can be declared3
&he next step is to consider encapsulation 4 the 2nd 00P principle.class Student { int id;
vector subjects; void show_subjects(); void register_subject(); void withdraw_subject();};
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
15/40
15
OOP Principle "OOP Principle "
$ncapsulation$ncapsulationDncapsulation is the idea that the internal
workings of an ob'ect can be hidden fro!the outside world.
Dncapsulation is perfor!ed in 2 ways3
–
%e encapsulate how attributes areaccessed via access privileges5private, public, protected, friend6.
– %e encapsulate how !ethods are
i!ple!ented by separating interfaceand i!ple!entation into di/erent :les.
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
16/40
16
#ccess Pri%ileges in C&ccess Pri%ileges in C&&ccess privileges allow us to restrict the
access to an ob'ectEs !e!bers.B types of access privileges in #))3
1.private
2.public3.protected 5-ecture A Inheritance6
4.friend 5-ecture 0perator 0verloading6
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
17/40
privateprivate andand publicpublic #ccess#ccessPri%ilegesPri%ileges
private !e!bers Got accessible fro!
anywhere except for
the ob'ect itself . %e generally declareattributes as private.
$y default, all!e!bers of a #))
class are declared asprivate ifunspeci:ed.
public !e!bers ccessible fro!
anywhere including
outside the class. %e generally declare!ethods as public.
$y default, all!e!bers of a #))
struct are declaredas public ifunspeci:ed.
17
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
18/40
18
OOP Principle "OOP Principle "
$ncapsulation$ncapsulation &o !aintain data integrity, attribute is
usually set hidden9private inside a class toprevent direct !odi:cation fro! outside theclass.
&o access or !odify an ob'ect7 attribute fro!outside the class, we provide public get orset !ethods.
Het !ethod 4 a !ethod that returns thevalue of an attribute but does not !odify it.et !ethod 4 a !ethod that !odi:es the
value of an attribute.
OOP n Pract cen ract ce
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
19/40
19
OOP n Pract ce "n ract ce "$ncapsulation$ncapsulation
ince all our !ethods are !eant to be used fro! outside the class, we should declare the! as public.
&he next step is to consider where to place the i!ple!entation.class Student { // private by deault
int id; vector subjects; public! // public ro" now on int get#d(); // get "ethod or id void set#d (int id); // set "ethod or id void show_subjects(); void register_subject();
void withdraw_subject();};
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
20/40
20
Placing ImplementationPlacing Implementation &here are 2 ways to place the
i!ple!entation 5!ethod body6 in #)).
class Student {
int id;
$$$
int get#d(); //prototype
void set#d (int id); //prototype
$$$
}; // %nd o class
int Student!!get#d() {
return id;
}
void Student!!set#d (int id) {
this&>id ' id;
}
class Student {
int id;
$$$
int get#d() {
return id;
}
void set#d (int id) {
this&>id ' id;
}
$$$
}; // %nd o class
Inside o! class declarationOutside o! class declarati
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
21/40
21
Placing ImplementationPlacing Implementation
Outside o! Class 'eclarationOutside o! Class 'eclaration
"!!" is a scope resolution operator. "Student!!" indicates that the function is a
method of Student class, not a global function.
class Student { int id;
$$$
int get#d(); //prototype
void set#d (int id); //prototype
$$$
}; // %nd o class
int Student!!get#d() {
return id;
}
void Student!!set#d (int id) {
this&>id ' id;}
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
22/40
22
(he(he thisthis PointerPointer
We use this pointer to refer to a member of aclass.
class Student {
int id;
$$$
void set#d (int id); //prototype
$$$
}; // %nd o class
$$$
void Student!!set#d (int id) {
this&> id ' id;
}
id here is function parameter,
not attribute
id here is attribute
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
23/40
23
(he(he thisthis PointerPointer
We use this pointer to refer to a member of aclass.
class Student {
int id;
$$$ void set#d (int id); //prototype
$$$
}; // %nd o class
$$$
void Student!!set#d (int id) {
id ' id; // rong *para"eter ' para"eter*$
// +ttribute id is not updated$
this&> id ' id; // ,orrect *attribute ' para"eter*$
Student!!id ' id; // ,orrect *attribute ' para"eter*$
}
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
24/40
24
)aintaining 'ata Integrit* +ith)aintaining 'ata Integrit* +ith
$ncapsulation$ncapsulation
By declaring attributes as private and providingpublic set methods, we can prevent erroneousdata being entered into the object.
$$$// %nsure id entered is within range$
void Student!!set#d (int id) {
i (id < -......... id > 0000000000) {
cout id ' .;
} else
this&>id ' id;
}
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
25/40
25
Implementation in SeparateImplementation in Separate
,iles,ileso bring encapsulation to the ne!t level, we
separate the interfaces and its implementations
into different files.
For every class
# $lace its interface in a .hpp file # called header file, e.g.
%tudent.hpp.
# $lace its implementation in a .cpp file # called source
file, e.g. %tudent.cpp.
# &n every cpp that needs to refer to the class, "'include"
the hpp.
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
26/40
26
PP eader ,ilePP eader ,ilehe use of #ifndef, #define ( #endif is to
prevent multiple inclusion.
// Student$hpp header ile
2inde S345%63_788 // 8revent "ultiple inclusion$
2deine S345%63_788
2include
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
27/40
27
CPP Source ,ileCPP Source ,ile
)very cpp source file that needs to refer to theclass should "'include" the hpp.
// Student$cpp source ile
2include *Student$hpp* // 4se ** not $
int Student!!get#d() { return id; }void Student!!set#d (int id) { this&>id ' id; }void Student!!show_subjects() { $$$ }void Student!!register_subject() { $$$ }void Student!!withdraw_subject() { $$$ }
// "ain$cpp source ile2include *Student$hpp* // 4se ** not $
int "ain() { Student s; s$register_subject(); $$$
}
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
28/40
28
Wh* $ncapsulation?Wh* $ncapsulation?1. &o !aintain data integrity
4 $y li!iting direct access to attributes, we prevent classusers fro! providing invalid data to the ob'ect.
2. &o reduce the need for class users to worry aboutthe i!ple!entation4 $y separating the interface fro! the i!ple!entation
5shorter class declaration6, class users can focus on usingthe class instead of being bothered by the i!ple!entationof the class.
A. &o i!prove progra! !aintenance1.eparating the interface fro! the i!ple!entation enables
us to change the i!ple!entation without the class usersever being aware 5provided that the interface re!ains thesa!e6.
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
29/40
29
Categoriing ObjectsCategoriing Objects &here !ight be !any ob'ects.
2 co!!on ways to categori*e ob'ects are3
1. #ategori*e si!ilar ob'ects that have thesa!e attributes and behaviors, e.g. the
student exa!ple.4 %e have covered this one up until now.
2. #ategori*e ob'ects by co!position, that iswhen an attribute of a class is a class by
itself, e.g. a faculty has !any students.
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
30/40
30
Categoriing b* CompositionCategoriing b* Composition %hen categori*ing ob'ects by composition, an
attribute of a class is a class by itself. Is also referred to as a ;has$a< relationship.
Dxa!ple 13 faculty has !any students 5bothfaculty and student are classes6.
Dxa!ple 23 &ypical corporate organi*ation34 A classes can be identi:ed3 %ept& 'ta(& %ata.
4 %ept has 'ta( and %ata.
$ersonnel Data
$ersonnel%taff
Finance Data
Finance
%taff
%ales Data
%ales
%taff
Sales Dept. Finance Dept.
Personnel Dept.
Messages
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
31/40
31
Categoriing b* CompositionCategoriing b* Composition
%ample code for the composition involving facultyand student.
class Student { // Student is a class$
$$$
}; // %nd o class
class 9aculty { // 9aculty is a class$
string na"e;
vector students-; // ,o"position attribute is
// a class by itsel$
//vector students; // +lso co"position$ //Student students=-.; // +lso co"position$
//Student: students; // +lso co"position$
$$$
};
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
32/40
32
U)/ Class 'iagramU)/ Class 'iagram
)"* is a for!al notation to describemodels 5representations ofthings9ob'ects6 in sofware develop!ent.
#lass %iagram is one type of !anydiagra!s in +-.
#lass iagra! contains 2 ele!ents3
4 #lasses represent ob'ects with co!!onattributes, operations, and associations.
4 Associations represent relationships thatrelate two or !ore classes.
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
33/40
33
Class 'iagramClass 'iagram
ust have A sections.
#lass na!e is placed at the top section.
ttributes are placed at the !iddle section.
$ehaviors are placed at the botto! section.
77 denotes private access privilege.
7)7 denotes public access privilege.
7JK7 denotes "!any" 5array, vector6.
Student
id3intsub'ects3stringJK
)getId563int)setId5id3int63void)show8sub'ects563void)register8sub'ect563void)withdraw8sub'ect563void
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
34/40
34
Class 'iagramClass 'iagram
Associations shows the relationship betweeninstances of classes, e.g. a faculty has one or!ore students, a student belongs to exactly onefaculty only.
,acult*
na!e3stringstudents3tudentJK
)intake56
ssociation
1 1..n
7n7 !eans!any
Student
id3 intsub'ects3stringJK
)getId563int)setId5id3int63void)show8sub'ects563void)register8sub'ect563void
)withdraw8sub'ect563void
0e%isiting Procedural0e%isiting Procedural
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
35/40
35
0e%isiting Procedural0e%isiting Procedural
ProgrammingProgramming #)) in P11@1 is taught as a procedural
progra!!ing language. In procedural progra!!ing, the idea is to design a
progra! to solve a proble! by concentrating on theprocedures :rst and data second. &his approach isknown as topdown design.
Procedures and data are 2 separate units that relatepri!arily via function para!eter.
)ain Program'ata
,unction1 ,unction2 ,unction3
bl i h d lP bl ith P d l
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
36/40
36
Problems +ith ProceduralProblems +ith Procedural
ProgrammingProgramming +nrestricted access to global
data and procedures. Poor !odeling of the real
world 5data and proceduresare separated6.
Got the way that hu!ansnaturally think about a situation.
Poor code reusability.
struct Student {
int id; vector subjects;};
void register_subject(Student? a){ $$$ }
void withdraw_subject(Student? s){ $$$ }
int "ain() {
Student s; register_subject (s); withdraw_subject (s);}
Wh Obj O i dWh Obj t O i t d
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
37/40
Wh* Object-OrientedWh* Object-Oriented
Programming?Programming?
%olve the problems of procedural programming. Lestrict access to data and procedures 5via
encapsulation6.
Better modeling of real world objects *via
abstraction+.4 ata and procedures are co!bined in a single unit.
# )asier to understand, correct, and modify.
Better code reusability # e!isting objects can be
reused to create new objects via inheritance *ecture
-+.
More useful for development of large and comple!
systems.
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
38/40
38
Con%erting ProceduralCon%erting Procedural
Program to OOPProgram to OOP
1. Identify the variables and the globalfunctions that use the variables aspara!eters, create a class and include thevariables and the global functions as class
!e!bers.2. ake all attributes private. #onst attributes
can opt for public.
A. For !ethods that use the class as
para!eter5s6, re!ove 0GD such para!eter. &hen update the !ethod body to refer to the!e!ber instead of the re!oved para!eter.
// 8rocedural version 8oint read8oint() {
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
39/40
39
// 8rocedural version2include using na"espace std;struct 8oint {
int @ y; // public by deault};
8oint read8oint();void print8oint (8oint);double dist8oint (8oint 8oint);
int "ain() { 8oint p- ' read8oint(); cout
-
8/20/2019 Lecture01 Objectorientedprogramming 130120010815 Phpapp01 (1)
40/40
40
// CC8 version2include using na"espace std;
class 8oint { int @ y; // private by deault public! void read8oint(); void print8oint(); double dist8oint (8oint A );
};
void 8oint!!read8oint() {cout > @ >> y;}
void 8oint!! print8oint() {cout