1 Introduction
1 Introduction
Main contents
2
11
3
11
4
11
5
11
Data
6
11
Representation of Data
7
11
Representation of Data
• Numerical data are classified into two types:
Integer
Floating-point
8
11
Representation of Data
9
11
Data types
A data type refers to the type of data that variables
hold.
The C language supports three classes of data
types:
- Primary or Basic data types
- Derived data types
- User-defined data types
10
11
Data types
11
11
• Example of Integer:
4, 6, -12, …
• Example of Array:
A list of values of interest in each month of this
year.
• Example of Struct:
Information of a student (name, age, class,
program, GPA)
12
11
13
11
14
11
15
11
Data Structure
• Data Structure is the representation of the logical
relationship between individual elements of data.
• Data Structure is defined as a mathematical or
logical model of organizing the data items into
computer memory to use efficiently.
• Data Structure refers to the study of data and
representation of data objects within the program.
16
11
18
11
19
11
21
11
23
11
Note: Different kinds of data structures are suitable for different kinds of
application
24
11
25
Object-oriented programming (OOP)
26
Object-oriented programming (OOP)
27
Object-oriented programming (OOP)
Class
Objects
28
Main Principles of OOP
29
Abstract Data Types
• Proper planning is essential to successful
implementation of programs
• Typical design methodologies focus on developing
models of solutions before implementing them
• These models emphasize structure and function of the
algorithms used
• Initially our attention is on what needs to be done, not
how it is done
31
Abstract Data Types (cont.)
• So we define program behavior in terms of operations to be
performed on data
• The details will emerge as we refine the definitions of the
operations
• Only then will implementation of those operations be carried
out
• As part of this implementation, we must choose appropriate
data structures
• A data structure is a technique of storing and organizing data
so it can be used efficiently
32
Abstract Data Types (continued)
• In our program models, data structures are described by
abstract data types (ADTs)
• An Abstract Data Type (ADT) describes the data
objects, which constitute the data structure and the
fundamental operations supported on them.
• ADTs are defined indirectly, in terms of operations to be
performed rather than in terms of its inner structure
• ADTs can then be implemented through class definitions in
an object-oriented language.
33
Abstract Data Types: Examples
• A stack is a last-in first-out (LIFO) linear structure
where items can only be added and removed from one
end
• Operations on this stack ADT might include:
◦ PUSH – add an item to the stack
◦ POP – remove the item at the top of the stack
◦ TOP – return the value of the item at the top of the stack
◦ EMPTY – determine if the stack is empty
◦ CREATE – create a new empty stack PUSH POP
34
Abstract Data Types: stacks
35
Data encapsulation
• A class is a template which implements the ADT defining the objects
that the class creates.
• Within a class, the data elements are called data members, and
member functions.
• The combination of data members and methods in a class is referred
to as data encapsulation
36
Data encapsulation
• This approach has several advantages:
• The strong link between data and operations better mimics real-world
behavior, on which program models are based
• Errors in implementation are confined to the methods of a particular class in
which they occur, making them easier to detect and correct
• Details of the implementation of the object can be hidden from other objects
to prevent side effects from occurring.
37
Inheritance
38
Inheritance
39
Inheritance
The amount of access, and level of modifications, are controlled
by specifying public, protected, or private in the derived class
header. derived class
(public inheritance)
Base
class
derived class
(protected inheritance)
derived class
(private inheritance)
40
Polymorphism
Polymorphism is the ability, in many OOLs, to create objects of
different types that respond to method calls having the same
name
They differ in that they respond according to type-specific
behavior
Which method is called depends on the time at which the
decision is made about the call
This decision is referred to as binding, and can occur in
different ways
◦ Static binding determines the function call at compile time
◦ Dynamic binding delays the decision until run time
41
Polymorphism
42
The Standard Template Library (STL)
While C++ is a powerful language in its own right, recent
additions have added even more capabilities
Of these, perhaps the most influential is the Standard Template
Library (STL)
It provides three generic entities: containers, iterators, and
algorithms, and a set of classes that overload the function
operator called function objects
It also has a ready-made set of common classes for C++, such
as containers and associative arrays
These can be used with any built-in type and with any user-
defined type that supports some elementary operations
43
The Standard Template Library (cont.)
Containers
◦ Designed to hold objects of the same type
◦ Containers are implemented as template classes whose
methods specify operations on the data in the structures as
well as the structures themselves
◦ A number of these methods are common to all containers,
while some are more specific to the container they are
defined in
◦ The data stored in containers can be of any type and must
supply some basic methods and operations
◦ This is especially necessary if pointers are involved
44
Containers in C++
Vector
List
Queue (Priority Queue)
Deque
Stack
Map/Multimap
►
Set/MultiSet
The Standard Template Library (cont.)
Iterators
◦ An iterator is an object that accesses the elements of a
container
◦ The STL implements five types of iterators
◦ Input iterators, which can read a sequence of values
◦ Output iterators, which can write a sequence of values
◦ Forward iterators, which can be read, written to, or moved forward
◦ Bidirectional iterators, which behave like forward iterators but can also move backwards
◦ Random iterators that can move freely in any direction at one time
46
The Standard Template Library (cont.)
Algorithms
◦ About 70 generic functions, known as algorithms, are
provided in the STL
◦ These perform operations such as searching and sorting
◦ Each is implemented to require a certain level of iterator
(and therefore will work on any container that provides an
interface by iterators)
◦ Algorithms are in addition to the methods provided by
containers, but some algorithms are implemented as
member functions for efficiency
47
Algorithms in C++
max_element(), max_element(iterl, iter2)
min_element(), min_element(iterl, iter2)
sort(), sort(iterl, iter2)
random_shuffle() random_shuffle(iterl, iter2)
binary_search()
for_each()
find()
►
Data Structures and OOP
• The notion of a data type is an abstraction that allows us to
hide the details of how the type is implemented
• To use effectively, we concern ourselves with the
operations that can be performed on it that make it
distinctive
• While a given programming language may incorporate
some data types, the user may have to define their own
• These new types typically have a distinct organization that
can be exploited to define the type’s behavior
• The task then is to study the structure of these types in
terms of time and space requirements so we can determine
their usefulness
49
Data Structures and OOP (continued)
50
Data Structures and OOP (continued)
51
Summary
• Data is the basic entity or fact that is used in the
calculation or manipulation process.
• Data are raw facts without context, whereas information
is data with context.
• Data Structure is defined as a mathematical or logical
model of the particular organization of data items in
computer memory so that it can be used efficiently.
• An Abstract Data Type (ADT) describes the data objects,
which constitute the data structure, and the fundamental
operations supported on them.
• The main principles in OOP include Abstraction,
encapsulation, inheritance, and polymorphism
52
55