Professional Documents
Culture Documents
04 OO Thinking
04 OO Thinking
Thinking
2
z
Today’s Lesson
z
Lesson Outcomes
z
Structured Programming
▪ Repetition/Iteration
▪ Selection
5
z
Structured Programming in the real world
Add sugar
COE 254: Object-Oriented Prgramming
Add milk
Stir
6
z
Structured Programming in the real world
Stack dishes by sink
Wash dish
COE 254: Object-Oriented Prgramming
Drain sink
Wipe counter
7
z
Structured Programming in the real world
▪ Selection Check
weather
z
Procedural Programming
z
Procedural Programming
▪ Functions essentially are like black boxes which recieve input and produce
outputs
COE 254: Object-Oriented Prgramming
output
input function
10
z
Procedural Programming
▪ For this reason the function was adopted as a way to make programs more
comprehensible to their human creators.
COE 254: Object-Oriented Prgramming
z
Procedural Programming
z
Procedural Programming
▪ Function Parameters/Arguments
z
Advantages of Procedural Programming
manipulation of memory
14
z
Disadvantages of Procedural Programming
z
Problems with Procedural Programming
z
Unrestricted access to global data
▪ In a procedural program, one written in C for example, there are two kinds
of data.
▪ When two or more functions must access the same data—and this is true of
COE 254: Object-Oriented Prgramming
the most important data in a program—then the data must be made global.
z
Unrestricted access to global data
Global
Variables
Accessible by any Function
Local Local
Variables Variables
Function A Function B
18
z
Unrestricted access to global data
z
Unrestricted access to global data
z
Unrestricted access to global data
z
Unrestricted access to global data
▪ When data items are modified in a large program it may not be easy to tell
which functions access the data, and even when you figure this out,
modifications to the functions may cause them to work incorrectly with other
global data items.
z
Poor representation of real-world objects
▪ In the physical world we deal with objects such as people and cars. Such
objects aren’t like data and they aren’t like functions.
COE 254: Object-Oriented Prgramming
z
Poor representation of real-world objects
z
Poor representation of real-world
objects
▪ Behavior is something a real-world
object does in response to some
stimulus.
z
Object-Oriented World
▪ The fundamental idea behind object-oriented languages is to combine into a single unit
both data and the functions that operate on that data. Such a unit is called an object.
▪ An object’s functions, called member functions in C++, typically provide the only way to
access its data.
▪ If you want to read a data item in an object, you call a member function in the object. It
will access the data and return the value to you. You can’t access the data directly.
▪ The data is hidden, so it is safe from accidental alteration. Data and its functions are said
COE 254: Object-Oriented Prgramming
▪ Data encapsulation and data hiding are key terms in the description of object-oriented
languages.
26
z
Object-Oriented World
▪ If you want to modify the data in an object, you know exactly what functions
interact with it: the member functions in the object.
▪ No other functions can access the data. This simplifies writing, debugging,
and maintaining the program.
z
Object-Oriented World
Object
Data
Function
Function
Object Object
COE 254: Object-Oriented Prgramming
Data Data
Function Function
Function Function
28
z
Object-Oriented World
▪ What are called member functions in C++ are called methods in some other
object-oriented (OO) languages.
the object.
29
z
Object-Oriented World
▪ It is only when you look at the larger context that you can determine whether a
statement or a function is part of a procedural C program or an object-oriented
C++ program.
30
z
Object-Oriented Programming:
Objects
▪ This results from the close match between objects in the programming
sense and objects in the real world.
31
z
Object-Oriented Programming:
Classes
z
Object-Oriented Programming:
Inheritance
▪ In our daily lives, we use the concept of classes divided into subclasses.
▪ The vehicle class is divided into cars, trucks, buses, motorcycles, and so
on.
▪ In C++ the original class is called the base class; other classes can be
COE 254: Object-Oriented Prgramming
defined that share its characteristics, but add their own as well. These are
called derived classes.
33
z
Object-Oriented Programming:
Base Class
Inheritance
Feature A
Feature B
COE 254: Object-Oriented Prgramming
Feature B Feature B
Feature C Feature D Feature E Feature F
Derived Classes
34
z
Object-Oriented Programming:
Reusability
▪ Once a class has been written, created, and debugged, it can be distributed
to other programmers for use in their own programs.
z
Object-Oriented Programming:
Polymorphism and Overloading
OOP.
36
z
Object-Oriented Programming:
Summary
▪ Fundamentally, an object can be
Function 1
defined as an entity which contains Object 1 Object 2
both data and behaviour
Data 1 Data 1
▪ Objects combine data and
behaviour in one entity providing a Global Data 2 Data 2
more realistic approach data
Function 1 Function 1
▪ This addresses the procedural
problem of uncontrolled access to
Function 2 Function 2
COE 254: Object-Oriented Prgramming
global data
Function 2
▪ Access to methods and data in an
object can be controlled
Procedural Object-Oriented
Paradigm Paradigm
37
z
Procedural versus OO Programming
z
C++ and C
Any Questions?
z
The End
Contact: tsadjaidoo@knust.edu.gh