1 OOP - An Introduction ISQS 6337 John R. Durrett.

15
1 OOP - An Introduction ISQS 6337 John R. Durrett

Transcript of 1 OOP - An Introduction ISQS 6337 John R. Durrett.

Page 1: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

1

OOP - An Introduction

ISQS 6337

John R. Durrett

Page 2: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

Development Paradigms

• Increasing program complexity

• 1. Front panel switches

• 2. Assembly language

• 3. High level languages (3gl)(COBOL)

• 4. Structured Programming– reduce complex to simple parts– top down

• 5. OOP– self-contained == data + methods– inside out from classes

Page 3: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

3

Top Down, procedure-oriented design steps

• Identify tasks (verbs) to perform

• Stepwise refinement (N. Wirth)– break task into sub-tasks until

sub-tasks are simple enough to implement directly

• Write procedures to solve sub-tasks

• Combine simple procedures to create functionality required

Page 4: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

4

Procedural Programming

Main

Function 1Function 1 Function 5Function 4Function 3Function 2Function 1

Function 2.1 Function 2.2 Function 2.3

Function 2.2.1 Function 2.2.2

Function

Function

Global Data

FunctionFunction

Page 5: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

5

OO design steps

• Identify objects (nouns) in environment

• Establish behavior and attributes in objects

• Identify relationships between objects

• Specify class/object interface & implementation

Page 6: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

6

Object Oriented Programming

Data

Method Method

MethodMethod

Data

Method Method

MethodMethod

Data

Method Method

MethodMethod

Page 7: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

7

Classes & Objects

• Class– abstract pattern or template– “cookie cutter”– interface defining behavior– description of data & related

operations

• Object– particular instance or specimen– “cookie”– instantiation of abstract class– definition of memory

Page 8: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

8

Example: Integers

• Data (Attributes, fields, data members)

– Integers

• Operations (Methods, member funcs)

– Account_Balance()– Deposit()– Withdrawal()– Interest()

• All a user needs to care about is information input to operations and info returned

Page 9: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

Encapsulation (information hiding)

• binds data & code together• black box

– no need for programmer to understand how methods are implemented

• fields are safe from outside interference– program never access data members

– only access is through public methods

– semi-global variables

• key is to make data private & provide public access methods– i.e. a well defined interface

Page 10: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

10

C++ Access specifiers - control level of encapsulation

• All can contain data & methods

• public– available anywhere with scope – provide interface to private– like struct

• private– accessible only through public

member methods

• protected– Just like private (for now)

• friend

Page 11: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

Polymorphism

• one name many purposes– C: abs() labs() fabs()– C++ abs()

• one interface multiple methods

• function overloading

• operator overloading

• compile time polymorphism– int abs(int);– long abs(long);– float abs(float);

Page 12: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

12

Methods

• Functions that are members of class & therefore available in objects

• interface to private data

• Constructors (next slide)

• Mutators

• Accessors

• Destructors

Page 13: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

Initialization

• automatic initialization of member data

• Constructors

• C++ ex:class myclass {

private: // default for class

int a;

public:

myclass (int x);

void show();

};

myclass::myclass(int x) {

a = x;

}

void myclass::show(void) {

cout << a;

}

void main(){

myclass ob(4);

ob.show(); // ok

ob.a = 12; // wrong!

}

Page 14: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

Inheritance

• one object acquires properties of another

• hierarchical classification

• single inheritance (Java)

• multiple inheritance (C++)

• class - abstract representation

• object - defined variable of class type

Page 15: 1 OOP - An Introduction ISQS 6337 John R. Durrett.

15

Operator Overloading

• Increased flexibility

• Increased ease of use

• Increased control of class actions

• Not in java

• Example• Employee E1, E2;

• E1 = E2; cout << E1;

• Class-1.cpp example