0% found this document useful (0 votes)
36 views45 pages

1-Dsa - Oop and Dsa Basics

Uploaded by

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

1-Dsa - Oop and Dsa Basics

Uploaded by

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

DATA STRUCTURES AND

ALGORITHMS

1
Introduction
2 General Rules & Regulations

 No Late Arrivals.
 No Disturbance In Class
 No Use Of Mobile Phones(keep Them Silent And In Your
Pockets)
 No Retake Of Quizzes/Assignments
 No Late Submissions
 Student Is Responsible For His/Her Short Attendance.
3 Course Information

 Pre-requisite
 Programming Fundamentals/Object Oriented
Programming
 Course meeting times
 Lectures: 2 Sessions/Week
4 Course Aim

 To emphasize the importance of data


structures and efficient algorithmic design
for better management of computing
resources while programming.
Revision of OOP Concepts
WHAT IS OBJECT-ORIENTED
PROGRAMMING?
• OOP is centered around the object, which packages together
both the data and the functions that operate on the data
• Object-oriented programming (OOP) is a programming
language model organized around objects rather than
"actions" and data rather than logic

6
WHAT IS OBJECT-ORIENTED
PROGRAMMING?
Member Variables
float width;
float length;
float area;
Member Functions
void setData(float w, float l)
{ … function code … }
void calcArea(void)
{ … function code … }
void getWidth(void)
{ … function code … }
void getLength(void)
{ … function code … }
void getArea(void)
{ … function code … } 5
OOP TERMINOLOGY

• In OOP, an object’s member variables are often called its


attributes and its member functions are sometimes referred
to as its behaviors or methods

8
OOP TERMINOLOGY

9
INTRODUCTION TO THE CLASS

• In C++, the class is the construct primarily used to create


objects
class class-name
{
// declaration statements here
};

10
INTRODUCTION TO THE CLASS CONT..

11
EXAMPLE:
class Rectangle
{
private:
float width, length, area;
public:
void setData(float,
float); void
calcArea(void);
float
getWidth(void); float
getLength(void);
float
getArea(void);
}; 12
STRUCTURE VS CLASSES
• In C++ struct and class can be used interchangeably to create a class
with one exception
• What if we forget to put an access modifier before the first field?
struct smallObj{ OR class smallObj {
int someData; int someData;
In a class, until an access modifier is supplied, the fields are assumed to be
private
In a struct, the fields are assumed to be public

13
STRUCTURE VS CLASSES CONT..

• Other differences are


 Classes are usually used for large amounts of data, whereas
structs are usually used for smaller amounts of data
 Classes can be inherited whereas structures not
 A structure couldn't have a destructor such as a class
 A structure can't be abstract, a class can

14
ACCESS SPECIFIERS
• The key words private and public are access specifiers.
• private means they can only be accessed by the member functions
• public means they can be called from statements outside the
class
 Note: the default access of a class is private, but it is still a good
idea to use the private key word to explicitly declare private
members
 This clearly documents the access specification of the class

15
DEFINING AN INSTANCE OF A CLASS

• Class objects must be defined after the class is declared.


• Defining a class object is called the instantiation of a class.
• Rectangle box; // box is an instance of Rectangle

16
OBJECTS AND CLASSES

#include <iostream>
class smallobj //define a class
{
private:
int somedata; //class data
public:
void setdata(int d) //member function to set
data
{ somedata = d; }
void showdata() //member function to
display data
{ cout << “Data is “ << somedata << endl; }
}; 17
OBJECTS AND CLASSES CONT..
int main()
{
smallobj s1, s2; //define two objects of class smallobj
s1.setdata(1066); //call member function to set data

s2.setdata(1776); //call member function to display data


s1.showdata();
s2.showdata();
} return 0;
18
DEFINING OBJECTS

• The first statement in main()


smallobj s1, s2;
• Defines two objects, s1 and s2, of class smallobj
• Defining objects means creating them which is called instantiating
them
• The term instantiating arises because an instance of the class is
created
• An object is an instance (that is, a specific example) of a class
• Objects are sometimes called instance variables

19
CALLING MEMBER FUNCTION

• The next two statements in main() call the member function


setdata():
s1.setdata(1066);

s2.setdata(1776);
• Because setdata() is a member function of the smallobj class, it
must always be called in connection with an object of this
class
• To use a member function, the dot operator (the period) connects
the object name and the member function 20
DEFINING MEMBER FUNCTION OUTSIDE OF
THE
• CLASS
Scope Resolution Operator (::) can be used to define a
member function out side of the class
void Distance :: add_dist(Distance d2, Distance d3)
{
inches = d2.inches +
d3.inches; feet = 0;
If (inches >= 12.0)
{
inches - =
12.0; feet++;
}
feet += d2.feet +
d3.feet;
}
21
CLASS DATA

• The smallobj class contains one data item: somedata, which is


of type int
• The data items within a class are called data members (or
sometimes member data)
• There can be any number of data members in a class, just as
there can be any number of data items in a structure
• The data member somedata follows the keyword private, so it
can be accessed from within the class, but not from outside

22
MEMBER FUNCTIONS

• Member functions are functions that are included within a


class
• In some object-oriented languages, member functions are
called methods; some writers use this term in C++ as well
• There are two member functions in smallobj:
 setdata() and showdata()

23
DATA MEMBERS AND FUNCTIONS

• Usually the data within a class is private and the functions are
public
• The data is hidden so it will be safe from accidental
manipulation, while the functions that operate on the data
are public so they can be accessed from outside the class
• However, there is no rule that says data must be private and
functions public; in some circumstances you may find you’ll
need to use private functions and public data

24
CONST MEMBER FUNCTIONS
• Const Member Functions
 A const member function guarantees that it will never modify
any of its class’s member data
class aClass
{
private:
int
alpha;
public:
voi
d
nonFunc(
)
{ alpha = 25
CONST MEMBER FUNCTIONS
CONT..
• The non-const function nonFunc() can modify member data alpha,
but the constant function conFunc() can’t
 If it tries to, a compiler error results
• A function is made into a constant function by placing the keyword
const after the declaratory but before the function body
• If there is a separate function declaration, const must be used in
both declaration and definition

26
Review of Arrays & Pointers in C+
27
+
28 Review of Arrays & Pointers in C++
 Every byte in the computer’s memory has an address
 Addresses are numbers just as every house in a locality has got an
address
 Your program, when it is loaded into memory, occupies certain range of
these addresses
 This means that every variable and every function in your program
starts with a particular address

 NOTE : Pointer can only contain the ADDRESS of certain memory


location
29 Review of Arrays & Pointers in C++
 Normally a variable directly contains a specific value
 Pointer, on the other hand, contains an address of the variable that
contains the specific value
 Pointers, like other variables must be declared before they are used
int *ptr, temp;
 Pointers should be initialized either when they are declared or in an
assignment statement
 A pointer may be initialized to 0, NULL or an address
 A pointer with the value 0 or NULL points to nothing
30 Review of Arrays & Pointers in C++
 NULL is a symbolic constant defined in the header file
<iostream.h>
 Initializing a pointer to NULL is equivalent to initializing a
pointer to 0
 Initializing a pointer is necessary to prevent it pointing to
unknown or un-initialized area of memory
 There are two pointer operators
 the address of operator &
 indirection operator or dereferencing operator *
31 Review of Arrays & Pointers in C++

 & is a unary operator that returns the address of its


operand
int x = 5;
int *ptrr = NULL;
ptrr = &x;
 In above * shows ptrr is pointer variable whose type
is int
 i.e. it can only contain the address of any integer
variables in memory
32 Review of Arrays & Pointers in C++

cout<< *ptrr << “\n” ;


cout<< x << “\n” ;
 Will produce the same result 5
 Here * means value of the variable to which ptrr points to,
which is x

 Note that the dereferenced pointer may also be used on the left
side of an assignment statement as follows.
*pttr = 9; //or
cin>>*ptrr;
Output of
33 Program
5
5
0x0064FDF4
0x0064FDF4
0x0064FDF4
0x0064FDF4
9
9
34 new and delete operators
 Variables created during execution (run time) of a program
by means of special operation is known as Dynamic Data
 In C++ operation is new
int *intptr;
char *namestr;
intptr = new int;
namestr = new char[8];

 Variables created by new are said to be on the free store or


heap, a region of memory set aside for Dynamic Variables
 The new operator obtains a chunk (portion) of memory from
the free store
35 new and delete operators

 Dynamic data can be destroyed at any time during


the execution of a program when it is no longer
needed
 The built-in operator delete does that and has two
forms
 one for deleting single variable
 the other for deleting an array

delete pointer;
delete [ ] pointer;
37 Introduction
 What is data?
 Information converted into binary/digital form
 Types of data?
 text, numeric, audio, video etc.
 Importance of data?
 Primary purpose of computer programs is to store
and retrieve information, usually as fast as possible
38 What is a Data Structure?

 A particular way of organizing data in a


computer so that it can be used effectively
 E.g. we can store a list of items having the
same data-type using the array data
structure

 Method of representing logical relationships


between individual data elements related to the
solution of a given problem
 Resource constraints  space, time
39 Basic Data Structures

Basic
Data Structures

Linear Non-Linear
Data Structures Data Structures

Linked Hash
Arrays Stacks Queues Trees Graphs
Lists Tables
40 Basic Data Structures
 Linear: Values are arranged in a linear fashion
 Array: Fixed size
 Linked-List: Variable size
 Stack: Add to top & remove from top
 Queue: Add to back & remove from front
 Priority Queue: Add anywhere, remove the highest
priority
41 Basic Data Structures
 Non-Linear: Values are not
arranged in a linear fashion
 Hash Table: Unordered lists
which use hash function to
insert & search
 Tree: Data is organized in
branches
 Graph: A more general
branching structure with less
strict connection conditions
than that for a tree
42 Types of Data Structures
(composition)
 Homogenous: In this
type of data structures,
values of the same
types of data are stored
 Array
 Non-Homogenous: In
this type of data
structures, data values
of different types are
grouped and stored
 Structures
 Classes
43 Selection of Data Structure
 Each data structure has costs and benefits
 Rarely is one data structure better than another in
all situations
 A data structure requires:
 space for each data item it stores
 time to perform each basic operation
 programming effort
 Each problem has constraints on available time and
space
 Only after a careful analysis of problem
characteristics can we know the best data structure
for the task
44 Characteristics of Data Structures
45 Characteristics of Data Structures

You might also like