CS 596 OODP
Introduction to OO
[To Lecture Notes Index]
San Diego State University -- This page last updated August 30, 1995
Contents of Intro Lecture
- References
- Introduction
- History and Languages
- Methodologies
- Meyer's Criteria for Evaluating for Modularity
- Principles for Software Development
- Simple C++ IO
- IO Operators
- Some IO Goodies
- File IO
- How to find out more about Streams
- What is Object-Oriented Programming
- Language Level Definition
- Conceptual Level Definition
C++ Primer 2nd Ed., Lippman, Addison Wesley, 1991, Appendix A
Object-Oriented Software Construction, Bertrand Meyer, Prentice Hall,
1988
Object-Oriented Software Development, Mark Lorenz, Prentice Hall, 1993,
page 185
The C++ Programming Language, 2nd Ed., Stroustrup, Addison Wesley, 1991,
chapter 10 Streams
1967 Simula
1970 to 1983 Smalltalk
1979 Common LISP Object System
1980 Stroustrup starts on C++
1981 Byte Smalltalk issue
1983 Objective C
1986 C++
1987 Actor, Eiffel
1991 C++ release 3.0
199x Volume of OO books/articles
deforests Oregon
1995 Java
1983 to 1989 Language books with OO concepts
1989 to 1992 Object-oriented design books
1992 to present Object-oriented methodology books
Other Languages
Java
Self
Python
Perl
Prograph
Modula 3
Oberon
Smalltalk Venders
ParcPlace, Digitalk, Quasar
Prolog++
Ada 9X
Object Pascal (Delphi)
Object X, X = fortran, cobal, etc.
Approach to developing software
Methodologies encompass
- Step-by-step methods
- Graphical notation
- Documentation techniques
- Principles, guidelines, policies
Object-Oriented Design (OOD)
- Booch
Object-Oriented Systems Analysis (OOSA)
- Shlaer & Mellor
Object Modeling Technique (OMT)
- Rumbaugh et al.
Object-Oriented Analysis (OOA)
- Coad & Yourdon
Hierarachial Object Oriented Design (HOOD)
- European Space Agency, HOOD Working Group
Responsibility-Driven Design
- Wirfs-Brock et al.
Object-Oriented Software Engineering (Objectory)
- Jacobson
Fusion
What is the Big Deal?
Category | # of Programmers | Duration | Size (loc) |
Trivial | 1 | 1-4wks | 500 |
Small | 1 | 1-6mos | 1-2K |
Medium | 2-5 | 1-2yrs | 5-50K |
Large | 5-20 | 2-3yrs | 50-100K |
Very Large | 100-1000 | 4-5yrs | 1M |
Extremely large | 2000-5000 | 5-10yrs | 1-10M |
Need better ways to develop software
Need to develop better software
Software Crises
Manager's View
- Behind schedule
- Over budget
- Foreign competition
Customer's View
- Buggy software lacks features
- Too expensive
- Why can't you make this "little" change?
Programmer's View
- Multiple platforms
- Unix
- PowerPC
- Dos
- Macintosh
-
- Client/Server Applications
-
- GUIs
- X Windows
- Motif
- Microsoft Windows
Decomposability
Decompose problem into smaller subproblems
that can be solved separately
Example: Top-Down Design
Counter-example: Initialization Module
Meyer's Criteria for Evaluating for Modularity
Composability
Freely combine modules to produce new systems
Examples: Math libraries
Unix command & pipes
Meyer's Criteria for Evaluating for Modularity
Understandability
Individual modules understandable by human reader
Counter-example: Sequential Dependencies
Meyer's Criteria for Evaluating for Modularity
Continuity
Small change in specification results in:
Changes in only a few modules
Does not affect the architecture
Example: Symbolic Constants
const MaxSize = 100
Meyer's Criteria for Evaluating for Modularity
Protection
Effects of an abnormal run-time condition is confined to a few modules
Example: Validating input at source
KISS Keep it simple, stupid
Supports:
-
- Understandablity
- Composability
Decomposability
Small is Beautiful
Upper bound for average size of an operation[1]
Language | Lines of Code |
Smalltalk | 8 |
C++ | 24 |
Supports:
- Decomposability
- Composability
- Understandability
Applications of Principles
First program:
- #include <iostream.h>
-
- void main()
- {
- cout << "Hello World\n";
- }
Grow programs
- Start with working program
-
- Add small pieces of code and debug
-
-
Learning new language features
Look it up first
Test the new feature in a separate small program
#include <iostream.h>
void main()
{
int accessCode;
float transactionAmount;
char userName[50];
cout << "Please enter:
your name, access code and amount\n";
cin >> userName>> accessCode>> transactionAmount;
cout << "Your input: ";
cout << userName
<< accessCode
<< transactionAmount
<< endl ;
}
cin
- Removes all white space
- Accepts multiple input values in order - left to right
- Returns false when reaches end-of-file condition
endl
- add "\n" and flush output buffer
More IO
Predefined Streams
- cin
- standard input
-
- cout
- standard output (buffered)
-
- cerr
- standard error
-
- clog
- standard error, but buffered
Problems with cin
#include <iostream.h>
void main()
{
int integerInput;
for (int K = 0; K < 8;K++)
{
cin >> integerInput;
cout << integerInput;
};
}
Input Output
1 2 3 4.5 6 7 8 9 12344444
get(char& ch);
- Removes a single character from the input stream and places it in
ch
get(char *dest, int length, char delim = '\n');
- Stores characters in the array dest, until
- it encounters the delimiter, delim
-
- or it has read length -1 characters
-
- or reaches end of file
- dest is terminated with a null character
-
- delim is not removed from the input stream
put(char& ch);
- Places a single character into the output stream
"get(char&)" Example
#include <iostream.h>
void main()
{
char userInput;
while (cin.get(userInput))
cout.put(userInput);
}
"get()" Example
#include <iostream.h>
void main()
{
int userInput;
while ((userInput = cin.get()) != EOF)
cout.put(userInput);
}
Using the ">>" operator
#include <iostream.h>
void main()
{
char userInput;
while ( cin >> userInput )
cout << userInput;
}
More Get
#include <iostream.h>
void main()
{
char buffer[100];
cin >> buffer; // Not safe, can overflow
cin.get(buffer, 100, '\n'); // Safe
char next;
if ( (cin.get(next) && next) != '\n') {
// If reach here input line was longer than
// 99 characters
}
}
More Operators You Should Know About
getline( char *buffer, int limit, char delimiter = '\n')
- Reads either limit-1 characters, or to the delimiter, which ever comes
first. Places characters read in buffer, Adds null character to buffer. Does
not flush input to next line.
read( char* buffer, int size)
- Reads the size number of bytes from the input, places them in
buffer
gcount()
putback(char c)
peek()
ignore( int limit = 1, int delimiter = eof)
- Disregards up to limit characters in input stream, stops if encounters
delimit character
#include <iostream.h>
void main() {
char buffer[10];
char overFlow[10];
cin.getline(buffer, sizeof(buffer));
cin.getline(overFlow, sizeof(buffer), 'x');
cout << buffer<<'\t'
<< overFlow << endl;
}
Manipulators - Formating
#include <iostream.h>
#include <iomanip.h>
void main()
{
int x;
x = 8;
cout << setw(12) << x << endl;
cout << oct << x << '\t'
<< x
<< endl; // 8 in octal is 10
cout << hex << x << endl;
}
Output
8
10 10
8
Other manipulators
cout << flush // flush io buffer
cout << endl // insert newline (\n) and then flush buffer
cout << ends // insert a null(0) character
// used for array output with strstreams
Some
Common Problems
#include <iostream.h>
#include <iomanip.h>
void main()
{
char name[10];
char address[10];
cout << "Input your name: "; // buffered output!
cin >> name; // overflow of name!
cout << "Your input: ";
cout << name << endl;
}
#include <iostream.h>
void main()
{
char name[10];
char address[10];
cin.tie(cout);// cout is flushed if cin needs input
cout << "Input your name: ";
cin >> setw(sizeof(name)) >>name;
cout << "Your input: ";
cout << name << endl;
cin.tie(0); // unties cin and cout, makes cin faster
}
iostream header files
iostream.h
- Declares basic io features
- Do not use with stdio functions (scanf, etc)
fstream.h
- Basic file io
strstream.h
- io for arrays
iomanip.h
- Declares manipulators, which are values you insert into or extract from
iostreams to have different effects
stdiostream.h
- Allows use of iostreams (cin, cout) and stdio functions
-
- Some compilers require that you call
- cin.sync_with_stdio()
-
- if you mix stdio functions (scanf, etc) with iostreams
-
stream.h
- For backward-compatibility only
See the above files in /opt/SUNWspro/SC3.0.1/include/CC on rohan for more
information about the contents of these files.
File Streams (in fstream.h)
- ifstream
- input file
-
- ofstream
- output file
-
- fstream
- input/output file
fstream with File Modes
#include <iostream.h>
#include <fstream.h>
void main()
{
fstream inputFile("test.cc", ios::in);
if (!inputFile)
cerr << "Could not open 'test.cc'";
fstream outputFile("backup.cc", ios::out);
if (!outputFile)
cerr << "Could not open backup.cc'";
char c;
while (outputFile && inputFile.get(c))
outputFile .put(c);
}
File Modes
app
- Open file at the end of the file
- File is write only
ate
- Open file at the end of the file
in
- Open file as read only
out
- Open file as write only
- if file already exists, delete contents
trunc
- If file already exists, delete contents
nocreate
- If file does not already exists, open will fail
noreplace
- If file already exists, open will fail
ifstream and ofstream - Implicit File Modes
#include <iostream.h>
#include <fstream.h>
void main()
{
ifstream inputFile("test.cc");
if (!inputFile)
cerr << "Could not open 'test.cc'";
ofstream outputFile("backup.cc");
if (!outputFile)
cerr << "Could not open 'backup.cc'";
char c;
while (outputFile && inputFile.get(c))
outputFile.put(c);
}
File Position
fstream keeps track of the current location of the file
The current location can the changed
#include <iostream.h>
#include <fstream.h>
void main()
{
fstream readWriteFile("test.cc",
ios::in | ios::out);
if (!readWriteFile)
cerr << "Could not open 'test.cc'";
readWriteFile.seekg(-10,ios::end);
char x;
readWriteFile >> x;
cout << x;
streampos here = readWriteFile.tellp();
readWriteFile << "Hi mom";
readWriteFile.seekg(here);
}
Note:
readWriteFile.seekg(x, ios::cur)
// positions x bytes from current location
readWriteFile.seekg(x, ios::beg)
//positions x bytes from beginning of file
Read
C++ Primer 2nd Ed., Lippman, Addison Wesley, 1991, Appendix A
The C++ Programming Language, 2nd Ed., Stroustrup, Addison Wesley, 1991,
chapter 10 Streams
When you know more about classes read the following file on Rohan
/opt/SUNWspro/SC3.0.1/include/CC/iostream.h
Language Level Definition
Conceptual Level Definition
Object-Oriented Programming
Object
Object-Oriented Programming
Language Level Definition
Class
Object-Oriented Programming
Language Level DefinitionInheritance
Object-Oriented Programming
Abstraction
"Extracting the essential details about an item or group of items, while
ignoring the unessential details."
Edward Berard
"The process of identifying common patterns that have systematic variations; an
abstraction represents the common pattern and provides a means for specifying
which variation to use."
Richard Gabriel
Example
Pattern: Priority queue
Essential Details: length
items in queue
operations to add/remove/find item
Variation: link list vs. array implementation
stack, queue
Object-Oriented Programming
Conceptual Level DefinitionEncapsulation
Enclosing all parts of an abstraction within a container
Example
Object-Oriented Programming
Conceptual Level DefinitionInformation Hiding
Hiding parts of the abstraction
Example
Object-Oriented Programming
Conceptual Level DefinitionHierarchy
Abstractions arranged in order of rank or level
Class Hierarchy
Object-Oriented Programming
Conceptual Level DefinitionHierarchy
Object Hierarchy