object oriented programming with c++ - uranchimeg.comuranchimeg.com/ec203/mec203lec02.pdfm.ec203* --...

41
Object oriented programming with C++ T.Uranchimeg Prof. Dr. Email [email protected] Power Engineering School http://www.uranchimeg.com/

Upload: vokiet

Post on 14-Apr-2019

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02

Object oriented programming

with C++

T.UranchimegProf. Dr.

Email

[email protected]

Power Engineering School

http://www.uranchimeg.com/

Page 2: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 2

Subjects

• Object Interactions

• A message

• Inheritance

• Polymorphism

• SDLC

Page 3: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 3

Exercise 1-001

Page 4: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 4

True table

Page 5: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 5

Exercise 1-002

From previous

table make

UML notation

Page 6: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 6

Exercise 1-003

Page 7: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 7

Instance of class

Page 8: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 8

Object Interactions

Objects interact by sending

messages to each other. The

object that sends the message is

the requestor of a service that

can be provided by the receiver

object.

Page 9: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 9

A message

The object's interface consists of a set

of commands, each command

performing a specific action. An

object asks another object to perform

an action by sending it a message. The

requesting (sending) object is referred

to as sender and the receiving object is

referred to as receiver.

Page 10: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 10

Object and message

Page 11: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 11

An example

For example, a School object

asks the Student object for its

name by sending it a message

asking for its name. The

receiving Student object

returns the name back to the

sending object.

Page 12: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 12

An message

A message can also contain information the sending

objects needs to pass to the receiving object, called

the argument in the message. A receiving object

always returns a value back to the sending object.

This returned value may or may not be useful to the

sending object.

Page 13: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 13

The example

For example, the School object now

wants to change the student's name. It

does this by sending the Student

object a message to set its name to a

new name. The new address is passed

as an argument in the message. In this

case, the School object does not care

about the return value from the

message.

Page 14: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 14

A message

A message contains

three parts:

Page 15: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 15

Some example messages

Page 16: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 16

Message structure

1. A receiver object

2. A method the receiver

knows how to execute

3. An ordered set of

parameters that this method

requires to carry out its

function

Page 17: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 17

Message structure

1. The operation to be invoked or started,

which is the service requested and

must be an accessible operation

2. The input data required by the

operation to perform, which is known

as the arguments

3. The output data, which is the reply to

the message and is the actual result of

the request

Page 18: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 18

Message signature

In order for a message to make

sense, the sender and receiver must

agree on the format of the message.

This format is stipulated in a

message signature that specifies

the name of the method to be

executed and the parameters to be

included.

Page 19: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 19

Inheritance

Another important concept of

object-oriented programming is

inheritance. Inheritance allows a

class to have the same behavior as

another class and extend or tailor

that behavior to provide special

action for specific needs.

Page 20: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 20

Example

Let's use the following

application as an example. Both

Graduate class and

Undergraduate class have

similar behavior such as

managing a name, an address, a

major, and a GPA.

Page 21: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 21

Continue

Rather than put this behavior in

both of these classes, the behavior

is placed in a new class called

Student. Both Graduate and

Undergraduate become subclass

of the Student class, and both

inherit the Student behavior.

Page 22: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 22

Example

Both Graduate and Undergraduate classes can then

add additional behavior that is unique to them. For

example, Graduate can be either Master's program or

PhD program. On the other hand, Undergraduate

class might want to keep track of either the student is

Peter, Junior or Senior.

Page 23: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 23

Super and sub class

Classes that inherit from a class are

called subclasses. The class a

subclass inherits from are called

superclass. In the example, Student

is a superclass for Graduate and

Undergraduate. Graduate and

Undergraduate are subclasses of

Student.

Page 24: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 24

Subclasses of the Vehicle class

Page 25: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 25

Polymorphism

he ability of different objects to respond to

the same message in different ways is

called polymorphism, a Greek term

meaning "many forms." The term can be

intimidating, and polymorphism is often

considered an advanced concept in object

technology. But the basic idea couldn't be

simpler: Each object can have a unique

response to the same message.

Page 26: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 26

Polymorphism example

Can be send same message to

objects but can be returned

different result.

Page 27: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 27

Lifecycle of program

Page 28: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 28

SDLC

A Systems Development Life

Cycle (SDLC) is any logical

process used by a systems analyst

to develop an information system,

including requirements,

validation, training, and user

(stakeholder) ownership.

Page 29: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 29

SDLC

Page 30: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 30

Waterfall model

Page 31: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 31

Spiral model

Page 32: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 32

V model

Page 33: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 33

Software Product Engineering Lifecycle

Page 34: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 34

Requirement and result

Page 35: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 35

Exercise 2-1

2-1. Обьектуудын харилцан

үйлчлэлцэх арга юун дээр

тулгуурладаг вэ? Жишээгээр

тайлбарла.

Page 36: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 36

Exercise 2-2

2-2. Өгөгдлийг яагаад нуудаг

вэ? Өгөгдлийг далдлах ямар

ач холбогдолтой вэ? Бүх

өгөгдлийг нууц болох уу?

Яагаад вэ?

Page 37: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 37

Exercise 2-3

2-3. Ангийн удамших шинж

гэж юу вэ? Удамшил ямар

онцлогтой вэ? Эх болон хүү

анги гэж юу вэ?

Page 38: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 38

Exercise 2-4

2-4. Полиморфизм гэж юу вэ?

Энгийн жишээгээр тайлбарла.

Page 39: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 39

Have you questions?

Page 40: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 40

Summary

• Object Interactions

• A message

• Inheritance

• Polymorphism

• SDLC

Page 41: Object oriented programming with C++ - uranchimeg.comuranchimeg.com/ec203/mec203Lec02.pdfM.EC203* -- OOP (C++) -- Lecture 02 19 Inheritance Another important concept of object-oriented

M.EC203* -- OOP (C++) -- Lecture 02 41

End of…

Thank you for

ATTENTION