Title: Introduction to ObjectOriented Paradigm
1Introduction to Object-Oriented Paradigm String
Objects
2Function-Oriented Programming
- Views a program as a collection of functions with
a controller - Uses design strategies to isolate (to the extent
possible) each function in a separate component
of the program - Uses functions, procedures, subroutines, etc. to
implement the primary structure - Uses structure charts to describe the program
structure
3Object-Oriented Programming
- Views a program as a collection of cooperating
objects - Uses design strategies to isolate objects in
separate component of the program - Uses classes to implement the primary program
structure - Uses object diagrams to describe the program
structure
4Name the objects in this classroom
5An Object
- has a state which is described by attributes
- has behavior which is defined through methods
- has an identity which is a name
6Example Object Door
Attributes? Behavior? Identity?
7OOP
- Object-oriented programming (OOP) models
real-world objects with software counterparts. - Takes advantage of class relationships
- Objects of a certain class have common
characteristics - Example class vehicles
8Objects of Type Vehicle
Land Vehicle
Vehicle
Car
Air Vehicle
9Major Elements of OO Model (Booch)
- Abstraction
- Describing the essential characteristics of an
object that distinguish it from other kinds of
objects - Encapsulation (information hiding)
- Hiding all the details of an object that do not
contribute to its essential characteristics - Modularity
- Decomposition of domain into a set of cohesive
and loosely coupled modules - Hierarchy
- Ranking or ordering of abstractions
10Abstraction
Process of extracting relevant properties of an
object while ignoring nonessential details
Major Repairs?
Price?
11Encapsulation
Separating the aspects of an object into external
and internal aspects.
12Modularity
Complex systems are modularconstructed by
combining simpler working components.
13Hierarchy
14Basic Concepts of OOClasses and Objects
- A class is a definition of a general type of
object (attributes and methods) - An object is an instance of a class
- Analogy
- Class is a blueprint
- Object is an house built by the blueprint
15Example A Circle Class
- Attributes (describe state)
- radius
- location of center (x, y)
- Methods (define behavior)
- set its center
- set its radius
- draw itself
- calculate its area
16Example A Circle Object
Name theCircle (identity)
radius
centerX
centerY
SetCenter
(attributes)
SetRadius
(methods)
Draw
Area
17Example A Circle Object
Declare theCircle to be a Circle object
//This process is called instantiation begin SetR
adius of theCircle to 3 SetCenter of theCircle
to (2.5, 4.5) Draw theCircle display Area of
theCircle end
18First Look at a Class
- Character strings are commonly used in many
applications - There are some standard operations that one would
like to perform on strings - We will look at a class for character strings
that is defined in C in the standard library.
19String Class
- A string is a sequence of characters treated as a
single object. - A string constant is a sequence of zero or more
characters enclosed in double quotes. - C provides a standard library that defines a
class that can be used to create and manipulate
string objects - To use the string class, include the following
directive - include ltstringgt
20Creating Objects
- Create (or instantiate) an object of the class
string the same way you declare variables - string greeting Hello
- The above statement creates an object of the
class string and initializes it to a value
21Attributes of string Objects
- The characters that comprise the string
- The number of characters in the string
- string message Help!
message
H e l p !
Length
5
22Behaviors of String Objects
- When you want an object to do something, you can
use operators that are defined for the string or
send it a messages that it understands. - There are many operators and messages that string
objects understand. - The operations include
- Assignment
- Extraction gtgt
- Insertion ltlt
- Concatenation
- Appending
23Assignment
string message1 Hello! string message2
message2 message1
24Examples
- string prompt
- string firstName, lastName
- prompt Enter your first name and last name
- cout ltlt prompt
- cin gtgt firstName gtgt lastName
25Extracting strings from cin
- Skips initial white space
- Reads a white space terminated word
- What is read by previous code with each of the
following sets of input? - Set 1 John Doe
- Set 2 Doe John
- Set 3 JohnDoe
26The Concatenation Operation
- string firstName Tiger
- string lastName Wood
- string fullName
- fullName firstName lastName
27The Append Operation
- The append operation () adds characters to the
end of a string. - Examples
- string message Help
- message !
- message Wow
- string who Me
- who message
28C Syntax for Sending Messages
Object name Message type Optional
list of arguments
(contents of the
message)
Identifier.Message(Arg1, Arg2, Argn)
29The size message
- string date
- int i
- date August 28, 2000
- i date.size()
- cout ltlt i
30The substr message
- Returns a substring of a string
- Has two arguments
- Starting position of substringfirst position is
position 0 - Length of substring
- Technicalities
- A starting position beyond the end is an error
- If the length is too long, just return what is
there
31Example
- string somestring Merry Christmas
- string otherstring
- otherstring somestring.substr(0,5)
- otherstring somestring.substr(5,2)
- somestring otherstring.substr(0,4)
- somestring otherstring.substr(15,10)
32The find message
- Two arguments
- A substring to find
- The position in the string to start the search
- Returns the position within the string where the
substring is found - If substring not found, returns a number that is
larger than any legal position in the string.
33Example
- string spockSays Live long and prosper!
- int i
- i spockSays.find(and, 0)
- cout ltlt i is ltlt i ltlt endl
- i spockSays.find(and, 5)
- cout ltlt i is ltlt i ltlt endl
- i spockSays.find(and, 11)
- cout ltlt i is ltlt i ltlt endl
- (Note See next slide for details on MSV C
behavior of this function.)
34Example (Cont..)
- string spockSays Live long and prosper!
- unsigned long int i
- i spockSays.find(and, 11)
- cout ltlt i is ltlt i ltlt endl
- (Note i must be declared unsigned long int or
MSV C will return 1.)
35The getline auxillary function
- Reads an entire line into a string
- Not a message, but is an auxillary function
- Arguments
- Stream to read from
- String to receive the input line
- Character that terminates the extraction
36Example
- string inputLine
- cout ltlt Please enter some text
- getline(cin, inputLine, \n)
- cout ltlt Your input is \ ltlt inputLine
- ltlt \ ltlt endl
37Example Program
- Write a program to read in a date in American
format (e.g. August 28, 2000) and print the date
in international format (e.g. 28 August 2000).