chapter 01-introduction to oop
Post on 17-Jan-2016
256 Views
Preview:
DESCRIPTION
TRANSCRIPT
ECE3206 – Chapter 1 1 of 42 ECE3206 – Chapter 1 1 of 42
ECE3206 – Object Oriented Programming
with C++
Chapter 1: Introduction to OOP
ECE3206 – Chapter 1 2 of 42 ECE3206 – Chapter 1 2 of 42
Introduction to Programming
What are computer programs? Algorithms + Data Structures = Programs
Computer programs are instructions for a computer. These
instructions are made up of commands that create algorithms. The
commands make use of data structures to store and retrieve values in
order to reach a certain goal.
ECE3206 – Chapter 1 3 of 42 ECE3206 – Chapter 1 3 of 42
Algorithms
An algorithm is step-by-step computational method for solving
a known problem in a finite number of steps.
Any algorithm must fulfil the following requirements:
• It should solve the given problem.
• It should be unambiguous.
• It must be able to terminate within a finite number of
steps, with a possibly computed end value.
Algorithms can be expressed in many different ways:
• Natural language (e.g. words).
• Formal language (e.g. flow charts).
ECE3206 – Chapter 1 4 of 42 ECE3206 – Chapter 1 4 of 42
Data Structures
Data structures refer to methods of storing and organizing
data in a computer so that it can be used efficiently.
Lower-level languages generally do not have support for data
structures.
High-level languages however usually have built in support /
syntax for structures such as vectors, arrays, linked list etc.
OO based languages provide opaque data structures that allow
data to be hidden from other objects.
ECE3206 – Chapter 1 5 of 42 ECE3206 – Chapter 1 5 of 42
Programming paradigms
A programming paradigm or style can differ based
on the concepts and abstractions used in the
language as well as the methods to complete a task.
Examples of paradigms include event-driven, object-oriented,
iterative, goal-driven, agent-oriented etc.
Any language can actually implement multiple paradigms – C++
and Pascal support both object-oriented and procedural
programming styles.
Differences in paradigms/styles means having to learn (or relearn)
how to implement something when changing languages.
ECE3206 – Chapter 1 6 of 42 ECE3206 – Chapter 1 6 of 42
Unstructured programming paradigm
• Earliest programming paradigm and the least restrictive.
• Programs are coded into a single ‘block’ section (i.e. main) - consists
of sequentially ordered commands, or statements, that are usually
numbered or may have labels.
Has basic flow control – loops, branching and jumps.
Sub-routines are common but procedures are not available
These sub-routines have multiple entry/exit points and no control
over where a program can jump into/out of (GOTO & JUMP
commands were common)
This freedom resulted in many programs
created as ‘spaghetti’ code.
E.g. COBOL, Basic, assembly, C, etc.
ECE3206 – Chapter 1 7 of 42 ECE3206 – Chapter 1 7 of 42
Advantages:
– Simple and easy to practice by novice programmer.
– Good for small programs; no lengthy planning
needed.
Disadvantages: - Repetition of code
- If the same statement sequence is needed at the different
locations within the program, the sequence must be
copied.
- Cumbersome maintenance
- when a certain statement is changed, all
subsequent affected statements must
be adjusted accordingly.
ECE3206 – Chapter 1 8 of 42 ECE3206 – Chapter 1 8 of 42
Procedural programming paradigm
• Procedural programming programs are broken up into smaller tasks
(procedures), variables and data structures.
• Procedures are basically a series of steps/computations that must be
performed.
At any time, procedures can be called upon in a program’s
execution, including by the procedure itself or by others.
Most programs begin with the ‘main’ procedure and proceed from
there.
E.g. FORTRAN, C, Pascal, ALGOL,
BASIC, Matlab
ECE3206 – Chapter 1 9 of 42 ECE3206 – Chapter 1 9 of 42
Advantages:
– Programs can be written in more structured
manner, which will minimize error.
– The ability to re-use the same code at different
places in the program without copying it.
Disadvantages : - Obviously more restrictive and less flexible than unstructured
programming languages.
- Difficulties in reasoning about programs sometimes hard to
match what you want the program to do and what the program
can do.
ECE3206 – Chapter 1 10 of 42 ECE3206 – Chapter 1 10 of 42
Structured programming paradigm
• Structured programming is a subset of procedural programming and is
also known as modular programming.
• Removed the GOTO/JUMP statement method of coding.
In structured programming, a big problem is divided into smaller
sub-problems that are solved independently - each is tackled by a
procedure (function).
Procedures with common functionality are grouped together into
modules .
Each module can interact through procedure calls.
ECE3206 – Chapter 1 11 of 42 ECE3206 – Chapter 1 11 of 42
Advantages:
– Localised variables allow new concepts of
programming such as recursive and iterative
functions.
– Larger data type set allows more control over the
storage of data.
Disadvantages: - No data protection in
procedures
- any other procedure is free to
access the members of
another procedure/module.
ECE3206 – Chapter 1 12 of 42 ECE3206 – Chapter 1 12 of 42
Object-oriented programming (OOP)
paradigm
• The ‘latest’ paradigm in programming style, OOP was introduced
through the language Smalltalk by Xerox.
• OOP took foothold only with development of C++ which is an
extension to C.
Note: Java & C# are also other examples of OOP languages which have rapidly
gained favor in developing portable programs. Object oriented programming is
characterized by the defining of classes of objects, and their properties.
Inheritance of properties is one way of reducing the amount of
programming, and provision of class libraries in the programming
environment can also reduce the effort required.
ECE3206 – Chapter 1 13 of 42 ECE3206 – Chapter 1 13 of 42
Main concepts in OO
– Software - collection of discrete objects with their data
and functions that model “Real World” objects.
– Object
• own predefined function/method and is responsible
for its own data.
• assigned a responsibility/task to provide a set of
service to other objects.
OO focuses on
“What can these objects do”
rather than “How they do it”
All OO objects can be easily
replaced, modified, and reused
ECE3206 – Chapter 1 14 of 42 ECE3206 – Chapter 1 14 of 42
Program design approach
Top-down design
– Essentially breaking down a larger system to gain
insight into its compositional sub-systems divide-
and-conquer.
– The sub-problems can then be split into further sub-
problems (if necessary), and so on.
Different parts of an algorithm can be refined until a level is
reached where the solution become trivially simple.
The trivial problem is then solved by implementing the
function.
ECE3206 – Chapter 1 15 of 42 ECE3206 – Chapter 1 15 of 42
Bottom-up design
– A bottom-up approach works by piecing together
smaller systems to give rise to bigger systems, thus
making the original systems, sub-systems of the
emergent system.
The individual base elements of the system are first
specified in great detail.
These elements are then linked together to form larger
subsystems, which then in turn are linked, sometimes in many
levels, until a complete top-level system is formed.
ECE3206 – Chapter 1 16 of 42 ECE3206 – Chapter 1 16 of 42
Procedural vs. OO
In procedural design, a program is created by
determining
– The sequence of tasks you want your program to
accomplish
– Then figuring out the steps or procedures that are
needed to accomplish the task
Consider the data to be processed by the procedure
The code is organized into several functions, and each function
typically represents a sub-task.
The program is typically modeled with a data flow diagram
ECE3206 – Chapter 1 17 of 42 ECE3206 – Chapter 1 17 of 42
ECE3206 – Chapter 1 18 of 42 ECE3206 – Chapter 1 18 of 42
Procedural Design Example int main () {
......
clApply = applyUni (“Tom”,15, …);
......
}
int applyUni ( … ) {
......
if (cgpa > 130) { ...... }
......
}
int regStud ( … ) {
......
}
Resulting program are multiple
procedures or “functions” that
perform the tasks as outlined in the
DFD. Each is called when required
from the MAIN body of the program.
ECE3206 – Chapter 1 19 of 42 ECE3206 – Chapter 1 19 of 42
In object oriented design, the same program is
created by determining:
What type of objects the program needs to accomplish its goal
What each objects’ responsibility is and services that it can
provide to other objects.
To provide this service, the objects must have their own data
and the related member functions.
Using the previous example, two ‘objects’ are actually interacting
with each other – the student and the university these are the
objects we require in the program.
ECE3206 – Chapter 1 20 of 42 ECE3206 – Chapter 1 20 of 42
Each object must ensure that it can provide the
services required from it
For each kind of object, you determine what information
(attribute) the object needs to contain, and what operation
(method) the object can perform.
Asking the object to activate its operation can also be viewed
as sending a ‘message’ to the object to perform the operation.
Each message interacts with a method in the object
The program is then developed by creating the objects and
invoking the object’s method.
ECE3206 – Chapter 1 21 of 42 ECE3206 – Chapter 1 21 of 42
STUDENT UNIVERSITY
Name
DOB
SSID
--------------
createApp()
accptOffer()
rejOffer()
payFees()
Faculty
Intake
Address
-------------
sendApp()
sendOffer()
sendRej()
regStud()
Data (Attributes)
Methods
ECE3206 – Chapter 1 22 of 42 ECE3206 – Chapter 1 22 of 42
Fundamentals of OO Design
Object oriented design involves the following concepts
– Classes
• Similar to struct in C but can include member
functions
– Objects
• Instance of the Class
– Data Abstraction and Encapsulation
– Composition
– Generalization and Inheritance
– Polymorphism
– Message Communication (i.e. message passing)
ECE3206 – Chapter 1 23 of 42 ECE3206 – Chapter 1 23 of 42
OO Objects
• Objects are the central idea in OO programming.
• Compare this with simple variable in procedural programming
languages (e.g. int xyz, char option).
• An object is the basic run-time entity in an object oriented
system.
• In C++ object is created by typing the class name followed by
the object name. E.g.: Person AgentX
Person is class name, AgentX is the class instance or better
known as object
ECE3206 – Chapter 1 24 of 42 ECE3206 – Chapter 1 24 of 42
Attributes of
the object
Methods of the
object
getGender ( … )
setHeight ( … )
setWeight ( … )
Name = XYZ
Height = 180
Weight = 90
Gender = Male
……….
AgentX Object name
Note : The object name (AgentX) and object attribute called
name are two different labels. One refers to the object variable
name whilst the other is the state of that particular attribute
ECE3206 – Chapter 1 25 of 42 ECE3206 – Chapter 1 25 of 42
OO Classes
A class is a blueprint/template used to create object. Person p1, p2 // Person is class. p1,p2 is object name
Classes are user defined data types and behave like the built-
in types of a programming language such as int, char, float.
A class contains all the information (data and methods) a typical
object should have, i.e. a class is an abstraction of a software entity
Person, BankAccount
Once a class has been defined, we can create any number of
objects belonging to this class.
Person ali ; Button okButton ;
When you create an object of a class (create an instance of a class),
the object gets its state initialized.
ECE3206 – Chapter 1 26 of 42 ECE3206 – Chapter 1 26 of 42
Classes (and, for that matter, their objects created)
in OOP have several characteristics that support
how they work
– Abstraction
– Encapsulation
– Separation
– Composition
– Generalization
ECE3206 – Chapter 1 27 of 42 ECE3206 – Chapter 1 27 of 42
Data Abstraction
Abstraction - a general concept formed by extracting
common features from specific examples. In
programming, abstraction is basically a simplified
representation of the entity in a software system.
It refers to the act of representing the entity with only the essential
features to function as required without including the unnecessary
details. This includes
• Data structure of a set of items.
• Operations that work on the data structure and act as an
interface of the entity.
Any entity with the above features is called an abstract data type.
ECE3206 – Chapter 1 28 of 42 ECE3206 – Chapter 1 28 of 42
Abstract Data Type
Formal definition - An abstract data type (ADT) is
characterized by the following properties:
– It exports a type.
– It exports a set of operations called its’ interface.
– Operations of the interface are the one and only access
mechanism to the type's data structure.
ECE3206 – Chapter 1 29 of 42 ECE3206 – Chapter 1 29 of 42
ECE3206 – Chapter 1 30 of 42 ECE3206 – Chapter 1 30 of 42
Data Encapsulation
Encapsulation is the principle of hiding an object’s data structure
(attributes) and only allowing access to them through a predetermined
interface.
Implementation details are hidden from external view and only the
interface member functions are exposed.
Encapsulation allows a programmer to control how an objects’
data is retrieved, used and/or manipulated.
If no method/function is provided, then it is not possible for other
objects to operate on an object’s data structure.
This provides some level of data security and reliability as illegal
operations are avoided (unless the object’s own method is illegal)
ECE3206 – Chapter 1 31 of 42 ECE3206 – Chapter 1 31 of 42
Other OO strategies
Separation
– Separate what the entity does (responsibilities) from
how it does it (methods).
– Other objects only need to know what an object does
and how to call it but need not know how it is done.
Composition
– Build complex system by assembling the simpler part
together .
– This is done by the method of:
• Association (outsource the job to other object)
• Aggregation (model the has a relationship e.g. a car has
an engine)
ECE3206 – Chapter 1 32 of 42 ECE3206 – Chapter 1 32 of 42
Generalization
– Find similar classes and group them under a single
general class together
– Promote reuse
– Generalization can be achieved using
• Hierarchy
• Genericity
• Polymorphism
ECE3206 – Chapter 1 33 of 42 ECE3206 – Chapter 1 33 of 42
Design Strategies In OOP
The diagram here shows how the object oriented
software structures is used to implement the design
strategies in order to achieve the desired software
engineering goals.
ECE3206 – Chapter 1 34 of 42 ECE3206 – Chapter 1 34 of 42
Class and Object relationship
Following is an example of a class and its instance (object)
class ‘Person’ Instance ‘p’ of the class Person
Variables
double Height
double Weight
char Gender
char Name[200]
Methods
getName (…)
getGender (…)
…
Height = 165.0
Weight = 70.5
Gender = M
Name = Tom
getName (…)
getGender (…)
…
ECE3206 – Chapter 1 35 of 42 ECE3206 – Chapter 1 35 of 42
Object inheritance
Inheritance is the process by which objects of one class acquire the
properties of objects of another class.
The concept of inheritance provides the idea of reusability.
The base class (super class) represents the generalized model of
objects you are dealing with.
The derived classes (sub classes) represent specialized objects
constructed by customizing the base class for a specific purpose.
Each derived class shares common characteristics depending on the
base class from which it is derived
i.e., cats and dogs are subclasses of a superclass called animals.
Cats and Dogs are derived classes and Animal is base class
ECE3206 – Chapter 1 36 of 42 ECE3206 – Chapter 1 36 of 42
Polymorphism
• Polymorphism means the ability to take on more than one form.
• In terms of computer programming, with polymorphism an
operation may exhibit different behavior in different instances.
The behavior depend upon the types
of object that invoke the operation
(method)
Cat->sound() dog->sound()
The function output is different
depending on the object type.
Polymorphism is made possible
with dynamic binding, where the
code associated with a given
procedure call is not known until
the time of the call at run-time.
ECE3206 – Chapter 1 37 of 42 ECE3206 – Chapter 1 37 of 42
Message Communication
An object-oriented program consists of a set of objects that
communicate with each other through their interfaces.
The process of programming in an object-oriented language
therefore involves the following basic steps:
– Creating classes that define objects and their behavior.
– Creating objects from the class.
– Establishing communication among objects.
Objects communicate with one another by sending and receiving
messages much the same way as people pass messages to one
another.
ECE3206 – Chapter 1 38 of 42 ECE3206 – Chapter 1 38 of 42
Messages and Methods
Action is initiated in object-oriented programming by a transmission of a message to an object responsible for the action.
The message is nothing but a request for an action, accompanied by any additional information (arguments) needed to carry out the request similar to a function call.
The receiver is the object to whom the message is sent. In response to the message, the receiver will invoke a method to satisfy the request.
ECE3206 – Chapter 1 39 of 42 ECE3206 – Chapter 1 39 of 42
C vs. C++
C has many legacy applications and strengths that
make it so popular even today.
Some of C’s strengths include
• Small compiled size of executable
• Fast execution speed
• Structured/modular language support
• Not strongly typed
• Allows for address manipulation/pointers for both
data and functions.
But with strength, C also has its own weaknesses
• Not strongly typed
• Incomplete compile and run-time checking
ECE3206 – Chapter 1 40 of 42 ECE3206 – Chapter 1 40 of 42
C++ shares the base of its features from C and thus
most of its strengths and weaknesses.
However, C++ does improve on C strengths by:
• Encapsulation for data security.
• Encourages reusable code.
• Support for object-oriented programming.
C++ weaknesses are mostly inherited from C (Still suffers
memory leaks (bad garbage collection))
ECE3206 – Chapter 1 41 of 42 ECE3206 – Chapter 1 41 of 42
What’s new/different in C++?
Referencing:
C allows passing arguments by value but C++ allows passing by
value or by reference.
Scope referencing operator:
Scope access (resolution) operator (::) allows access to global
names even when overwritten by a local name.
NEW and DELETE operators:
Operators that allow dynamic storage allocation and deallocation
ECE3206 – Chapter 1 42 of 42 ECE3206 – Chapter 1 42 of 42
Classes:
Extensions to pre-defined types available that allows for new
features such as inheritance etc.
Abstract classes:
Classes with at least one virtual function
Virtual functions:
Ability for a derived classes to provide different versions of a base
class function
Templates:
Generic or parameterized types that allow you to construct a
family of related functions or classes.
top related