1-Dsa - Oop and Dsa Basics
1-Dsa - Oop and Dsa Basics
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
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
8
OOP TERMINOLOGY
9
INTRODUCTION TO THE CLASS
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..
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
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
19
CALLING MEMBER FUNCTION
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
22
MEMBER FUNCTIONS
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 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];
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?
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