0% found this document useful (0 votes)
39 views38 pages

Unit 1 (OOPS)

Btech cse oops notes

Uploaded by

HIMANSHU SHARMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views38 pages

Unit 1 (OOPS)

Btech cse oops notes

Uploaded by

HIMANSHU SHARMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 38

UNIT-1(Astract data types)

Data types:Two important things about data types-


1. Defines a certain domain of values

2. Defines operations allowed on those values.

Example:

1. int type

-takes only integer values

-operations:addition,subtraction,multiplication,bitwise operations etc.

2.float type

-takes only floating point values

-operations:addition,subtraction,multiplication,division etc.(bitwise and % operations are not


allowed).

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.

 ADT consists of following three parts-

1. Data-Describes the structure of data used in ADT

2. Operation-Describes the valid operation for ADT.

3. Error-Describes how to deal with the errors that can occur.

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.

 The List ADT Functions is given below:

 get() – Return an element from the list at any given position.

 insert() – Insert an element at any position of the list.

 remove() – Remove the first occurrence of any element from a non-empty list.

 removeAt() – Remove the element at a specified location from a non-empty list.

 replace() – Replace an element at any position by another element.

 size() – Return the number of elements in the list.

 isEmpty() – Return true if the list is empty, otherwise return false.

 isFull() – Return true if the list is full, otherwise return false.

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.

 push() – Insert an element at one end of the stack called top.

 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.

 size() – Return the number of elements in the stack.

 isEmpty() – Return true if the stack is empty, otherwise return false.

 isFull() – Return true if the stack is full, otherwise return false.

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.

 enqueue() – Insert an element at the end of the queue.

 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.

 size() – Return the number of elements in the queue.

 isEmpty() – Return true if the queue is empty, otherwise return false.

 isFull() – Return true if the queue is full, otherwise return false.

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.

 Data Structure Independence: ADTs can be implemented using different data


structures, which can make it easier to adapt to changing needs and requirements.

 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.

Implimentation of Abstract data types:


 To implement an ADT, we need to choose a concrete data structure that can store the
values and support the operations of the ADT.

 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.

Decomposition : It is an application of the old good principle "divide and conquer" to


software development. It is a technique of classifying, structuring and grouping complex
elements in order to end up with more atomic ones, organized in certain fashion and easier to
manage. In all phases there are lots of examples:

 functional decomposition of a complex process to hierarchical structure of smaller sub-


processes and activities

 high-level structure of an complex application to 3 tiers - UI, logic and data.

 Class structure of a complex domain

 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 is one of the fundamental principles of object oriented programming. Abstraction


allows us to name objects that are not directly instantiated but serve as a basis for creating
objects with some common attributes or properties. For example: in the context of computer
accessories Data Storage Device is an abstract term because it can either be a USB pen drive,
hard disk, or RAM. But a USB pen drive or hard disks are concrete objects because their
attributes and behaviors are easily identifiable, which is not the case for Data Storage Device,
being an abstract object for computer accessories. So, abstraction is used to generalize objects
into one category in the design phase. For example in a travel management system you can use
Vehicle as an abstract object or entity that generalizes how you travel from one place to
another .

Abstraction is a process of focusing attention on the main problems by ignoring lower-level


details.

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)

// REQUIRES: num x is only integer

// MODIFIES: system.out

// EFFECTS : is num x % 2==0

// then num x is even else it is odd.

if (x % 2 == 0) {

system.out.print(num + "is even ") else system.out

.print(num + "is odd")

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.

Abstraction in Header files

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.

Abstraction using Access Specifiers

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.

Type hierarchy-Type hierarchy in software development is a system


that organizes or categorizes types, often called classes or interfaces, based
on their relationships. It defines how different types of objects relate to one
another, with more specific types inheriting attributes and behaviors from
more general ones.

 Inheritance is a key feature in type hierarchies, enabling new classes


to derive from existing ones.
 It helps in creating a structured and manageable code base by
promoting code reuse and polymorphism.
 Type hierarchies are essential in object-oriented programming (OOP),
where they facilitate the modeling of real-world systems.
CLASS-TO-OBJECT HIERARCHICAL RELATIONSHIP

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)

 HAS-A” HIERARCHICAL RELATIONSHIP(Aggregation, Association, and


Composition.)

Car Dealership Hierarchy:

1. Class: CarDealership

2. Association: CarDealership has Customers (Association)

3. Aggregation: CarDealership has Salespeople (Aggregation)

4. Composition: CarDealership has CarInventory (Composition)

 “IS-A” HIERARCHICAL RELATIONSHIPS(Inheritance)

Animal Hierarchy:
1. Superclass: Animal

2. Subclasses: Mammal, Reptile, Bird

3. Further subclasses: Dog, Cat, Snake, Eagle

Iteration Statements or Loops in C++


The statements that cause a set of statements to be executed repeatedly either for a specific
number of times or until some condition is satisfied are known as iteration statements. That is,
as long as the condition evaluates to True, the set of statement(s) is executed. The various
iteration statements used in C++ are for loop, while loop and do while loop.

The for Loop

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 syntax of the for loop is

for(initialize; condition; update) {

//body of the for loop

The while Loop

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) {

// body of while loop

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.

The syntax of the do-while loops is given here.

1 do {

2 //body of do while loop

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 .

 For example, if we implement a Stack ADT using an array-based stack, then:

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.

o The abstraction function is a mapping from an array-based stack to a sequence


of elements that represents the contents of the stack from top to bottom.

Implementing Operations, Illustrated by the Text Example


 To implement an operation of an ADT, we need to write a function or method that
takes some input parameters and returns some output values or modifies the
state of the ADT .
 The function or method should follow the specification of the operation given by
the ADT, and maintain the concrete invariant and abstraction function.
 For example, if we implement a Text ADT using a string as the concrete data
structure, then:
o The concrete state space is the set of all possible strings that can store
characters.
o The concrete invariant is that there is no limit on the length of the string.
o The abstraction function is a mapping from a string to a sequence of
characters that represents the contents of the text.
o To implement an operation such as insert(pos,c), which inserts a
character c at position pos in the text, we can write a function like this (in
pseudocode):

You might also like