1
Basic Concepts of
Object-Oriented Design
2
What is this Object ?What is this Object ?
There is no real answer to the question, but we’ll call it a “thinking cap”.
The plan is to describe a thinking cap by telling you what actions can be done to it.
3
Using the Object’s SlotsUsing the Object’s Slots
You may put a piece of paper in each of the two slots (green and red), with a sentence written on each.
You may push the green button and the thinking cap will speak the sentence from the green slot’s paper.
And same for the red button.
4
ExampleExample
5
ExampleExample
That test was a breeze !
6
ExampleExample
I shouldstudy harder !
7
Thinking Cap ImplementationThinking Cap Implementation
We can implement the thinking cap using a data type called a class.
class ThinkingCap {
. . .
};
8
Thinking Cap ImplementationThinking Cap Implementation
The class will have two components called green_string and red_string. These compnents are strings which hold the information that is placed in the two slots.
Using a class permits two new features . . .
class ThinkingCap { . . . char green_string[50]; char red_string[50]; };
9
Thinking Cap ImplementationThinking Cap Implementation
The two components will be private member variables. This ensures that nobody can directly access this information. The only access is through functions that we provide for the class.
class ThinkingCap { . . .private: char green_string[50]; char red_string[50];};
10
Thinking Cap ImplementationThinking Cap Implementation
In a class, the functions which manipulate the class are also listed.
class ThinkingCap {public: . . .private: char green_string[50]; char red_string[50];};Prototypes for the Prototypes for the
thinking cap thinking cap functions go here,functions go here,after the word after the word public:public:
11
Thinking Cap ImplementationThinking Cap Implementation
In a class, the functions which manipulate the class are also listed.
class ThinkingCap {public: . . .private: char green_string[50]; char red_string[50];};Prototypes for the Prototypes for the
thinking cap thinking cap member functions member functions go herego here
12
Thinking Cap ImplementationThinking Cap Implementation
class ThinkingCap {public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const;private: char green_string[50]; char red_string[50];};
Our thinking cap has at least three member functions:Our thinking cap has at least three member functions:
Functio
n bodie
s
will b
e el
sewher
e.
13
Thinking Cap ImplementationThinking Cap Implementation
class ThinkingCap {public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const;private: char green_string[50]; char red_string[50];};
The keyword The keyword constconst appears after two prototypes:appears after two prototypes:
This means that thesefunctions will not changethe data stored in aThinkingCap.
14
Files for the Thinking CapFiles for the Thinking Cap
The ThinkingCap class definition, which we have just seen, is placed with documentation in a file called thinker.h, outlined here.
The implementations of the three member functions will be placed in a separate file called thinker.cxx, which we will examine in a few minutes.
Documentation
Class definition:• ThinkingCap class definition which we have already seen
15
Using the Thinking CapUsing the Thinking Cap
A program that wants to use the thinking cap must include the thinker header file (along with its other header inclusions).
#include <iostream.h>#include <stdlib.h>#include "thinker.h"
...
16
Using the Thinking CapUsing the Thinking Cap
Just for fun, the example program will declare two ThinkingCap variables named student and fan.
#include <iostream.h>#include <stdlib.h>#include "thinker.h"
int main( ){ ThinkingCap student: ThinkingCap fan;
17
Using the Thinking CapUsing the Thinking Cap
Just for fun, the example program will declare two ThinkingCap objects named student and fan.
#include <iostream.h>#include <stdlib.h>#include "thinker.h"
int main( ) { ThinkingCap student; ThinkingCap fan;
18
Using the Thinking CapUsing the Thinking Cap
The program starts by calling the slots member function for student.
#include <iostream.h>#include <stdlib.h>#include "thinker.h"
int main( ) { ThinkingCap student; ThinkingCap fan;
student.slots( "Hello", "Goodbye");
19
Using the Thinking CapUsing the Thinking Cap
The program starts by activating the slots member function for student.
#include <iostream.h>#include <stdlib.h>#include "thinker.h"
int main( ) { ThinkingCap student: ThinkingCap fan;
student.slots( "Hello", "Goodbye");
20
Using the Thinking CapUsing the Thinking Cap
The member function activation consists of four parts, starting with the object name.
int main( ) { ThinkingCap student; ThinkingCap fan;
student.slots( "Hello", "Goodbye");
Name of the object
Name of the object
21
Using the Thinking CapUsing the Thinking Cap
The instance name is followed by a period.
int main( ) { ThinkingCap student; ThinkingCap fan;
student.slots( "Hello", "Goodbye");A
Per
iod
A P
erio
d
22
Using the Thinking CapUsing the Thinking Cap
After the period is the name of the member function that you are activating.
int main( ) { ThinkingCap student; ThinkingCap fan;
student.slots( "Hello", "Goodbye");
Name of the Function
Name of the Function
23
Using the Thinking CapUsing the Thinking Cap
Finally, the arguments for the member function. In this example the first argument (new_green) is "Hello" and the second argument (new_red) is "Goodbye".
#include "thinker.h"
int main( ) { ThinkingCap student; ThinkingCap fan;
student.slots( "Hello", "Goodbye");
Arg
umen
ts
Arg
umen
ts
24
A QuizA Quiz
How would you activate student's push_green member function ?
What would be the output of student's push_green member function at this point in the program ?
int main( ) { ThinkingCap student; ThinkingCap fan;
student.slots( "Hello", "Goodbye");
25
A QuizA Quiz
Notice that the push_green member function has no arguments.
At this point, activating student.push_green will print the stringHello.
int main( ) { ThinkingCap student; ThinkingCap fan;
student.slots( "Hello", "Goodbye");
student.push_green( );
26
A QuizA Quiz
Trace through this program, and tell me the complete output.
int main( ) { ThinkingCap student; ThinkingCap fan;
student.slots( "Hello", "Goodbye");
fan.slots( "Go Cougars!", "Boo!");
student.push_green( );
fan.push_green( );
student.push_red( ); . . .
27
A QuizA Quiz
Hello
Go Cougars!
Goodbyeint main( ) { ThinkingCap student; ThinkingCap fan;
student.slots( "Hello", "Goodbye");
fan.slots( "Go Cougars!", "Boo!");
student.push_green( );
fan.push_green( );
student.push_red( ); . . .
28
The Key Software Trend: Object Technology Objects
Reusable software components that model real world items
Meaningful software units– Date objects, time objects, paycheck objects,
invoice objects, audio objects, video objects, file objects, record objects, etc.
– Any noun can be represented as an object More understandable, better organized and
easier to maintain than procedural programming
Favor modularity
Information Hiding
Hiding the details of a function or data structure with the goal of controlling access to the details of a module or structure.
PURPOSE: To prevent high-level designs from depending on low-level design details that may be changed.
Two Approaches to Building Manageable Modules
Divides the problem into more easily handled subtasks, until the functional modules (subproblems) can be coded.
Identifies various objects composed of data and operations, that can be used together to solve the problem.
FUNCTIONALDECOMPOSITION
OBJECT-ORIENTED DESIGN
FOCUS ON: processes FOCUS ON: data objects
FindWeighted Average
PrintWeighted Average
Functional Design Modules
Main
Print Data
Print Heading
Get DataPrepare File for Reading
Object-Oriented Design
A technique for developing a program in which the solution is expressed in terms of objects -- self- contained entities composed of data and operations on that data.
Private data
<<
setf...
Private data
>>
get...
ignore
cin cout
33
OCD: Object-Centered Design
1. Identify the objects in the problem's specification and their types.
2. Identify the operations needed to solve the problem.
3. Arrange the operations in a sequence of steps, called an algorithm, which, when applied to the objects, will solve the problem.
More about OOD
Languages supporting OOD include: C++, Java, Smalltalk, Eiffel, and Object-Pascal.
A class is a programmer-defined data type and objects are variables of that type.
In C++, cin is an object of a data type (class) named istream, and cout is an object of a class ostream. Header files iostream.h and fstream.h contain definitions of stream classes.
Procedural vs. Object-Oriented Code
“Read the specification of the software you want to build. Underline the verbs if you are after procedural code, the nouns if you aim for an object-oriented program.”
Brady Gooch, “What is and Isn’t Object Oriented Design,” 1989.
36
Data Abstraction
Separation of a data type’s logical properties from its implementation.
LOGICAL PROPERTIES IMPLEMENTATION
What are the possible values? How can this be done in C++?
What operations will be needed? How can data types be used?
37
APPLICATION
0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1
REPRESENTATION
Data Encapsulation
is the separation of the representation of data from the applications that use the data at a logical level; a programming language feature that enforces information hiding.
int y;
y = 25;
38
Encapsulated C++ Data Type int
Value range: INT_MIN . . INT_MAX
Operations: + prefix - prefix + infix - infix * infix / infix % infixRelational Operators infix
TYPE int
(inside)
Representation of
int
as 16 bits two’s complement
+
Implementation of Operations
39
Abstract Data Type (ADT)
A data type whose properties (domain and operations) are specified independently of any particular implementation.
40
Application (or user) level: modeling real-life data in a specific context.
Logical (or ADT) level: abstract view of the domain and operations. WHAT
Implementation level: specific representation of the structure to hold the data items, and the coding for operations. HOW
Data from 3 different levels
41
Viewing a library from 3 different levels
Application (or user) level: Library of Congress, or Baltimore County Public Library.
Logical (or ADT) level: domain is a collection of books; operations include: check book out, check book in, pay fine, reserve a book.
Implementation level: representation of the structure to hold the “books”, and the coding for operations.
42
Composite Data Type
A composite data type is a type which
stores a collection of individual data components under one variable name,
and allows the individual data components to be accessed.
4 Basic Kinds of ADT Operations
Constructor -- creates a new instance (object) of an ADT.
Transformer -- changes the state of one or more of the data values of an instance.
Observer -- allows us to observe the state of one or more of the data values without changing them.
Iterator -- allows us to process all the components in a data structure sequentially.
43
Two Forms of Composite Data Types
Components are not organized with respect to one another.
The organization determines method used to access individual data components.
UNSTRUCTURED STRUCTURED
EXAMPLES: EXAMPLES: arraysclasses and structs
44
45
C++ Built-In Data TypesC++ Built-In Data Types
Composite
array struct union class
Address
pointer reference
Simple
Integral Floating
char short int long enum
float double long double
46
Records
A record is a composite data type made up of a finite collection of not necessarily homogeneous elements called members or fields. For example . . .
.year 1999
.maker ‘h’ ‘o’ ‘n’ ‘d’ ‘a’ ‘\0’ . . .
.price 18678.92
thisCar at Base Address 6000
47
struct CarTypestruct CarType{
int year ; char maker[10];
float price ;
} ;
CarType thisCar; //CarType variables
CarType myCar;
48
Accessing struct members
The member selection operator (period . ) is used between the variable name and the member identifier to access individual members of a record (struct or class) type variable.
EXAMPLES
myCar.year
thisCar.maker[4]
49
Valid struct operations
Operations valid on an entire struct type variable:
assignment to another struct variable of same type,
pass as a parameter to a function
(either by value or by reference),
return as the value of a function.