Unit 1 (OOPS)
Unit 1 (OOPS)
Example:
1. int type
2.float type
User defined data types: The operations and values of user defined data types are not
specified in the language itself but is specified by the user.
Example:structure,union,enumeration.
By using structure,we are defining our own type by combining other data types
struct point {
int x;
int y;
};
Abstract data types: Data types such as int, float, double, long, etc. are considered to be in-
built data types and we can perform basic operations with them such as addition, subtraction,
division, multiplication, etc. Now there might be a situation when we need operations for our
user-defined data type which have to be defined. These operations can be defined only as and
when we require them. So, in order to simplify the process of solving problems, we can create
data structures along with their operations, and such data structures
Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of
values and a set of operations. The definition of ADT only mentions what operations are to be
performed but not how these operations will be implemented. It does not specify how data will
be organized in memory and what algorithms will be used for implementing the operations.
A wall of ADT operations isolated a D.S from the program that uses it.
Now we’ll define three ADTs namely List ADT, Stack ADT, Queue ADT.
1. List ADT
The data is generally stored in key sequence in a list which has a head structure
consisting of count, pointers and address of compare function needed to compare the
data in the list.
The data node contains the pointer to a data structure and a self-referential
pointer which points to the next node in the list.
remove() – Remove the first occurrence of any element from a non-empty list.
2. Stack ADT
In Stack ADT Implementation instead of data being stored in each node, the pointer to
data is stored.
The program allocates memory for the data and address is passed to the stack ADT.
The head node and the data nodes are encapsulated in the ADT. The calling function can
only see the pointer to the stack.
The stack head structure also contains a pointer to top and count of number of entries
currently in stack.
pop() – Remove and return the element at the top of the stack, if it is not empty.
peek() – Return the element at the top of the stack without removing it, if the stack is
not empty.
3. Queue ADT
View of Queue
The queue abstract data type (ADT) follows the basic design of the stack abstract data
type.
Each node contains a void pointer to the data and the link pointer to the next element in
the queue. The program’s responsibility is to allocate memory for storing the data.
dequeue() – Remove and return the first element of the queue, if the queue is not
empty.
peek() – Return the element of the queue without removing it, if the queue is not
empty.
Advantages:
Encapsulation: ADTs provide a way to encapsulate data and operations into a single
unit, making it easier to manage and modify the data structure.
Abstraction: ADTs allow users to work with data structures without having to know the
implementation details, which can simplify programming and reduce errors.
Information Hiding: ADTs can protect the integrity of data by controlling access and
preventing unauthorized modifications.
Modularity: ADTs can be combined with other ADTs to form more complex data
structures, which can increase flexibility and modularity in programming.
Disadvantages:
Overhead: Implementing ADTs can add overhead in terms of memory and processing,
which can affect performance.
Complexity: ADTs can be complex to implement, especially for large and complex data
structures.
Learning Curve: Using ADTs requires knowledge of their implementation and usage,
which can take time and effort to learn.
Limited Flexibility: Some ADTs may be limited in their functionality or may not be
suitable for all types of data structures.
Cost: Implementing ADTs may require additional resources and investment, which can
increase the cost of development.
We also need to write the code for each operation, using the chosen data structure and
algorithms.
The implementation details are hidden from the user by using encapsulation and
abstraction techniques.
For example, we can implement a List ADT using a dynamic array or a linked list as the
concrete data structure, and write the code for operations such as get, insert, remove,
replace, size, isEmpty, isFull etc.
The user of the List ADT does not need to know whether it is implemented using an
array or a linked list, only how to use the operations.
namespaces as a common concept of breaking a global scope into several local ones
UML packages are a direct use of decomposition on the model level - use packages to
organize your model
Abstraction is somehow more generic principle than decomposition, kind of "father of all
principles" :)
Abstraction Mechanism
1. Abstraction by Parameterization.
2. Abstraction by Specification.
1. Abstraction by Parameterization-It abstracts from the identity of the data by replacing them
with parameters.
Example –
x%2 = 0
It describes a computation that when we divide a number x to 2 then the remainder is equal to
zero which we use for finding that given number x is even or not.
x : int(x % 2 == 0)(y)
It is identical in meaning to
y % 2==0
In more familiar notation, we might denote the previous expression by the following expression
given below.
class Abstraction {
// Abstraction by parameterization
int Even(int x)
if (x % 2 == 0)
return x;
2. Abstraction by Specification:
It abstracts from the implementation details (how the module is implemented) to the behavior
users can depend on (what the module does). It isolates modules from one another’s
implementations.
It allows us to abstract from the computation described by the body of a procedure to the end
that procedure was designed to accomplish. We do this by associating with each other
procedure a specification of its intended effect and then considering the meaning of a
procedure call to be based on this specification rather than on the procedure’s body.
Even-odd procedure:
int Even(int x)
// MODIFIES: system.out
if (x % 2 == 0) {
Kinds of Abstraction-
1. Data abstraction – This type only shows the required information about the data and
ignores unnecessary details.
2. Control Abstraction – This type only shows the required information about the
implementation and ignores unnecessary details.
Abstraction using Classes
We can implement Abstraction in C++ using classes. The class helps us to group data members
and member functions using available access specifiers. A Class can decide which data member
will be visible to the outside world and which is not.
One more type of abstraction in C++ can be header files. For example, consider the pow()
method present in math.h header file. Whenever we need to calculate the power of a number,
we simply call the function pow() present in the math.h header file and pass the numbers as
arguments without knowing the underlying algorithm according to which the function is
actually calculating the power of numbers.
Access specifiers are the main pillar of implementing abstraction in C++. We can use access
specifiers to enforce restrictions on class members. For example:
Members declared as public in a class can be accessed from anywhere in the program.
Members declared as private in a class, can be accessed only from within the class. They
are not allowed to be accessed from any part of the code outside the class.
Procedural abstraction
Its a model of what we want a subprogram to do (but not how to do it). It provides mechanisms
for calling well defined procedures or operations as entities.
Example: Let there are 3 programmers (Alice, Bob & Jack). Alice need to write a program to
calculate sum-of-squares. Alice understand the meaning of sum but have no idea about square.
Bob & Jack both understand square and can help Alice as below.
Alice can use any of the square method(Either Bob code or Jack code). Alice don’t care
about the implementation of square procedure.
Here square procedure work as a layer for sum-of-squares procedure. Sometime we call
these layers as blocks or entities.
More meaningful layer you create, more powerful abstraction you will build.
Animal Hierarchy:
1. Class: Animal
2. Objects: lion, tiger, leopard (objects of different types from the Animal class)
Vehicle Hierarchy:
1. Class: Vehicle
2. Objects: car, motorcycle, truck (objects of different types from the Vehicle class)
Shape Hierarchy:
1. Class: Shape
2. Objects: circle, rectangle, triangle (objects of different types from the Shape class)
Employee Hierarchy:
1. Class: Employee
2. Objects: manager, developer, designer (objects of different types from the Employee
class)
1. Class: CarDealership
Animal Hierarchy:
1. Superclass: Animal
The for loop is one of the most widely used loops in C++. The for loop is a
deterministic loop in nature, that is, the number of times the body of the loop
is executed is known in advance.
The while loop is used to perform looping operations in situations where the
number of iterations is not known in advance. That is, unlike the for loop, the
while loop is non deterministic in nature.
The syntax of the while loop is
while(condition) {
The do-while loop: in a while loop, the condition is evaluated at the beginning of the loop
and if the condition evaluates to False, the body of the loop is not executed even once.
However, if the body of the loop is to be executed at least once, no matter whether the
initial state of the condition is True or False, the do-while loop is used. This loop places the
condition to be evaluated at the end of the loop.
1 do {
3 } while(condition) ;
Concrete state space- The concrete state space of an ADT is the set of all possible
values that the data can take on. The concrete state space is implementation-
dependent, meaning that it depends on how the ADT is implemented.
Concrete invariant- The concrete invariant of an ADT is a property that must be true for all valid
states of the data. The concrete invariant is also implementation-dependent.
Abstraction function- An abstraction function is a mapping from the concrete state space to
the abstract state space, which preserves the essential features of the ADT .
o The concrete state space is the set of all possible arrays of a fixed size that can
store elements of a given type.
o The concrete invariant is that there is an integer variable top that indicates the
index of the top element in the stack, and 0 <= top < size, where size is the
capacity of the array.