Introduction to C++

Download Report

Transcript Introduction to C++

In tro ductio n to C ++
Programming is taking
• A problem
Find the area of a rectangle
• A set of data
length
width
• A set of functions
area = length * width
• Then
Applying functions to data to get answer
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
-1-
In tro ductio n to C
++
Find the Area of a Rectangle
In C
In English
area = length * width
Mentally form model
Compute Area
2 parameters
Length, l
Width, w
1 function
area = l * w
return
area
int compute_area (int l, int w)
{
return ( l * w );
}
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
-3-
In tro ductio n to C
++
Abstracting the World
Must ask several questions:
1. Should any arbitrary set of data and functions be grouped?
2. Are there preferred groupings?
3. If preferred groupings, how are these selected?
C
opyright 2006 OxfordConsulting, Ltd
1 January 2006
-4-
In tro ductio n to C
++
Abstracting the World
Object
Procedural
C, Basic, Pascal, Fortran
Around Procedures
Functions
Data
Functional
Lisp
Around Functions
Logical
Prolog
Around logical relationships
Object Centered
Smalltalk, C++, Objective C, Eiffel
Around Objects
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
-5-
In tro ductio n to C
++
Introduction to Objects
Objects are an Abstraction
Represent Real World Entities
Numbers
Character strings
Electronic Parts
Signals
Fruits
Usually nouns…...
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
-6-
In tro ductio n to C
++
Introduction to Objects
Electronic Part
State
conducting
Objects Have State
Value
ON
At a particular time
Electronic Part
Conducting
ON
Signal
State
value
Signal
Value
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
-7-
In tro ductio n to C
++
Introduction to Objects
Actions or Operations
Electronic Part
Methods
Describe how to carry out operations
Usually verbs
Electronic Part
Enable
Disable
Signal
Activate
State
value
Signal
Operations
add
scale
Add
invert
Scale
Invert
Copyright 2006 Oxford Consulting, Ltd
State
conducting
ON
Operations
enable
disable
activate
1 January 2006
-8-
In tro ductio n to C
++
Introduction to Objects
Set of Messages
Request for object to carry out one of its operations
Means to exchange data between objects
Signal
State
3
Operations
add
scale
invert
Signal
State
2
Operations
add
scale
invert
ADD
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
-9-
In tro ductio n to C
++
Object Oriented Programs
Program
Set of Objects
Set of possible actions
Exchange of messages
Instructing a particular object
To carry out one of its actions
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 10 -
In tro ductio n to C
++
Organizing Objects
Classes
Means for grouping
Entities with Common Properties and Attributes
Hierarchy
Means for Organizing
Classes into Subgroups of a Group
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 11 -
In tro ductio n to C
++
Organizing Objects
Programming
Languages
Object
Oriented
C++
Procedural
Java
Functional
C#
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 12 -
In tro ductio n to C
++
Organizing Objects
Vehicles
Sea
Air
Truck
Copyright 2006 Oxford Consulting, Ltd
Land
Automobile
Bicycle
1 January 2006
- 13 -
In tro ductio n to C
++
Communicating with Objects
In C++
Object
class Rectangle
{
public:
Rectangle() {l = 10; w = 22; }
int area () { return ( l * w );}
Rectangle
data - encapsulated
Length, l
Width, w
private:
function - encapsulated
area = l * w
int l;
int w;
}
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 14 -
In tro ductio n to C
++
Encapsulating Objects
Information Hiding
Abstra
c tion
En
c apsu
l ation
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 15 -
In tro ductio n to C
++
Encapsulating Objects - Information Hiding
Want to Make Certain Unnecessary Details
Inaccessible
Motivation
Reduce number of details need to deal with
Want interaction among objects simple as possible
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 16 -
In tro ductio n to C
++
Encapsulating Objects - Information Hiding
Key to obje
c t oriented thinking
Must have good strategy
Can not simply hide
Object
Is black box
Must have well defined interface
Only access through methods specified in interface
No access to internals
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 17 -
In tro ductio n to C
++
Encapsulating Objects - Abstraction
Strategy for information hiding
Consider the details…...
Ignore
Unessential
Less important
Focus
On essential
More important
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 18 -
In tro ductio n to C
++
Encapsulating Objects - Abstraction
Levels of Abstraction
Higher
More important details
Lower
Shift focus to lower
Types of Abstraction
Data
Data used becomes a high level concept
Functional
Function to be performed becomes a high level concept
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 19 -
In tro ductio n to C
++
Encapsulating Objects - Encapsulation
Packaging of related entities
Logically
Physically
Treat packaged items as unit - Usually the nouns
These will be the objects
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 20 -
In tro ductio n to C
++
Encapsulating Objects
List
Contents
Items in list
State Information
Number of items in the list
Operations
Add
Delete
Length
Copy
Exceptions
Overflow
Underflow
Exportable Items
Empty List
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 21 -
In tro ductio n to C
++
Definitions
What is object oriented programming…….
Not
– Programming with objects
– Programming with an object oriented language
– Moving objects around on a screen
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 22 -
In tro ductio n to C
++
Definitions
Must support 4 fundamental criteria…..
Must support abstraction
Focus on essential details
While ignoring non-essential
Must support abstract data types
ADT is a model
Encompasses
Type
Int, char, string
Set of Operations
These characterize the behavior of the type
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 23 -
In tro ductio n to C
++
Definitions
Must support inheritance
Ability to derive new objects from old
Makes the language extensible
Must support polymorphism
Specifies how objects
Respond to certain kinds of messages
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 24 -
In tro ductio n to C
++
Summary
Information hiding
Process of making certain items inaccessible
Abstraction
Strategy for information hiding
Focus attention on certain details
Ignore others
Encapsulation
Packaging mechanism
In object oriented paradigm
Objects are the encapsulation
Objects
Complete entities….. Data + Functions
Highly independent with a means for interaction
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 25 -
In tro ductio n to C
++
Summary
A program consists of a collection of
Data
Functions
Programming methodology characterized by
How data and functions are encapsulated
Procedural
Functional
Logical
Object Centered
Copyright 2006 Oxford Consulting, Ltd
1 January 2006
- 26 -