Академический Документы
Профессиональный Документы
Культура Документы
II
OO
programiranje
Ivica Crnković
Mälardalen
University,
Osijek
University
Ivica.crnkovic@mdh.se
Object-‐oriented
programming
• Objects
–
the
primary
interest
of
programming
– Object-‐oriented
Analysis
and
Design
– Object-‐oriented
programming
languages
• C++
• C#
• Java
• ….
IDEA
of
OO
• Map
the
objects
from
the
real
world
to
objects
in
soOware
• Example:
-‐
a
library
system
IDEA
of
OO
• Map
the
objects
from
the
real
world
to
objects
in
soOware
• Example:
-‐
a
library
system
– a
computer
with
a
list
of
available
books
– customers
–
librarian
IDEA
of
OO
• Example:
-‐
a
library
system
– a
computer
with
a
list
of
available
books
–
list
of
borrowed
books,
– Customers
– librarian
*Books
*Customers
*Librarians
Object
-‐
Methods
• Example:
-‐
Librarian
Librarian
UserName
Password
Name
LastName
E-‐Mail
Telephone
ExperZse
registerNewCcustomer
registerNewBook
borrow_book
return_book
getPersonalData
setPersonalData
getExperZse
setExperZse
Object
-‐
Methods
• Example:
-‐
Customer
Customer
UserName
Password
Name
LastName
E-‐Mail
Telephone
RegistraZon_date
*Borrowed_books
listBorrowedBooks
getPersonalData
setPersonalData
getRegistraZonData
getRegistraZonDate
isMembershipValid
Object
-‐
AUributes
• Example:
-‐
Book
Book
Author
Title
ISBN
Publisher
Borrowed
isBorrowed
setBorrowed
getBookData
getBookData
Object
-‐
AUributes
• Example:
-‐
Library
Library
*Books
*Customers
*Librarians
listBooks
addBook
removeBook
addCustomer
removeCustomer
addLibrarian
removeLinbrarian
listexpiredCustomers
Object
-‐
Inheritance
• Example:
-‐
Librarian
&
Customer
Librarian
Customer
UserName
UserName
Password
Password
Name
Name
LastName
LastName
E-‐Mail
E-‐Mail
Telephone
Telephone
RegistraZon_date
ExperZse
*Borrowed_books
registerNewCcustomer
listBorrowedBooks
registerNewBook
getPersonalData
borrow_book
setPersonalData
return_book
getRegistarZonData
getPersonalData
getRegistarZonDate
setPersonalData
isRegistarZonValid
getExperZse
setExperZse
Object
-‐
Inheritance
User
UserName
Password
Name
LastName
E-‐Mail
Telephone
getPersonalData
setPersonalData
Librarian
Customer
ExperZse
RegistraZon_date
registerNewCcustomer
*Borrowed_books
registerNewBook
listBorrowedBooks
borrow_book
getRegistarZonData
return_book
getRegistarZonDate
getExperZse
isRegistarZonValid
setExperZse
Object
vs.
Classes
• Class
–
defines
a
form
of
an
object
• Object
–
an
instance
of
a
class
!"#$%&'()
!"#$%&'#(
)&""*+$,(
%&'#(
-&".%&'#(
/01&23(
!"#$%&'()
4#3#56+7#(
!"#$%&'#( *'+,#$(-.%/01-$')
)&""*+$,( 2%((%3'1)4%%5#6)
Customer
Ivica;
%&'#(
-&".%&'#( 7,#$2%((%3'12%%5#)
/01&23(
8#.)#$"+7&39&.&(
4#3#56+7#(
*'+,#$(-.%/01-$') Customer
Franjo;
"#.)#$"+7&39&.&(
2%((%3'1)4%%5#6) +'$*'+,#$-(.%/8-$-)
7,#$2%((%3'12%%5#)
+'$*'+,#$-(.%/8-$')
8#.)#$"+7&39&.&( ,#*'+,#$-(.%/9-7,1)
"#.)#$"+7&39&.&(
+'$*'+,#$-(.%/8-$-)
+'$*'+,#$-(.%/8-$') !"#$%&'()
,#*'+,#$-(.%/9-7,1)
!"#$%&'#(
New
Customer;
)&""*+$,(
%&'#(
-&".%&'#(
/01&23(
4#3#56+7#(
*'+,#$(-.%/01-$')
2%((%3'1)4%%5#6)
7,#$2%((%3'12%%5#)
8#.)#$"+7&39&.&(
"#.)#$"+7&39&.&(
+'$*'+,#$-(.%/8-$-)
+'$*'+,#$-(.%/8-$')
,#*'+,#$-(.%/9-7,1)
OO
Programming
• C++
• Java
• C#
• ObjecZveC
• Python…
– Similar
in
principles
–
different
in
syntaxes
and
details
OO
Programming
• Tools
– gcc
or
g++
compilers
– Eclipse
– CodeBlocks
– VisualStudio
Eclipse
CodeBlocks
Print
and
read
in
C++
Output:
159;
Example
Clock
Clock
Hour
Minute
Second
get_hours
get_minutes
get_seconds
display
reset
Tick
Clock()
Clock(int
h,
int
m,
int
s)
Class
DefiniZon
class
Name
{
public:
declara*on
of
visible
member
func*ons
and
data
members
private:
declara*on
of
hidden
member
func*ons
and
data
members
};
Class
Clock
class Clock {!
public: !
Clock ();!
Clock ( int hour, int min, int sec);!
void set ( int hour, int min, int sec);!
int read_hour () {return h;}!
int read_min () {return m;}!
int read_sec () {return s;}!
void write (bool write_sec=true);!
void tick ();!
private:!
int hh, mm, ss;!
};!
Class
Clock
constructors
Clock::Clock () !
{ hh=0; mm=0; ss=0;}!
!
!
Clock::Clock ( int hour, int min, int sec)!
{ hh=hour; mm=min; ss=sec;}!
Class
Clock
member
funcZons
void Clock::set ( int hour, int min, int sec);!
{!
hh=hour; mm=min; ss=sec;!
};!
!
void Clock::write (bool write_sec=true)!
{!
cout << setw(2) << setfill(‘0’) << hh << “:” !
<< setw(2) << setfill(‘0’) << mm;!
if (write_sec)!
<< setw(2) << setfill(‘0’) << ss; !
};!
!
!
Class
Clock
member
funcZons
void Clock::tick()!
{!
ss = (ss+1) % 60;!
if (ss == 0)!
{!
mm = (mm+1) % 60;!
if (mm == 0)!
hh = (hh+1) % 24;!
}!
};!
!
!
CreaZng
objects
Clock k1;!
Clock k2 (8,25,3);!
Clock k3 = Clock(12,11,3);!
!
Clock *p1, *p2; // pointers to Clock objects!
!
p1 = new Clock;!
p2 = new Clock (2,8, 34);!
!
class
Array
Data
size
size
a:
Data
??
size
b:
Data
??
size
Int Array:: length () const { // when the function is defined outside the class
More
methods
double Array::getArray ( int i ) { // get array element!
!
if ( i < size )!
return (data[i]);!
else {!
cout << “Wrong index “ << i << endl;!
return 0;!
}!
}!
!
Void Array::putArray ( int i, double d ) { // get array element!
!
if ( i < size )!
data[i] = d;!
else !
cout << “Wrong index “ << i << endl;!
return;!
}!
!
Example
–
using
objects
#include <iostream>!
using namespace std;!
int main () {!
Array a(25);!
int l = a.lenght();!
double d =0.0;!
for (int i=0; i < l; i++) {!
a.putArray(i, d);!
d=d+1;!
}!
}!
a:
Data
0.0
25
1.0
2.0
25
24.0
Example
–
using
objects
#include <iostream>!
using namespace std;!
int main () {!
Array a(25);!
int l = a.lenght();!
double d =0.0;!
for (int i=0; i < l; i++) {!
a.putArray(i, d);!
d=d+1;!
}!
a.putArray(0,30.0);!
}!
a:
Data
30.0
25
1.0
2.0
25
24.0
Example
–
using
objects
#include <iostream>!
using namespace std;!
int main () {!
Array a(25);!
int l = a.lenght();!
double d =0.0;!
for (int i=0; i < l; i++) {!
a.putArray(i, d);!
d=d+1;!
}!
a.putArray(0, 30.0);!
cout << a.getArray(0) << a.getArray(1) << endl;!
}!
Output:
30.0
1.0
Example
–
using
objects
#include <iostream>!
using namespace std;!
int main () {!
Array a(25), b(25);!
int l = a.lenght();!
double d =0.0;!
for (int i=0; i < l; i++) {!
a.putArray(i, d);!
d=d+1;!
}!
b = a;!
cout << “a: “ << a.getArray(0) !
<< “b: “ << b.getArray(0) << endl;!
}!
Output:
a:
0.0
b:
0.0
Example
–
using
objects
#include <iostream>!
using namespace std;!
int main () {!
Array a(25);!
int l = a.lenght();!
double d =0.0;!
for (int i=0; i < l; i++) {!
a.putArray(i, d);!
d=d+1;!
}!
Array b = a;!
b.putArray(0, 50.0);!
cout << “a: “ << a.getArray(0) !
<< “b: “ << b.getArray(0) << endl;!
}!
Output:
a:
50.0
b:
50.0
Example
–
using
objects
a:
Data
0.0
25
1.0
2.0
25
b:
Data
25
Or!
Array::Array (cost Array& a): size(a.size) {!
a:
Data
0.0
25
1.0
2.0
25
b:
Data
0.0
25
1.0
2.0
25
24.0
Example
–
using
objects
#include <iostream>!
using namespace std;!
int main () {!
Array a(25);!
int l = a.lenght();!
double d =0.0;!
for (int i=0; i < l; i++) {!
a.putArray(i, d);!
d=d+1;!
}!
Array b = a;!
b.putArray(0, 50.0);!
cout << “a: “ << a.getArray(0) !
<< “b: “ << b.getArray(0) << endl;!
}!
Output:
a:
0.0
b:
50.0
What
happens
when
we
delete
an
object;
delete
a;
????
a:
Data
0.0
25
1.0
2.0
25
24.0
What
happens
when
we
delete
an
object;
delete
a;
a:
Data
0.0
25
1.0
2.0
25
24.0
Example
–
using
objects
#include <iostream>!
using namespace std;!
int main () {!
Array a(25), b(25);!
int l = a.lenght();!
double d =0.0;!
for (int i=0; i < l; i++) {!
a.putArray(i, d);!
d=d+1;!
}!
! b = a;! This
is
an
assignment
-‐
not
a
copy!
!
b.putArray(0, 50.0);!
cout << “a: “ << a.getArray(0) !
<< “b: “ << b.getArray(0) << endl;!
}!
Output:
a:
50.0
b:
50.0
n
“=“
i t
mea
s
at
doe
Example
–
using
objects
ent
de
fi n i Z o n
(wh
n
a s s ignm
s
is
a
Thia:
Data
0.0
25
1.0
2.0
25
To
avoid
problems
when
wriZng
a
=
a;
b = a;! 24.0
b:
Data
25
Output:
a:
50.0
b:
50.0
What
does+
mean
We
define
it
as
“concatenate”
a:
Data
0.0
a:
Data
0.0
25
1.0
25
1.0
2.0
2.0
25
24.0
24.0
50
0.0
b:
Data
0.0
1.0
25
1.0
2.0
2.0
25
Array c! 24.0
c = a + b;! 24.0