Вы находитесь на странице: 1из 70

DATA STRUCTURES Marks: 15

Abstract data types and objects,


graphical user intefaces;
 language suppoft and OOP: Inheritance,
classes and subclasses, header files,
overloading.
Programming with Data structures: Stacks,
queues, lists, trees and balanced binary trees,
algorithms for searching and sorting and open
office
Prof.A.KANNAMMAL,JCET 1
What is an Abstract Data Type?
• Abstract Data Type (ADT)
– Defines a particular data structure in terms of
data and operations
– Offers an interface of the objects (instances of an
ADT)
• An ADT consists of
– Declaration of data
– Declaration of operations
– Encapsulation of data and operations : data is
hidden from user and can be
Prof.A.KANNAMMAL,JCET
manipulated only by
2
means of operations
Data abstraction
• Data Type
A data type is a collection of objects and a set of
operations that act on those objects.
– For example, the data type int consists of the objects {0,
+1, -1, +2, -2, …, INT_MAX, INT_MIN} and the operations
+, -, *, /, and %.
• The data types of C
– The basic data types: char, int, float and double
– The group data types: array and struct
– The pointer data type
– The user-defined types
Prof.A.KANNAMMAL,JCET 3
Data abstraction
• Abstract Data Type
– An abstract data type(ADT) is a user defined
data type that is organized in such a way that
the specification of the objects and the
operations on the objects

Prof.A.KANNAMMAL,JCET 4
• A ADT data type is characterized by:
– a set of values
– a set of operations, which can be applied
uniformly to all these values

Prof.A.KANNAMMAL,JCET 5
Primitive types as data types
Type Values Representation Operations
boolean true, false Single byte &&, ||, !

char, byte, Integers of Two’s complement +, -, *, /,


short, int, varying sizes others
long
float, Floating point Two’s complement +, -, *, /,
double numbers of with exponent and others
varying sizes mantissa
and precisions
6
Data abstraction
• Data abstraction is one of the most
essential and important feature of
object oriented programming in C++.
Abstraction means displaying only
essential information and hiding the
details.

Prof.A.KANNAMMAL,JCET 7
• Every ADT should have specification
that:
– Specifies the set of valid values type of the
ADT
– Specifies, for each operation of the ADT:
• Its name
• Its parameter types
• Its result type, if any
• Its observable behavior
– Does not specify:
• The data representation
• The algorithms used to implement the
operations
Prof.A.KANNAMMAL,JCET 8
Implementing an ADT
• To implement an ADT, you need to choose
class type:
– a data representation
• must be able to represent all necessary values of
the ADT
• should be private
– an algorithm for each of the necessary
operations
• must be consistent with the chosen representation
• all auxiliary (helper) operations that are not in the
contract should be private

Prof.A.KANNAMMAL,JCET 9
ADT Implementation
• Implementaion of an Abstract Data Type
(ADT)
– Hidden from the user
– Same ADT may be implemented in different ways
in different languages
– Some languages offer built-in ADTs and/or
features to be used to implement ADTs (user-
define types)
• ADTs support modular design which is very
important in software development
Prof.A.KANNAMMAL,JCET 10
Model for an ADT
Interface - Operations

Data Structure

System design with ADTs


Identification of ADTs
Problem definiton (identify data or attributes)

Specify ADT interactions Specify ADT operations

Identify object hierarchy (if using OOP)

Implement ADTs
Prof.A.KANNAMMAL,JCET 11
Complex Number ADT Example
• A complex number has a real part and
an imaginary part. e.g.: 2+4i
• Interface (operations) :
– create a complex number
– add, subtract, multiply, divide
– print a complex number
– test to see if something is complex
– etc.

Prof.A.KANNAMMAL,JCET 12
How Well are ADTs Supported in C?

• Does C enforce the use of the ADTs


interface and the hiding of its
implementation?

• No

Prof.A.KANNAMMAL,JCET 13
ADT vs Object-Oriented Programming (OOP)
• ADTs are not a part of a particular programming
language
• Rather they are implemented by a programmer
to solve a particular problem or some class of
problems
• In OOP, an ADT can be easily modeled as a
class
– An instance as an object
– Data of ADT as properties or fields of a class
– Operations as methods
• ADT ≠ OOP
• Classes in OOP offers more features than ADTs
: Inheritance (Superclass-Subclass), Polymorphisms, etc.
Prof.A.KANNAMMAL,JCET 14
DATA ABSTRACTION

ABSTRACTION refers to the act of


representing essential features without
including the background details or
explanations.
It focuses upon the observable behavior of
an object.

Prof.A.KANNAMMAL,JCET 15
ENCAPSULATION

 The wrapping up of data and operations /


functions (that operate on the data ) into a
single unit (called class) is known as
ENCAPSULATION.
 Encapsulation is a way to implement data
abstraction. It hides the details of the
implementation of an object.
 It focuses upon the implementation that gives
rise to observable behavior.

Prof.A.KANNAMMAL,JCET 16
Notes
• Inheritance feature is used for concept of code
re-usability as in inheritance a class can inherit
properties and functions of existing well written
class.
Abstraction : Provide only necessary
information to client code.
Encapsulation: Hide complexity. e.g. by
wrapping private class data members by
functions. By making internal function of a class
private and using interfaces etc.
Prof.A.KANNAMMAL,JCET 17
LIST ADT example:

public interface ListADT<L>


{
void add(L item);
void add(int pos, L item);
boolean contains(L item);
int size();
boolean isEmpty();
L get(int pos);
L remove(int pos);
Prof.A.KANNAMMAL,JCET 18
}
Prof.A.KANNAMMAL,JCET 19
LOW LEVEL LANGUAGES

 Machine Language - instructions are


written in binary code in it . Only language
that computer can execute directly.

 Assembly Language - instructions r written


using symbolic names for machine operation
{READ,ADD,STORE} & operands. Program
is then converted into machine language
using ASSEMBLER software.
Prof.A.KANNAMMAL,JCET 20
HIGH LEVEL LANGUAGE
 English like keywords, constructs for sequence,
selection & looping and use of variables &
constants.
 This programs are converted into machine
language using compiler or interpreter
 Serve only 2nd aspect “close to programmer”

Prof.A.KANNAMMAL,JCET 21
Languages like C serve
both the aspects and hence, can be
called as

Middle Level Languages

Prof.A.KANNAMMAL,JCET 22
PROGRAMMING PARADIGMS
PARADIGM means organizing principle
of a program . It is an approach to the
programming.

Ex : Procedural programming


Modular programming
Object oriented programming

Prof.A.KANNAMMAL,JCET 23
Structured Programming
• Back in the "old days" we had Structured
Programming:
– data was separate from code.
– programmer is responsible for organizing
everything in to logical units of code/data.
– no help from the compiler/language for
enforcing modularity, …

Prof.A.KANNAMMAL,JCET 24
PROCEDURAL PROGRAMMING
 The focus is on processing
 It says :

DECIDE WHICH PROCEDURES YOU WANT


USE THE BEST ALGORITHMS YOU CAN FIND

 Languages support it by providing facilities


for passing arguments to functions &
returning values from functions
Prof.A.KANNAMMAL,JCET 25
MODULAR PROGRAMMING
 A SET OF RELATED PROCEDURE WITH DATA
THEY MANIPULATE IS CALLED A MODULE.
 It says

DECIDE WHICH MODULES YOU WANT


PARTITION THE PROGRAM
SO THAT DATA IS HIDDEN IN MODULES

 Works on principle of grouping of


components that carry out specific tasks.
Prof.A.KANNAMMAL,JCET 26
Drawbacks of Procedural &
Modular Approaches

• They do not model real world well. like a


procedural program on library management
aims at operations like issued, returned & give
2nd class status to real world entities : books.
• In modular programming, since many modules
access same data, arrangement of data can’t be
changed without modifying all modules that
access it

Prof.A.KANNAMMAL,JCET 27
OBJECT ORIENTED
PROGRAMMING

Prof.A.KANNAMMAL,JCET 28
OBJECT ORIENTED PROGRAMMING

 OBJECT is an identifiable entity with some


characteristics and behavior.

 In OOP programming object represents a


entity that can store data and has its
interface through functions.

 CLASS is a template/blue-print representing


a group of objects that share common
properties and relationships.
Prof.A.KANNAMMAL,JCET 29
It says:
decide which classes and objects are needed
provide a full set of operations for each class
OBJECT 1 OBJECT 2

PROPERTIES
PROPERTIES

BEHAVIOUR
BEHAVIOUR

DATA & FUNCTIONS ENCLOSED WITHIN OBJECTS


NEW OBJECTS COMMUNICATE WITH EACH OTHER
Prof.A.KANNAMMAL,JCET 30
PROCEDURAL PARADIGM

FUNCTION 1

DATA 1

FUNCTION 2

DATA 2
FUNCTION 3

Prof.A.KANNAMMAL,JCET 31
GENERAL CONCEPTS OF OOP

1. DATA ABSTRACTION

2. DATA ENCAPSULATION

3.MODULARITY

4. INHERITANCE

5.POLYMORPHISM

Prof.A.KANNAMMAL,JCET 32
OOP to the rescue
• Keep data near the relevant code.
• Provide a nice packaging mechanism for
related code.
• Model the world as objects.
• objects can send "messages" to each
other.

Prof.A.KANNAMMAL,JCET 33
Object
• Collection of:
– Fields (object state, data members, instance
variables, ..)
– Methods (behaviors, …)
• Each object has it's own memory(separate
memory) for maintaining state (the fields).
• All objects of the same type share
code.(shared memory)

Prof.A.KANNAMMAL,JCET 34
Modern OOP Benefits
• Code re-use
– programmer efficiency
• Encapsulation
– code quality, ease of maintenance
• Inheritance
– efficiency, extensibility.
• Polymorphism
– power!(act many form)

Prof.A.KANNAMMAL,JCET 35
Code Re-Use
• nice packaging makes it easy to document/find
appropriate code.

• everyone uses the same basic method of


organizing code (object types).

• easy to re-use code instead of writing minor


variations of the same code multiple times
(inheritance).

Prof.A.KANNAMMAL,JCET 36
Encapsulation
• Information Hiding.
• Don't need to know how some component
is implemented to use it.
• Implementation can change without
effecting any calling code.
• "protects us from ourselves"

Prof.A.KANNAMMAL,JCET 37
Inheritance
• The language mechanism by which one
class acquires the properties (data and
operations) of another class
• Base Class (or superclass): the class
being inherited from
• Derived Class (or subclass): the class that
inherits

Prof.A.KANNAMMAL,JCET 38
Inheritance
• On the surface, inheritance is a code re-
use issue.
– we can extend code that is already written in
a manageable manner.
• Inheritance is more, it supports
polymorphism at the language level!

Prof.A.KANNAMMAL,JCET 39
Inheritance
• Take an existing object type (collection of
fields and methods) and extend it.
– create a special version of the code without
re-writing any of the existing code (or even
explicitly calling it!).
– End result is a more specific object type,
called the sub-class / derived class / child
class.
– The original code is called the superclass /
parent class / base class.
Prof.A.KANNAMMAL,JCET 40
DATA

MEMBER
FUNCTIONS

DATA
DATA
MEMBER
FUNCTIONS MEMBER
FUNCTIONS

THE OOP APPROACH


DATA ABSTRACTION & ENCAPSULATION
Prof.A.KANNAMMAL,JCET 41
Inheritance is the capability of one
class of things to inherit capabilities or
properties from other class.

UP NEXT

 Why inheritance was introduced into


OO languages ?

Prof.A.KANNAMMAL,JCET 42
Different Types of Inheritance

• OOPs support the six different types of


inheritance as given below :
– Single inheritance
– Multi-level inheritance
– Multiple inheritance
– Multipath inheritance
– Hierarchical Inheritance
– Hybrid Inheritance

Prof.A.KANNAMMAL,JCET 43
Prof.A.KANNAMMAL,JCET 44
Base and Derived Classes

• Often an object from a derived class (subclass) “is


an” object of a base class (superclass)
Base class Derived classes

Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount

Prof.A.KANNAMMAL,JCET 45
Inheritance
• One object type is defined as being a
special version of some other object type.
– a specialization.
• The more general class is called:
– base class, super class, parent class.
• The more specific class is called:
– derived class, subclass, child class.

Prof.A.KANNAMMAL,JCET 46
9.12 Composition vs. Inheritance
• "is a" relationship
– Inheritance
• "has a" relationship
– Composition - class has an object from another class as a
data member

Employee “is a” BirthDate; //Wrong!


Employee “has a” Birthdate;//Composition

Prof.A.KANNAMMAL,JCET 47
Arrange concepts into an
inheritance hierarchy
• Concepts at higher levels are more general
• Concepts at lower levels are more specific
(inherit properties of concepts at higher
levels)
Vehicle

Wheeled vehicle Boat

Car Bicycle

2-door 4-door
Prof.A.KANNAMMAL,JCET 48
Advantages of inheritance

• When a class inherits from another


class, there are three benefits:
• (1) You can reuse the methods and
data of the existing class
(2) You can extend the existing class by
adding new data and new methods
(3) You can modify the existing class by
overloading its methods with your own
implementations
Prof.A.KANNAMMAL,JCET 49
Inheritance and accessibility

• A class inherits the behavior of another


class and enhances it in some way
• Inheritance does not mean inheriting
access to another class’ private
members

Prof.A.KANNAMMAL,JCET 50
Forms of Inheritance
• Derived class inherits from base class
• Public Inheritance (“is a”)
– Public part of base class remains public
– Protected part of base class remains protected
• Protected Inheritance (“contains a”)
– Public part of base class becomes protected
– Protected part of base class remains protected
• Private Inheritance (“contains a”)
– Public part of base class becomes private
– Protected part of base class becomes private
Prof.A.KANNAMMAL,JCET 51
Public, Protected, Private Inheritance
class A { • Class A declares 3 variables
public: – i is public to all users of class A
int i;
– j is protected. It can only be used by methods
protected: in class A or its derived classes (+ friends)
int j;
– k is private. It can only be used by methods in
private: class A (+ friends)
int k;
};
• Class B uses public inheritance from A
Class B : public A { – i remains public to all users of class B
// ... – j remains protected. It can be used by methods
}; in class B or its derived classes
Class C : protected A {
// ... • Class C uses protected inheritance from A
}; – i becomes protected in C, so the only users of
Class D : private A { class C that can access i are the methods of class C
// ... – j remains protected. It can be used by methods
}; in class C or its derived classes

• Class D uses private inheritance from A


– i and j become private in D, so only methods of
class D can access them.
Prof.A.KANNAMMAL,JCET 52
Rules for building a class
hierarchy
• Derived classes are special cases of base
classes
• A derived class can also serve as a base class
for new classes.
• There is no limit on the depth of inheritance
allowed in C++ (as far as it is within the limits of
your compiler)
• It is possible for a class to be a base class for
more than one derived class

Prof.A.KANNAMMAL,JCET 53
Polymorphism Definition

Polymorphous:
– Having, or assuming, various forms,
characters, or styles. Term used in
several different ways in computer
science

Prof.A.KANNAMMAL,JCET 54
Types of Polymorphism
• Two types of Polymorphism are available in C++
object oriented programming i.e. compile time
and run time polymorphism. Also, known as
early binding and late binding respectively.
• Compiler time polymorphism features in C++
language are function overloading, constructor
and operator overloading etc. and run time
polymorphism is function overriding in
inheritance relationship.

Prof.A.KANNAMMAL,JCET 55
Forms of Polymorphism

• polymorphic variables (assignment polymorphism)

• overloading (ad hoc polymorphism)

• overriding (inclusion polymorphism)

• deferred (abstract) methods

• pure polymorphism (functions with polymorphic


parameters)

• generics or templates
Prof.A.KANNAMMAL,JCET 56
Polymorphic Variables

Variable declared as one class can hold values


from subclass

• Dynamic (run-time) type must be subclass of


static (declared) type

• In C++, only works with pointers and references

• Polymorphic variables (or values) basis for


power of OOP
Prof.A.KANNAMMAL,JCET 57
Overloading
A single function name denoting two or more function bodies is
overloaded

• Common example: + means both integer and floating addition


In Java, + also used for string concatenation

• Determination of which function to execute made by examining


argument/return types

• Bound either at compile-time or at run-time depending on language

• Should not be confused with overriding or refinement – functions need not be


related by classes

• Should not be confused with coercion (casting) – one type is converted into
another for operation
Prof.A.KANNAMMAL,JCET 58
What is Library?
• A Library is a collection of subprograms used to develop
other programs and software.
• C++ Library functions stores function in same
category.(i.e., Mathematical Functions, String Functions
etc..) under separate file known as header files
• In computer science, library is a collection of
subprograms used to develop software.
• Libraries are not independent programs, rather they are
helper code used in other independent programs

Prof.A.KANNAMMAL,JCET 59
Header Files
• some program elements (built-in functions,
variables, etc.) are contained in header files
• to use these program elements you need to
“include” the appropriate header files in your
source code
• header files have .h suffixes
• syntax for inclusion of header files:
#include <header_file_name>
• Included before function definition
• < and > are part of the syntax
• Note that the #include statement does not end
with a ; Prof.A.KANNAMMAL,JCET 60
stdio.h
clearerr fclose fcloseall fdopen feof ferror
fflush fgetc fgetchar fgetpos fgets fileno
flushall fopen fprintf fputc fputchar fputs
fread freopen fscanf fseek fsetpos ftell
fwrite getc getchar gets getw perror
printf putc putchar puts putw remove
rename rewind rmtmp scanf setbuf setvbuf
sprintf sscanf strerror _strerror tempnam tmpfile
tmpnam ungetc unlink vfprintf vfscanf vprintf
vscanf vsprintf vsscanf
Prof.A.KANNAMMAL,JCET 61
string.h

_fmemccpy _fmemchr _fmemcmp _fmemcpy _fmemicmp


_fmemset _fstrcat _fstrchr _fstrcmp _fstrcpy
_fstrcspn _fstrdup _fstricmp _fstrlen _fstrlwr
_fstrncat _fstrncmp _fstrnicmp _fstrncpy _fstrnset
_fstrpbrk _fstrrchr _fstrrev _fstrset _fstrspn
_fstrstr _fstrtok _fstrupr memccpy memchr
memcmp memcpy memicmp memmove memset
movedata movmem setmem stpcpy strcat
strchr strcmp strcmpi strcpy strcspn
strdup _strerror strerror stricmp strlen
strlwr strncat strncmp strncmpi strncpy
strnicmp strnset strpbrk strrchr strrev
strset strspn strstr strtok strxfrm
strupr

Prof.A.KANNAMMAL,JCET 62
math.h
abs acos, acosl asin, asinl
atan, atanl atan2, atan2l atof, _atold
cabs, cabsl ceil, ceill cos, cosl
cosh, coshl exp, expl fabs, fabsl
floor, floorl fmod, fmodl frexp, frexpl
hypot, hypotl labs ldexp, ldexpl
log, logl log10, log101 matherr, _matherrl
modf, modfl poly, polyl pow, powl
pow10, pow10l sin, sinl sinh, sinhl
sqrt, sqrtl tan, tanl tanh, tanhl

Prof.A.KANNAMMAL,JCET 63
stdlib.h
• This header file declares several
commonly used routines like conversion
routines. The function contained in stdlib.h
are,

Prof.A.KANNAMMAL,JCET 64
stdlib.h
abort abs atexit atof atoi
atol bsearch calloc div ecvt
exit _exit fcvt free _fullpath
gcvt getenv itoa labs ldiv
lfind _lrotl _lrotr lsearch ltoa
_makepath malloc max mblen mbtowc
mbstowcs min putenv qsort rand
random randomize realloc _rotl _rotr
_searchenv _splitpath srand strtod strtol
_strtold strtoul swab system time
ultoa wctomb wcstombs

Prof.A.KANNAMMAL,JCET 65
iostream.h
• The header file declares the basic C++
streams input/output routines. Some of the
functions defined in it are,

Prof.A.KANNAMMAL,JCET 66
iostream.h

open close get getline read write put


seekg seekp telg telp ignore pick putback
flush rdstate bad eof fill good

Prof.A.KANNAMMAL,JCET 67
• GUI(Graphical User Interface)

Prof.A.KANNAMMAL,JCET 68
QUERIES
?
Prof.A.KANNAMMAL,JCET 69
THANK
YOU
Prof.A.KANNAMMAL,JCET 70

Вам также может понравиться