Title: ObjectOriented Programming in C
1Object-Oriented Programming in C
2Prerequisites
- Access to a C compiler
- C programming, or good C programming skills
Evaluation
- Midterm (20)
- Projects (50)
- No late projects
- The projects must compile (ANSI) and execute
- Final Exam (30)
3Communications
e-mail jsowers_at_csc.com Tel 609.383.8186 Fax
609.646.1154 Web http//www.cis.njit.edu/jsowers
4Major Topics Covered
- Object-Oriented Programming
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
- Object-Oriented Design
- C Syntax
5Good Programming Principles
- Modular programming
- Divide and conquer
- Reusable code
- Example When an electrician needs a light
switch, they go to a bin and pick one out - Extensibility
- Extending the functionality of a piece of code
does not require the calling code to be rewritten
6Design Methodologies
- Monolithic Programming
- Procedural Programming
- Top-Down or Bottom-Up
- In functional decomposition the whole system is
characterized by a single function, and then the
function is decomposed into a set of functions in
a process of stepwise refinement.
7Design Methodologies (contd)
- Top-Down Design
- Modules with well-defined semantics that can be
directly implemented. - Data plays a secondary role
- Procedures own the data
- The data structures generated by functional
decomposition do not necessarily reflect the
states of abstraction in the application.
8Conquer the World
Conquer Europe
Conquer America
Conquer Asia
...
Conquer S. America
Conquer N. America
...
...
Get spouse to take out trash
9Design Methodologies (contd)
- Object-Orientation
- Objects are the building blocks of the program.
- Objects represent real-world abstractions within
the application.
10Design Considerations
- Quality
- Schedule
- Features
- Choose any 2 (and 1 has top priority!)
- if quality schedule are important then of
features are limited - if tight schedule large of features have
priority then quality suffers
11Myths that hurt Design
- There is no time to design the application
- Scheduling is bogus
- Estimates are certain
- 8 hours 1 day
- Keep two sets of schedules
- Kick up the pressure
- If the project is behind, add programmers
- If we leave Tom alone, it will get done
- Interim releases are a waste of time
- Well catch up / Well fix it when theres time
12Fundamental Concepts
13Abstraction Focusing on What is Important
- Used to identify the objects involved in a
particular application - Allows recognition of what is important and what
may be ignored - Tends to involve user (domain-expert) in the
design process - One object may have many differing abstractions -
dependent on the application - Possibly the hardest part of O-O
14Encapsulation Hiding your private parts
- Based on two principles
- Process Hiding
- Data Hiding
- The user does NOT need to know what the data
looks like - The user ONLY needs to know specific interface
procedures
In C, one used static for crude data hiding
How many people need to understand
nucleosynthesis to get a tan at the beach?
15Polymorphism Same Behavior, Different
Implementation
- The ability of objects related through
inheritance to respond differently to the same
message or function call. - Same behavior-Same concept different
implementations - calculate_work
- Carpenter
- Programmer
16Inheritance Attaining Reuse
- Expresses commonality among objects
- Allows code reusability
- Highlights has-a/is-a relationships
- Implementation Inheritance
- inherits common code and adds specifics
- Interface Inheritance
- inherits same interface, but changes
implementation
17Encapsulation
- Allows Modularity
- Controlled access to data
- Separates implementation from interface
- It extends the built-in types
18Encapsulation (continued)
- Data Hiding
- The user does not need to know what the data
looks like, that is, how it is structured - Process Hiding
- The set of actions which may be performed on the
data are described in the same module
19Encapsulation (continued)
- Built-in data types in procedural languages (int,
float) provide data abstraction - Data representation is hidden (the user does not
need to knows how the float is stored in memory) - Data is encapsulated with the operations which
are used to act on the data (e.g. , ). You
cant get at the data directly (but it can be
hacked).
20Encapsulation (continued)
- Procedural languages do not allow creation of new
abstract data types. - Object-oriented languages do.
- So you can create a new data type whose data
details are hidden, and which contains actions
to allow access and manipulation of the data.
21Why is Data Hiding Important?
- Lets say you have a data structure called date
which has two digits for the year field - Lets say youre working in a procedural
programming environment, and many functions use
this data structure. - If you change the date field from two digits to
four digits, all of these functions will have to
be changed to work correctly. - Remember Y2K ?
22Why is Data Hiding Important (continued)
- In an object oriented environment, not all of the
references to date will need to be changed - The internal makeup of the data is not important
to outside users - Another example is changing the definition of
float from 32 to 64 bits along with a processor
upgrade
23Process Hiding
- segregation of source code in functions lends
itself to process hiding - the user of the function does not need to know
how the function performs its task - the user only needs to know the name of the
function, what task it performs, and its
interface (input/output) - this is a step toward reusable code
24Why is encapsulation important?
- The data and the functions which are used to
manipulate the data are segregated into one unit - This provides a new level of modularity, above
that of procedure-oriented programming, where
only the functions are segregated - So the benefits of modularity are increased
25Why is encapsulation important (continued)
- Data encapsulated within an object is operated
on by functions within the object - So encapsulation hides data from external
functions - This prevents unrelated code from causing errors
within the encapsulated code - For example, it is difficult for external sources
to mess up floating point operations
26Which paradigm is best?
- Procedure oriented programming supports
modularity, and reusable code (to some extent) - Object oriented programming supports modularity,
reusable code (strongly), and extensibility
27Classic Example
- Write a program to maintain a list of shapes
created by the user, and print the shapes when
needed. - The shapes needed in the application are
- points
- lines
- rectangles
- circles
- etc...
28 29Conventional Programming
- For each Shape in ShapeList
- if (Shape.type is circle) then
- DrawCircle(Shape)
- else if (Shape.type is rectangle) then
- DrawRectangle(Shape)
- else if (Shape.type is point) then
- DrawPoint(Shape)
- else if( Shape.type is line) then
- DrawLine(Shape)
-
30Using Polymorphism
- For each Shape
- Shape.Draw()
31Compiling/Debugging
- man CC
- CC -g -o helloWorld helloWorld.cpp
- compiles helloWorld.cpp
- creates helloWorld executable
- includes information for the debugger
- debuggers
- dbx - tty interface
- debugger - X windows interface
- gdb
32Assignment 0
- // Hello World program, in C
- include ltiostream.hgt
- int main()
-
- cout ltlt "Hello World! ltlt endl
- return 0