Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented...
Transcript of Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented...
Object Oriented Programming
Computer Engineering Department
Sharif University of Technology
Classification (1)
2
Classification (2)
3
Classification (2)
3
Classification (3)
4
? ?
Classification (4)
5
Classification (4)
5
What is the problem of this classification?
What have you used for classification in C?
Sample (1)
7
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
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;
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;
Sample (3)
9
doube
getAreaForSquare(Squar
e s){
if(s == null){
return -1;
}
return
s.side * s.side;
}
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;
}
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;
}
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;
}
Any idea to make this easier?
An idea
12
typedef struct {
double h;
double b;
double getArea();
} Triangle;
...
Triangle t;
double area =
t.getArea();
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();
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”
.
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?
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) ...
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) ...
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!
Let’s extend the problem!
14
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
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){
...
}
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){
...
}
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”.
Now, you know what OOP is about!
Object
17
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
Object:1st definition
18
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
Object:2nd definition
19
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
Object:3rd definition
20
Object:3rd definition
• Object has state, behavior and identity.
20
Object:4th definition
21
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
Types of Objects
22
Types of Objects
• An object represents an entity
• Physical Entity: Book, Car, Road, City, ...
• Conceptual Entity: Formulas, Chemical Process, ...
• Software Entity: LinkedList, Structure, ...
22
Some questions
23
Some questions
• Is Rectangle an object?
• Is Shape an object?
• Is int an object?
• Is void an object?
23
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!
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!
Class
24
Class
• Class is the “type” of objects!
• Objects are referred to as “instances” of classes.
24
Class
• Class is the “type” of objects!
• Objects are referred to as “instances” of classes.
24
An instance of type Squarewith side = 490px;
Class Definitions
25
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
Any Questions?