Академический Документы
Профессиональный Документы
Культура Документы
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 &&, ||, !
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
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?
• 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
Prof.A.KANNAMMAL,JCET 15
ENCAPSULATION
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:
Prof.A.KANNAMMAL,JCET 21
Languages like C serve
both the aspects and hence, can be
called as
Prof.A.KANNAMMAL,JCET 22
PROGRAMMING PARADIGMS
PARADIGM means organizing principle
of a program . It is an approach to the
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 :
Prof.A.KANNAMMAL,JCET 27
OBJECT ORIENTED
PROGRAMMING
Prof.A.KANNAMMAL,JCET 28
OBJECT ORIENTED PROGRAMMING
PROPERTIES
PROPERTIES
BEHAVIOUR
BEHAVIOUR
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.
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
UP NEXT
Prof.A.KANNAMMAL,JCET 42
Different Types of Inheritance
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
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
Car Bicycle
2-door 4-door
Prof.A.KANNAMMAL,JCET 48
Advantages of inheritance
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
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
• generics or templates
Prof.A.KANNAMMAL,JCET 56
Polymorphic Variables
• 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
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
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