Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented...
Transcript of Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented...
![Page 1: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/1.jpg)
Object Oriented Programming
Computer Engineering Department
Sharif University of Technology
![Page 2: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/2.jpg)
Classification (1)
2
![Page 3: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/3.jpg)
Classification (2)
3
![Page 4: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/4.jpg)
Classification (2)
3
![Page 5: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/5.jpg)
Classification (3)
4
? ?
![Page 6: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/6.jpg)
Classification (4)
5
![Page 7: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/7.jpg)
Classification (4)
5
What is the problem of this classification?
![Page 8: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/8.jpg)
What have you used for classification in C?
![Page 9: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/9.jpg)
Sample (1)
7
![Page 10: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/10.jpg)
Sample (1)
• We want to write a program that will be used for computing area of geometric shapes.
• Circle, Triangle, Rectangle, Square, Kite, ...
• We want to write it in C.
7
![Page 11: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/11.jpg)
Sample (2)
8
typedef struct {
double h;
double b;
} Triangle;
typedef struct {
double side;
} Square;
typedef struct {
double w;
double h;
} Rectangle;
typedef struct {
double radius;
} Circle;
![Page 12: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/12.jpg)
Sample (2)We have to create a type for
each shape.
8
typedef struct {
double h;
double b;
} Triangle;
typedef struct {
double side;
} Square;
typedef struct {
double w;
double h;
} Rectangle;
typedef struct {
double radius;
} Circle;
![Page 13: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/13.jpg)
Sample (3)
9
doube
getAreaForSquare(Squar
e s){
if(s == null){
return -1;
}
return
s.side * s.side;
}
![Page 14: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/14.jpg)
Sample (3)We have to create a function for
computing the area of each shape.
9
doube
getAreaForSquare(Squar
e s){
if(s == null){
return -1;
}
return
s.side * s.side;
}
![Page 15: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/15.jpg)
Sample (4)
10
#define SQUARE 1;
#define TRIANGLE 2;
#define CIRCLE 4;
...
doube getArea(
void * s, int type){
if(type & SQUARE){
return
getAreaForSquare(
*((Square*) s));
}
...
return -1;
}
![Page 16: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/16.jpg)
Sample (4)We have to create a selection
function.
10
#define SQUARE 1;
#define TRIANGLE 2;
#define CIRCLE 4;
...
doube getArea(
void * s, int type){
if(type & SQUARE){
return
getAreaForSquare(
*((Square*) s));
}
...
return -1;
}
![Page 17: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/17.jpg)
![Page 18: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/18.jpg)
Any idea to make this easier?
![Page 19: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/19.jpg)
An idea
12
typedef struct {
double h;
double b;
double getArea();
} Triangle;
...
Triangle t;
double area =
t.getArea();
![Page 20: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/20.jpg)
An idea
• It will be good if each of the shapes computes its own area, isn’t it?
• How we can implement it in C?
• Function Pointer!?
• Does it really make it easy?
12
typedef struct {
double h;
double b;
double getArea();
} Triangle;
...
Triangle t;
double area =
t.getArea();
![Page 21: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/21.jpg)
An idea
• It will be good if each of the shapes computes its own area, isn’t it?
• How we can implement it in C?
• Function Pointer!?
• Does it really make it easy?
12
typedef struct {
double h;
double b;
double getArea();
} Triangle;
...
Triangle t;
double area =
t.getArea();
This
idea
is call
ed “E
ncap
sulat
ion”
.
![Page 22: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/22.jpg)
An idea
• It will be good if each of the shapes computes its own area, isn’t it?
• How we can implement it in C?
• Function Pointer!?
• Does it really make it easy?
12
typedef struct {
double h;
double b;
double getArea();
} Triangle;
...
Triangle t;
double area =
t.getArea();
This
idea
is call
ed “E
ncap
sulat
ion”
.
What about the selector function?Any idea to delete it?
![Page 23: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/23.jpg)
Another idea
13
typedef struct {
double getArea();
} Shape;
typedef struct
has functions of Shape{
double h;
double b;
double getArea();
} Triangle;
double getArea
(Shape * shape){
return
(shape->getArea)();
}
Triangle t;
... getArea(&t) ...
![Page 24: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/24.jpg)
Another idea
• It will be good if they are all cast-able to a type that has the getArea function!
• There is a trick to implement it in C. But for now, suppose there isn’t any!
• We invent the “has functions of”!
13
typedef struct {
double getArea();
} Shape;
typedef struct
has functions of Shape{
double h;
double b;
double getArea();
} Triangle;
double getArea
(Shape * shape){
return
(shape->getArea)();
}
Triangle t;
... getArea(&t) ...
![Page 25: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/25.jpg)
Another idea
• It will be good if they are all cast-able to a type that has the getArea function!
• There is a trick to implement it in C. But for now, suppose there isn’t any!
• We invent the “has functions of”!
13
typedef struct {
double getArea();
} Shape;
typedef struct
has functions of Shape{
double h;
double b;
double getArea();
} Triangle;
double getArea
(Shape * shape){
return
(shape->getArea)();
}
Triangle t;
... getArea(&t) ...
This
idea
is call
ed “P
olym
orph
ism”.
Each
sha
pe h
as it
’s ow
n
implem
enta
tion
of get
Are
a
but, sa
me
ones
syn
tact
icall
y!
![Page 26: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/26.jpg)
Let’s extend the problem!
14
![Page 27: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/27.jpg)
Let’s extend the problem!
• We want to draw the shapes.
• So, each shape has a color.
• We can add the color field to each of the types!
• Any easier approach?
14
![Page 28: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/28.jpg)
Yet another idea
15
typedef struct {
int color;
double getArea();
} Shape;
typedef struct
child of Shape{
double h;
double b;
double getArea();
} Triangle;
...
doube getArea(
Shape * s){
...
}
![Page 29: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/29.jpg)
Yet another idea
• It will be good if we can put the common section in the Shape!
• Again! There is a trick to implement it in C. But for now, suppose there isn’t any!
• We now invent “child of”.
15
typedef struct {
int color;
double getArea();
} Shape;
typedef struct
child of Shape{
double h;
double b;
double getArea();
} Triangle;
...
doube getArea(
Shape * s){
...
}
![Page 30: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/30.jpg)
Yet another idea
• It will be good if we can put the common section in the Shape!
• Again! There is a trick to implement it in C. But for now, suppose there isn’t any!
• We now invent “child of”.
15
typedef struct {
int color;
double getArea();
} Shape;
typedef struct
child of Shape{
double h;
double b;
double getArea();
} Triangle;
...
doube getArea(
Shape * s){
...
}
This
idea
is call
ed “I
nher
itanc
e”.
![Page 31: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/31.jpg)
Now, you know what OOP is about!
![Page 32: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/32.jpg)
Object
17
![Page 33: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/33.jpg)
Object
• An object is ....
• a programming entity that has both fields and method.
• a physical entity!
• a structure with inheritance, polymorphism, encapsulation.
• Hmm, let’s define it formally.
17
![Page 34: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/34.jpg)
Object:1st definition
18
![Page 35: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/35.jpg)
Object:1st definition
• An abstraction of something in problem domain, reflecting the capabilities of the system to keep information about it, interact with it, or both.
18
![Page 36: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/36.jpg)
Object:2nd definition
19
![Page 37: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/37.jpg)
Object:2nd definition
• Objects are deliberately characterized as if each were a person, with a role in the system that is based on its answer to these questions:
• What am I?
• What can I do?
• What do I know?
19
![Page 38: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/38.jpg)
Object:3rd definition
20
![Page 39: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/39.jpg)
Object:3rd definition
• Object has state, behavior and identity.
20
![Page 40: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/40.jpg)
Object:4th definition
21
![Page 41: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/41.jpg)
Object:4th definition
• We define an object as a concept, abstraction, or thing with crisp boundaries and meaning for the problem in hand. Objects serve two purpose: They Promote understanding of the real world and provide a practical basis for computer implementation.
21
![Page 42: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/42.jpg)
Types of Objects
22
![Page 43: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/43.jpg)
Types of Objects
• An object represents an entity
• Physical Entity: Book, Car, Road, City, ...
• Conceptual Entity: Formulas, Chemical Process, ...
• Software Entity: LinkedList, Structure, ...
22
![Page 44: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/44.jpg)
Some questions
23
![Page 45: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/45.jpg)
Some questions
• Is Rectangle an object?
• Is Shape an object?
• Is int an object?
• Is void an object?
23
![Page 46: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/46.jpg)
Some questions
• Is Rectangle an object?
• Is Shape an object?
• Is int an object?
• Is void an object?
23
Objec
ts?!
They
are
type
s!
![Page 47: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/47.jpg)
Some questions
• Is Rectangle an object?
• Is Shape an object?
• Is int an object?
• Is void an object?
23
Objec
ts?!
They
are
type
s!
I do not want to confuse you, but,Shape, Rectangle, int, and void types
are all objects of type: Type!
Forget it for now!
![Page 48: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/48.jpg)
Class
24
![Page 49: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/49.jpg)
Class
• Class is the “type” of objects!
• Objects are referred to as “instances” of classes.
24
![Page 50: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/50.jpg)
Class
• Class is the “type” of objects!
• Objects are referred to as “instances” of classes.
24
An instance of type Squarewith side = 490px;
![Page 51: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/51.jpg)
Class Definitions
25
![Page 52: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/52.jpg)
Class Definitions
• Defines the abstract characteristics of a thing, including the thing's characteristics, and the things it can do.
• Class is used to group related instances that have common properties, behavior, relationship, and semantics.
25
![Page 53: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification](https://reader035.fdocuments.us/reader035/viewer/2022062919/5f01e2de7e708231d4018473/html5/thumbnails/53.jpg)
Any Questions?