Professional Documents
Culture Documents
with C++
Chapter 1: Introduction to OOP
ECE3206 Chapter 1
1 of 42
Introduction to Programming
What are computer programs?
Algorithms + Data Structures = Programs
ECE3206 Chapter 1
2 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
3 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
4 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.
ECE3206 Chapter 1
5 of 42
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.
6 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
7 of 42
ECE3206 Chapter 1
8 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
9 of 42
ECE3206 Chapter 1
10 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
11 of 42
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.
ECE3206 Chapter 1
12 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
13 of 42
ECE3206 Chapter 1
14 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
15 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
16 of 42
ECE3206 Chapter 1
17 of 42
......
}
int applyUni ( ) {
......
if (cgpa > 130) { ...... }
......
}
int regStud ( ) {
......
}
18 of 42
ECE3206 Chapter 1
19 of 42
ECE3206 Chapter 1
20 of 42
STUDENT
Name
DOB
SSID
-------------createApp()
accptOffer()
rejOffer()
payFees()
UNIVERSITY
Data (Attributes)
Methods
ECE3206 Chapter 1
Faculty
Intake
Address
------------sendApp()
sendOffer()
sendRej()
regStud()
21 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
22 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
23 of 42
Object name
AgentX
Attributes of
the object
Name = XYZ
Height = 180
Weight = 90
Gender = Male
.
Methods of the
object
getGender ( )
setHeight ( )
setWeight ( )
ECE3206 Chapter 1
24 of 42
OO Classes
A class is a blueprint/template used to create object.
Person p1, p2
Classes are user defined data types and behave like the builtin 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
25 of 42
Abstraction
Encapsulation
Separation
Composition
Generalization
ECE3206 Chapter 1
26 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
27 of 42
ECE3206 Chapter 1
28 of 42
ECE3206 Chapter 1
29 of 42
Data Encapsulation
Encapsulation is the principle of hiding an objects 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 objects data structure.
This provides some level of data security and reliability as illegal
operations are avoided (unless the objects own method is illegal)
ECE3206 Chapter 1
30 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
31 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
32 of 42
ECE3206 Chapter 1
33 of 42
Variables
double Height
double Weight
char Gender
char Name[200]
Height = 165.0
Weight = 70.5
Gender = M
Name = Tom
Methods
getName ()
getGender ()
getName ()
getGender ()
ECE3206 Chapter 1
34 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.
35 of 42
Polymorphism
36 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
37 of 42
ECE3206 Chapter 1
38 of 42
C vs. C++
C has many legacy applications and strengths that
make it so popular even today.
39 of 42
ECE3206 Chapter 1
40 of 42
ECE3206 Chapter 1
41 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.
ECE3206 Chapter 1
42 of 42