oops: the harsh realities of programming · course overview complexity and the nature of...

25
Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ OOPs: The Harsh Realities of Programming Robert Lowe Division of Mathematics and Computer Science Maryville College Robert Lowe OOPs: The Harsh Realities of Programming

Upload: others

Post on 27-Apr-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

OOPs: The Harsh Realities of Programming

Robert Lowe

Division of Mathematics and Computer ScienceMaryville College

Robert Lowe OOPs: The Harsh Realities of Programming

Page 2: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Outline

1 Course Overview

2 Complexity and the Nature of Programming

3 Overview of Object Oriented Programming

4 Object Oriented Programming in C++

Robert Lowe OOPs: The Harsh Realities of Programming

Page 3: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Preliminaries

Required MaterialsBig C++ 2nd Edition by CayHorstmannAn Account onsmc.cs.maryvillecollege.edu

Catalog Description

A continuation of Computer Science111 with emphasis on advancedprogramming features. Laboratorywork supplements and expandslecture topics and offers supervisedpractice using programming.

Robert Lowe OOPs: The Harsh Realities of Programming

Page 4: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Recommended Supplementary Text

The C++ Programming Language 4thEdition by Bjarne Stroustrup

Written by the creator of C++Serves as a Good ReferenceVery in-depth Explanations ofThe LanguageThis should be on the bookshelfof all serious C++ programmers.

Robert Lowe OOPs: The Harsh Realities of Programming

Page 5: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Course Goals

Gain an advanced understanding of Object OrientedProgramming.Learn to use Object Oriented Analysis and Design to buildlarge complex programs.Gain a preliminary understanding of low level programmingconcepts, especially memory addressing.Increase your knowledge of using and programming theUNIX operating system.Hone your knowledge of programming tools like make,g++, and gdb.Learn new tools, like rcs.

Robert Lowe OOPs: The Harsh Realities of Programming

Page 6: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Grading

GradingLabs 15%Programming Assignments 50%Exams 35%

Robert Lowe OOPs: The Harsh Realities of Programming

Page 7: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Summary of Classroom Expectations

Show up for class.Don’t cheat.Attempt all the work.Ask lots of questions.Late assignments will generally not be accepted.Don’t Panic!

Robert Lowe OOPs: The Harsh Realities of Programming

Page 8: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Outline of Major Topics

I. Object Oriented Programminga. Classes and Objectsb. Inheritance and Polymorphismc. Overloaded Operators and Functionsd. Object Oriented Analysis and Design

II. Problem Solving Techniquesa. Recursionb. Backtracking

III. Tools and Techniquesa. gdbb. UNIX System APIc. Exceptions

Robert Lowe OOPs: The Harsh Realities of Programming

Page 9: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Software Development Life Cycle

Programming Is:ExpensiveVery ComplexCritical

The Ninety-Ninety RuleThe first 90 percent of the codeaccounts for the first 90 percent of thedevelopment time. The remaining 10percent of the code accounts for theother 90 percent of the developmenttime.– Tom Cargill, Bell Labs

Image Source: wikipedia.org

Robert Lowe OOPs: The Harsh Realities of Programming

Page 10: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Coping with Complexity

The Main Challenge is Dealing With ComplexityAnalyze Big TasksDecompose Big TasksEncode Tasks into Computer Code

Robert Lowe OOPs: The Harsh Realities of Programming

Page 11: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Coping with Customers

Real world customers are not very tech savvy.Often, programs perform tasks outside of theprogrammer’s expertise.Many iterations are needed to capture requirements.Even then, requirements are typically vague!

Robert Lowe OOPs: The Harsh Realities of Programming

Page 12: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

What Makes Programming Difficult

ComplexityVague and Underdeveloped RequirementsTime PressureOne-Off Nature of Programs

Robert Lowe OOPs: The Harsh Realities of Programming

Page 13: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Programming Paradigms

Programming paradigms represent ways of thinking andmethods of decomposition.Imperative Programming All statements are direct commands

which alter the program’s state. (ex. Assembly andALGOL)

Procedural Programming Programs are decomposed intosub-routines. (ex. C and Pascal )

Functional Programming Computation is represented asmathematical functions. (ex. LISP and Erlang)

Object Oriented Programming Programs are decomposed intoobjects. (ex. C++ and C#)

Robert Lowe OOPs: The Harsh Realities of Programming

Page 14: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Effective Use of a Programming Language

General Tips for Programming Language UseA programming language is a formal representation ofideas.Think first and then code!Use a language that is well suited to the task at hand.Learn the idioms of your language.Always use the language to express, rather than obscure,your intentions!

Robert Lowe OOPs: The Harsh Realities of Programming

Page 15: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Objects and Classes

Object An entity with state and behavior. (Instance ofClasses)

VariablesConcrete Objects

Class A collection of objects with identical attributes andbehaviors. (Definition of Objects)

Member FunctionsMember VariablesAbstract Entity

Robert Lowe OOPs: The Harsh Realities of Programming

Page 16: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Parts of Speech and OOP

Nouns Objects and ClassesVerbs Member Functions

Adjectives Member Variables

Robert Lowe OOPs: The Harsh Realities of Programming

Page 17: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

An OOP Translation Example

1 Write some sentencesdescribing the robot and itsstate.

2 Write some sentencesdescribing what the robotcan do.

3 Sketch out what a robotclass may look like.

Robert Lowe OOPs: The Harsh Realities of Programming

Page 18: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Four Major Principles of Object Oriented Programming

Encapsulation Attributes and state are contained within objectsand access to these attributes are restricted.

Abstraction The inner workings of the objects are hidden.Inheritance Objects types can be derived from, and extend,

other object types. (Is-A relationship)Polymorphism Objects can be referenced by different types,

and they still behave correctly when treated asinherited types.

Robert Lowe OOPs: The Harsh Realities of Programming

Page 19: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Goals for Effective Object Oriented Programming

Your programs should be clear in intention.Objects should be cohesive.Objects should not be tightly coupled with other objects.

Robert Lowe OOPs: The Harsh Realities of Programming

Page 20: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Class Declaration

class name{public:

//public methodsprotected:

//protected variables and methodsprivate:

//private variables and methods};

Robert Lowe OOPs: The Harsh Realities of Programming

Page 21: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Access Modifiers

public Accessible to code inside and outside of the class.

MethodsConstructorsDestructor

protected Accessible to code within the class andsub-classes. (A big part of inheritance.)

Variables (with great care)Methods

private Accessible only to code within the class.VariablesMethods

Robert Lowe OOPs: The Harsh Realities of Programming

Page 22: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Constructors and Destructors

ConstructorsInitializer for a classCan be overloadedDeclared with same name as an object: name();No return type

DestructorCleans up after an object.Used frequently with dynamic memory.Named: ∼name();

Robert Lowe OOPs: The Harsh Realities of Programming

Page 23: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Member Initialization Lists

Member initialization lists can be used when simply copyingvalues from constructor arguments. They are an absolute mustwhen working with references!

name(int x, int y) : x(x), y(y){//constructor code

}

Robert Lowe OOPs: The Harsh Realities of Programming

Page 24: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Constructor and Destructors Best Practices

Unless you really know what you are doing, constructorsshould be public.You should provide at least:

1 No-Argument constructor: name();A copy constructor: name(name &c);If initial conditions make sense, you should have aconstructor which allows them to be set.

If you use any dynamic memory, you must create adestructor.

Robert Lowe OOPs: The Harsh Realities of Programming

Page 25: OOPs: The Harsh Realities of Programming · Course Overview Complexity and the Nature of Programming Overview of Object Oriented Programming Object Oriented Programming in C++ Outline

Course OverviewComplexity and the Nature of ProgrammingOverview of Object Oriented Programming

Object Oriented Programming in C++

Putting it All Together

Let’s implement our robot class!

Robert Lowe OOPs: The Harsh Realities of Programming