35
C++: Object Oriented Programming Jim Allert

C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: [email protected] zPhone: y726-7194 zOffice: Heller Hall 324A

Embed Size (px)

Citation preview

Page 1: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

C++: Object Oriented Programming

Jim Allert

Page 2: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Introduction

Instructor: Jim AllertEmail: [email protected]:

726-7194Office: Heller Hall 324AHours: 10:00-12:00 MWFwww.d.umn.edu/~jallert

Page 3: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Registration and Waiting Lists

Waiting list priorities set by CSE

See me after class if you are interested in a override

Page 4: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Today

Course goals and prerequisitesSyllabus and logisticsC++: Larger picture and motivationC++: the kernel language

Page 5: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Course Goals

Change the way you think OOP different from traditional

procedural modelLearning the C++ constructs

why and when use a construct when not to use a construct

C++ syntax the easiest

Page 6: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

You Will Not...

Become an expert C++ programmer A starting point for further study

Learn all the features of C++ C++ is a very complex language

Learn advanced OOP techniquesBecome expert at object-oriented

design Read a book on Software Engineering

Page 7: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Expected Background

C programming skills Control structures Pointers (linked lists)

No object-oriented experience necessary

Experience with: Turbo C/C++ development environment

Page 8: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Syllabus

Textbooks and on-line resources please read assigned chapter(s) before

lectureAssignments and ExamsGroup work not allowed.UMD computer accounts and

facilities

Page 9: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Communication

Email is the preferred mediumProblems with homework

assignments, etc.1. Contact TA2. Contact me

Problems with TA or TA explanations Contact me

Page 10: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Outline: the Big Picture

History of programming languagesApproaches to Program Design Overview of object-oriented

programming conceptsObject-oriented programming and

C++

Page 11: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

History of Programming Languages

pre-mid-1950’s machine and assembly language “ad hoc” data representation and control

1960’s Fortran, Cobol, Algol arrays, control structures Simula 67, forerunner of C++

1970’s C, Pascal block program structure, structured control stepwise refinement

Page 12: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

History of Programming Languages

Early 80’s Modula-2, Ada modules, Abstract Data Types (ADT’s)

1980’s: “Classic” OOP C++, Smalltalk inheritance, polymorphism

1990’s: C++, Java templates, iterators, exceptions, interfaces

Page 13: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Approaches to Program Design

Stepwise refinement Based on the operations performed General operations iteratively decomposed into

specific ones Object-oriented design

Problem decomposed into real-world objects Objects have a well-defined interface Focus on behavior and collaboration of objects

Page 14: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Structure Chart Boxes

Example: getting from UMD to MetroDome

Algorithm:

Subtask 1: Get in car at parking lot …

Subtask 2: Drive to Minneapolis

Subtask 3: Find MetroDome

Subtask 4: Get out of car

Page 15: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Developing an Algorithm (cont)

Strategy (cont): repetitively divide tasks until each task is easily solved

Example: Subtask 2 - Drive to Minneapolis2.1 Drive east to I352.2 Drive south to I35W2.3 Drive south to Minneapolis

Each division of a task is a “stepwise refinement”

Page 16: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Stepwise Refinement

Do stepwise refinement until all tasks easy

Example: 2.1 - Drive east to I352.1.1 Exit parking lot to East2.1.2 Turn right on Woodland2.1.3 Turn left on 21st2.1.4 Enter I35

This process is know as Top-Down Design

Page 17: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Multi-layer Structure Chart

UMD toMetroDome

Drive toMinneapolis

Exit ParkingLot

South toMinneapolis

Drive southto I35W

Drive eastto I35

Enter I35Left on 21stRight at

Woodland

FindMetroDome

Get out ofCar

Get in Car

Page 18: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Another Example

Problem: Balance checkbookTop-level tasks

1. Get information2. Perform computations3. Print results

BalanceCheckbook

PerformComputations

GetInformation

PrintResults

Page 19: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Pseudo-code Example

1. Get information1.1 Get starting balance1.2 Get transaction type1.3 Get transaction amount

2. Perform computations2.1 IF deposit THEN add to balance ELSE subtract from

balance

3. Print results3.1 Print starting balance3.2. Print transaction

3.2.1 Print transaction type3.2.2 Print transaction amount

3.3 Print ending balance

Page 20: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Motivation for OO approach

More transparent mapping between: requirements, design, and implementation easier verification and validation

Lower software maintenance costs danger of “ripple effect” reduced

Better code reuse reuse by “tweaking” can be avoided

Page 21: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Bank account object design

Bank Account

balance

get_balance()deposit(amount)withdraw(amount)

Data portion

Functions

Page 22: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

OOP broad overview

1. Define bank account objecta. Data membersb. Member functions

2. Programa. Instantiate a bank account objectb. Use bank account object

i.e. Bank_account my_checking;my_checking.deposit(500);cout << my_checking.get_balance();

my_checking.withdraw(100); cout << my_checking.get_balance();

Page 23: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Manufacturers, Clients, Users

Manufacturers build objectsClients use objects in programs

client code uses objects you make available through your class definitions.

Users use programsWe are interested in object manufacturers and clients here

Page 24: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

OOProgramming: Concepts

AbstractionEncapsulationObjects and ClassesInheritancePolymorphismDynamic binding

Page 25: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Abstraction

Separate: essential qualities from incidental ones

Behavior is essentialImplementation is incidentalExample: an interface to a vending

machine as its abstraction essential behavior: dispensing products incidental implementation: the actual products

Page 26: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Encapsulation

Explicit boundary between abstraction and implementation

Encapsulation of all essential characteristics of the object

Frees developers to change implementation list processing using arrays list processing using linked lists

Protects clients from using unstable code

Page 27: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Objects and Classes

Object identifiable component in problem domain

Class Abstract Data Type (ADT): state +

operations (services)

Page 28: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Example

House: an Object data state:

kitchen_lights, data: on/off room_temperature, data: degrees

services: turn_on(kitchen_lights) adjust_thermostat(-5)

House blueprint: a Class

Page 29: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Inheritance

Object SingleFamilyHouse is a kind of object House

Inherits all state and servicesAdds new onesReuse and extend code

Page 30: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Polymorphism

Polymorphism Objects of related types can be used

interchangeably Can always use a specialized object where a

generic one is allowed Example: appraising a house

generic appraisal form applied to specific homes• appraise(rambler);• appraise(log_home);• appraise(townhouse);• appraise(colonial);

Page 31: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Dynamic Binding

Dynamic binding Object methods selected at run time Or: interface matched with

implementation at run-time

Page 32: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

OOP Concepts Recap

AbstractionEncapsulation

Objects and Classes

InheritancePolymorphismDynamic binding

Object-based programming

(Ada)

Object-oriented programming

(C++, Java)

Page 33: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

Motivation: why C++ for OOP?

Large user community high-quality compilers and development tools for

many platforms learning aids: books, conferences, newsgroup,

seminars, consultantsMulti-paradigm language

Procedural: better C, Object-based Object-oriented

Legacy code: (mostly) backward compatible with C

Page 34: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

C++ Language Evolution

Simula (Norway, late 60-s)Bjarne Stroustrup, Bell Labs, early 80’sComplex simulation software“C with classes”C++

features and complexity increased with time we are lucky - ANSI standard created and vendors are supporting it

Page 35: C++: Object Oriented Programming Jim Allert. Introduction zInstructor: Jim Allert zEmail: jallert@d.umn.edu zPhone: y726-7194 zOffice: Heller Hall 324A

C++ Distinguishing Characteristics

Strong static type checking“Manual” memory management

no automatic garbage collectionSupports multiple programming

models procedural and object-oriented

Multiple inheritanceOperator overloading