c++: object oriented programming jim allert. introduction zinstructor: jim allert zemail:...

Post on 11-Jan-2016

223 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

C++: Object Oriented Programming

Jim Allert

Introduction

Instructor: Jim AllertEmail: jallert@d.umn.eduPhone:

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

Registration and Waiting Lists

Waiting list priorities set by CSE

See me after class if you are interested in a override

Today

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

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

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

Expected Background

C programming skills Control structures Pointers (linked lists)

No object-oriented experience necessary

Experience with: Turbo C/C++ development environment

Syllabus

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

lectureAssignments and ExamsGroup work not allowed.UMD computer accounts and

facilities

Communication

Email is the preferred mediumProblems with homework

assignments, etc.1. Contact TA2. Contact me

Problems with TA or TA explanations Contact me

Outline: the Big Picture

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

programming conceptsObject-oriented programming and

C++

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

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

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

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

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”

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

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

Another Example

Problem: Balance checkbookTop-level tasks

1. Get information2. Perform computations3. Print results

BalanceCheckbook

PerformComputations

GetInformation

PrintResults

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

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

Bank account object design

Bank Account

balance

get_balance()deposit(amount)withdraw(amount)

Data portion

Functions

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();

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

OOProgramming: Concepts

AbstractionEncapsulationObjects and ClassesInheritancePolymorphismDynamic binding

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

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

Objects and Classes

Object identifiable component in problem domain

Class Abstract Data Type (ADT): state +

operations (services)

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

Inheritance

Object SingleFamilyHouse is a kind of object House

Inherits all state and servicesAdds new onesReuse and extend code

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);

Dynamic Binding

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

implementation at run-time

OOP Concepts Recap

AbstractionEncapsulation

Objects and Classes

InheritancePolymorphismDynamic binding

Object-based programming

(Ada)

Object-oriented programming

(C++, Java)

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

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

C++ Distinguishing Characteristics

Strong static type checking“Manual” memory management

no automatic garbage collectionSupports multiple programming

models procedural and object-oriented

Multiple inheritanceOperator overloading

top related