SDSU 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

  1. References
  2. Introduction
    1. History and Languages
    2. Methodologies
    3. Meyer's Criteria for Evaluating for Modularity
    4. Principles for Software Development
  3. Simple C++ IO
    1. IO Operators
    2. Some IO Goodies
    3. File IO
    4. How to find out more about Streams
  4. What is Object-Oriented Programming
    1. Language Level Definition
    2. Conceptual Level Definition

References


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

Introduction


History and Languages


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.

Methodologies


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 ProgrammersDuration Size (loc)
Trivial11-4wks500
Small11-6mos1-2K
Medium2-51-2yrs5-50K
Large5-202-3yrs50-100K
Very Large100-10004-5yrs1M
Extremely large2000-50005-10yrs1-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

Meyer's Criteria for Evaluating for Modularity

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






Principles for Software Development

KISS Keep it simple, stupid

Supports:
Understandablity
Composability
Decomposability


Small is Beautiful

Upper bound for average size of an operation[1]
LanguageLines of Code
Smalltalk8
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


Simple C++ IO

#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

IO Operators


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;
}

Some IO Goodies

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 IO


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

How to find out more about Streams


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

What is Object-Oriented Programming

Language Level Definition



Conceptual Level Definition






Object-Oriented Programming

Language Level Definition

Object




Object-Oriented Programming
Language Level Definition
Class



Object-Oriented Programming
Language Level DefinitionInheritance

Object-Oriented Programming

Conceptual Level Definition

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