+ All Categories
Home > Documents > Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented...

Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented...

Date post: 04-Jun-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
53
Object Oriented Programming Computer Engineering Department Sharif University of Technology
Transcript
Page 1: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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
Page 18: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Object

17

Page 33: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Class

24

Page 49: Object Oriented Programmingce.sharif.edu/.../root/lectures/presentation1.pdf · Object Oriented Programming Sharif University of Technology Computer Engineering Department. Classification

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

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

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

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

Any Questions?


Recommended