|
UNIT – 1 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
INTRODUCTION |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Unit-01/Lecture-01
What is an Object
Oriented Technology? ·
Object-oriented technology
(OOT) is a software
design model in which objects contain both data and the instructions that
work on the data. It is increasingly deployed in distributed computing. ·
Object Oriented
Programming (OOP) is to mean any kind of programming that uses a programming
language with some object oriented constructs or programming in an
environment where some object oriented principles are followed. ·
The Object-Oriented
(OO) process is an evolutionary approach to software engineering. ·
It encompasses the
entire software life cycle, from operational concept through release. ·
The OO paradigm
focuses on classes that encapsulate data and algorithms for manipulating the
data. OO classes promote reusability across applications. ·
OO technologies (OO
Design, OO Languages, OO Metrics, OO Modeling, OO Specifications, and OO
Testing) support the OO paradigm. ·
The first step in
OOP is to identify all the objects the programmer wants to manipulate and how
they relate to each other, an exercise often known as data
modeling. ·
The concept of a
data class makes it possible to define subclasses of data objects that share
some or all of the main class characteristics. Called inheritance, this property of OOP forces a more thorough data analysis,
reduces development time, and ensures more accurate coding. ·
It includes object-oriented programming languages such as Smalltalk,
C++, and Eiffel, object-oriented development methodologies, and management of
object-oriented projects, object-oriented computer hardware, and
object-oriented computer aided software engineering, among others. ·
In object oriented
programming, the data and related functions are bundled together into an
"object". Ideally, the data inside an object can only be
manipulated by calling the object's functions. This means that your data is
locked away inside your objects and your functions provide the only means of
doing something with that data. In a well designed object oriented system
objects never access shared or global data, they are only permitted to use
the data they have, or data they are given.
Fig. 1 ·
Modern OO languages provide the programmer
with three capabilities that improve and simplify the design of such
programs: encapsulation, inheritance, and polymorphism(or generic
functionality). ·
The basic unit of OOP is a class,
which encapsulates both the static attributes and dynamic behaviors
within a "box", and specifies the public interface for using these
boxes. Since the class is well-encapsulated (compared with the function), it
is easier to reuse these classes. In other words, OOP combines the data
structures and algorithms of a software entity inside the same box. ·
OOP languages permit higher level of
abstraction for solving real-life problems. The traditional procedural
language (such as C and Pascal) forces you to think in terms of the structure
of the computer (e.g. memory bits and bytes, array, decision, loop) rather
than thinking in terms of the problem you are trying to solve. The OOP
languages (such as Java, C++, C#) let you think in the problem space, and use
software objects to represent and abstract entities of the problem space to
solve the problem. .
Unit-01/Lecture-02 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Procedural Programming ·
Traditional programming
languages were procedural–C, Pascal, BASIC, Ada and COBOL ·
Programming in procedural
languages involves choosing data structures
(appropriate ways to store data), designing algorithms, and translating algorithm into
code. ·
In procedural
programming, data and operations on the data are separated. ·
This methodology requires sending data to
procedure/functions. ·
PROCEDURAL
PROGRAMMING which at times has been referred to as inline programming takes a more top down approach
to programming. OBJECT-ORIENTED PROGRAMMING uses classes and objects, PROCEDURAL
PROGRAMMING takes on applications by solving problems from the top of the
code down to the bottom. ·
This
happens when a program starts with a problem and then breaks that problem
down into smaller sub-problems or sub-procedures. These sub-procedures are
continually broken down in the process called functional decomposition until
the sub-procedure is simple enough to be solved. ·
The
issue that is obvious in PROCEDURAL PROGRAMMING is that if a edit is needed
to the program, the developer must edit every line of code that corresponds
to the original change in the code. An example would be if at the beginning
of a program a variable was set to equal the value of 1. If other
sub-procedures of the program rely on that variable equaling 1 to function
properly they will also need to be edited. As more and more changes may be
needed to the code, it becomes increasingly difficult to locate and edit all
related elements in the program. ·
When
developing with PROCEDURAL PROGRAMMING a developer may take a much different
approach to designing an application. ·
PROCEDURAL
PROGRAMMING takes a more top down approach to writing an application and
while a developer who uses OBJECT-ORIENTED PROGRAMMING to create applications
would think of planning out the program with re-usable classes, a developer
who uses PROCEDURAL PROGRAMMING might plan out the program without the idea
of recycling code. ·
Sometimes
our functions need to access data that is not provided as a parameter, i.e.,
we need access data that is outside the function. Data accessed in this way
is considered "global" or "shared" data.
Fig.2 ·
The programs are made up of functions.
Functions are often not reusable. It is very difficult to copy a
function from one program and reuse in another program because the the
function is likely to reference the headers, global variables and other
functions. In other words, functions are not well-encapsulated as a
self-contained reusable unit. ·
The procedural languages are not suitable
of high-level abstraction for solving real life problems. For example,
C programs uses constructs such as if-else, for-loop, array, function,
pointer, which are low-level and hard to abstract real problems such as a
Customer Relationship Management (CRM) system or a computer soccer game.
(Imagine using assembly codes, which is a very low level code, to write a
computer soccer game. C is better but no much better.)
Unit-01/Lecture-03
Procedural Vs Object Oriented Programming
Fig. 3 Procedural programming. The
main program coordinates calls to procedures and hands over appropriate data
as parameters
Fig.4
Unit-01/Lecture-04
Object oriented terms and Concepts Objects ·
An object is an instance of a class. A class must be instantiated into
an object before it can be used in the software. ·
More than one instance of the same class can be in existence at any
one time. ·
Objects are the physical and conceptual things we find in
the universe around us. ·
Objects
can be passed to functions as arguments in just the same way that other types
of data are passed. ·
An object may represent a real-world object or it may be a completely
imaginary object, such as conceptual object like a shape (rather than
specific type of shape like a square). But regardless of what they represent,
they are not physical objects that we can pick up and touch; they only exist
in a computer's memory. Thus an object is a software unit. ·
Software objects are often used to model the real-world objects that
you find in everyday life. ·
Syntactically, a functional object can be
represented as: name : object methods ... For example, Objects which have an updateable state are imperative objects.
Imperative objects are like variables. They are the objects of Simula,
Smalltalk and C++. They have a name, a collection of methods which are
activated by the receipt of messages from other objects, and instance
variables which are shared by the methods of the object but inaccessible to
other objects. ·
Objects
which may be active when a message arrives are active objects. In contrast,
functional and imperative objects are passive unless activated by a message.
Active objects have three modes: when there is nothing to do the object is dormant, when the agent is
executing it is active, and
when an object is waiting for a resource or the completion of subtasks it is waiting. Messages sent to an
active object may have to wait in queue until the object finishes a task.
Message passing among objects may be synchronous or asynchronous. Objects
instantiations and interactions ·
Class is a template for something, you can't actually execute a class,
you must instantiate it, which is creating an instance of a class in the form
of an object. ·
In object-oriented programming
(OOP), an instance
is a specific realization of any object. Formally, "instance" is
synonymous with "object" as they are each a particular value
(realization) and these may be called an instance object;
"instance" emphasizes the distinct identity of the object. The
creation of a realized instance is called instantiation. Object lifetime ·
Constructors and destructors control the creation and destruction of
objects. ·
Starts with Constructor execution -
As soon as Initialization ends and control enters Constructor Body. -
Must follow Memory Allocation ·
Ends with Destructor execution -
As soon as control leaves Destructor Body -
Must precede Memory De-allocation Ex. class
MyClass;
void MyFunc() // Allocation on stack for myobj {
MyClass myObj; // Construction … … myObj.myMethod(); //Use … …
Return; //Destruction-Implicit }
//De-Allocation from Stack for myObj Unit-01/Lecture-05
·
Static and dynamic objects ·
Global and local
objects ·
Application
of OOP Static and dynamic
objects -Static Object ·
Static almost always means fixed or bound at compile time, and cannot
thereafter be changed. ·
Static is a keyword in C++ used to give special characteristics to an
element. ·
Static object s are allocated storage only once in a program lifetime
in static storage area and Static Objects have scope till the program
lifetime. ·
Static objects make it possible to access
an object inside a function without having to pass along a pointer or
reference to it. Many objects can use the same object without each object
storing a pointer to the object, which can save space and sometimes make the
code less complex. ·
Static Objects are also initialized using constructors like other
normal objects. Assignment to zero, on using static keyword is only for
primitive data types, not for user defined data types. ·
Disadvantages of having static objects. Any function that has access
to a static object could use it, which means that it can be costly and
difficult to maintain code with many static objects. Ex. class Abc { Int I; Public: Abc() { I=0; cout
<< “constructor” ; }
~Abc()
cout<<”destructor”;
}
};
void f() {
Static Abc obj; } int
main() { Int
x=0;
If(x==0) {
F();
}
Cout <<”END”; } Output:
constructor END destructor Destructor not
called upon the end of the scope of if condition. Because Object was static
which has scope till the program lifetime, hence destructor for this object
was called when main () exits. -Dynamic Objects ·
Dynamic almost always means not fixed or bound until run time, and
therefore can change during the course of execution. ·
Dynamic Objects have scope within particular block. ·
Dynamic objects are used when the object to be created is not
predictable enough. ·
To create dynamic objects we cannot be given unique names. For these
we use pointers. ·
Dynamic objects use dynamic memory allocation. ·
In
C++ a pointer can be directed to an area of dynamically allocated memory at
runtime ·
This can then be made to point to a newly created object. ·
To
destroy a dynamic Object the destructor must be called. This is done by using
the Delete operator. ·
The
creation of a dynamic object implies the creation of an object by the program
itself according to its "needs". The dynamic objects don't have a
name to easily, instead they can be identified by pointers. -Definition of a pointer to a given class. -Create the object using the new keyword, which will also return the
address of the newly created object. -Assign the address to the pointer. The
syntax is as follows: Class_name* Pointer_name; Pointer_name = new Class_Name; Ex. # include <iostream>
# include <cstdlib>
# include “Colour.h” int main() { Colour *current; current = new Colour(1,0,0); current->Print(); delete current; current=new Colour(1,1,1); current ->Print(); delete current; } Global and local objects -Global objects ·
Persistent (in existence) throughout the lifetime of a program, i.e.
it’s scope is an entire program. ·
These have identifiers and attribute which remain constant throughout
the program. -Local objects ·
Local
Objects exist in a predictable manner for a particular period of time. ·
However
Automatic objects are instantiated within a given scope in a program that is,
declared within the braces {} of either main or a function. ·
Local
Objects are automatically destroyed once they fall out of the scope in which
they were declared. ·
Local
objects are persistent and visible within the scope they are declared in. Application
of OOP 1.
Real-time systems
Unit-01/Lecture-06
·
Class ·
Metaclass Class ·
Class is a user defined type / abstract data type. ·
Class represent template for an object. It actually contains data and
there behavior / methods in it and for utilization of it we need to create an
object of that particular class. ·
Class defines basic layout and functionality of an object. ·
A class is a blueprint or prototype from which objects are created.
This section defines a class that
models the state and behavior of a real-world object. ·
A class can have subclasses that can inherit all or some of the
characteristics of the class. In relation to each subclass, the class becomes
the superclass. ·
Subclasses can also define their own methods and variables that are
not part of their superclass. ·
The structure of a class and its subclasses is called the class
hierarchy. ·
In the
real world, you'll often find many individual objects all of the same kind.
There may be thousands of other bicycles in existence, all of the same make
and model. Each bicycle was built from the same set of blueprints and
therefore contains the same components. In object-oriented terms, we say that
your bicycle is an instance
of the class of objects
known as bicycles. A class is
the blueprint from which individual objects are created. ·
Syntactically,
a class can be represented as: name : class instance variables ... class variables ... instance methods ... class methods ...
·
A class
is just a template which contains the various attributes and functions of an
object of the class. ·
Example: The following class Bicycle { int cadence = 0; int speed = 0; int gear = 1; void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; } void speedUp(int increment) { speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void printStates() { System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear); }}
The output of this test prints the
ending pedal cadence, speed, and gear for the two bicycles: cadence:50 speed:10 gear:2cadence:40 speed:20 gear:3
·
The fields ·
You may have noticed that the ·
Class diagrams are widely used to describe the types
of objects in a system and their relationships. Class diagrams model class
structure and contents using design elements such as classes, packages and
objects. Class diagrams describe three different perspectives when designing
a system, conceptual, specification, and implementation. These perspectives
become evident as the diagram is created and help solidify the design. ·
The Class diagrams, physical data models, along with
the system overview diagram are in my opinion the most important diagrams
that suite the current day rapid application development requirements. Metaclass ·
In object-oriented programming, a metaclass is a class whose instances are classes. ·
A metaclass is a class whose instances themselves are classes.
This means when we use the instance creation mechanism in a metaclass, the
instance created will itself be a class. ·
The instance creation mechanism of this class can, in turn, be used to
create instances although these instances may or may not themselves be
classes. ·
A meta
class is a class of a class i.e. the objects of this class can themselves act
as classes. So a user can add or remove attributes at run time. ·
Through
this concept the class is applicable at places where users should be able to
add new attributes at runtime; -- a user defined class but at runtime. ·
This
mechanism is not supported directly in C++ as is in Smalltalk and CLOS. But
it can be implemented in C++ using its static data and function members. ·
This
program implements a meta class named ·
The
attributes added to this class are accessible to all its members. The idea is
to give limited access to the objects of this class, to these attributes.
This permission is assigned by assigning specific indexes to each one of the
objects. These indexes correspond to the values (attributes) in the link list
·
Thus a
number of objects of this class can all have different attributes; a kind of
user defined class, at run time. ·
This demonstration version of the meta
class makes use of string values as attributes. A user can use a link-list
instead as the static container object. PtrVec is a STL template and can
contain any type of objects. ·
Example: // MetaVec class definitiontypedef vectorPtrVec; // container objecttypedef PtrVec::iterator itPtrVec;typedef vectorListIndex;typedef ListIndex::iterator itListIndex;typedef long INDEX;typedef long ATTRIBUTE_NO;typedef long NO_OF_ATTRIBUTES;typedef ListIndex LIST_INDEX;class MetaVec {private: // identifer of this object int iClsID; // total count of classes for assigining ClsIDs automatically static int ClsCount; // list of indices for attributes // that are permitted fot this object ListIndex Index; // for locking the object. New attributes // can be added but cannot be used bool ObjectLock; // list of attributes for the metaclass ie all objects static PtrVec VecObject;public: MetaVec(bool LockStatus = false); ~MetaVec(); // Adds a new attributes to the metaclass - in the list of names static long AddAttribute(TCHAR* Name); // This assignes an index to an object // to decide the permissions that object has on // the attributes of the metaclass. Any object can // access only those attributes that // are assigned to it bool AssignIndex(ListIndex LstIndex); // overloaded to receive a single value bool AssignIndex(int iIndex); // removes the assigned index and // returns the removed index -- overloaded INDEX RevokeIndex(int iIndex); // removes a list of assigned indexes and returns the removed list LIST_INDEX RevokeIndex(ListIndex IndexList); // this lists all the available attributes and their IDs static NO_OF_ATTRIBUTES ListAllAttributes(); // Queries an object for its properties - Class ID, Index and Name void QueryObject(); // locks the object so that no new indices can be assigned to it // toggles between locked and un-locked bool LockObject(bool LockState = true); // checking the lock status of an object bool CheckLockStatus();};
Unit-01/Lecture-07
·
Attributes and
Methods Attributes and Methods ·
Attributes - Things that the object stores data in,
generally variables. -
Attributes define the characteristics of a class. -
The set of values of an attribute of a particular object is called its
state. -
In Class Program attribute can be a string or it can be a integer. -
Some of the types of information kept in objects may
be thought of as attributes of the object. Each attribute typically
has a value from a set associated with the attribute. -
Examples of attributes and possible value sets
include:
size {small, medium, large, ....}
shape {polygonal, elliptical, ....}
color {red, blue, green, ....}
border {none, thin, thick, ....} fill {vertical,
horizontal, diagonal, brick, ....} -
These
store information about the object. In the example above we store the fuel
and maxSpeed. The attributes are attached to the classes, and if there are
several instances (objects) of the classes then each will store its own
version of these variables. Note that instead of the usual dim, there is the
word private or public, we'll cover that in the encapsulation section. -
The attributes are the items that make up
the thing being modeled. Visual FoxPro calls these properties. -
In
general OOP terms, an attribute is called an instance variable. An invoice
would have attributes such as date, invoice number, billing address, shipping
address, and line items. -
A class never includes specific information
about an invoice. A class definition would not have information (attributes)
such as the specific date of the invoice, only an attribute that a date would
be included. ·
Methods - Functions
and Procedures attached to an Object and allowing the object to perform
actions. -A method is an operation which can modify objects
behaviour. In other words, it is something that will change an object by
manipulating its variables. -A method is similar to a procedure. -A method as described receives parameters from the caller, performs
the operations defined in the method body, and returns a piece of result (or
void) to the caller.
Fig. 1 Shows Class,
Methods and Attributes -Example: class car private maxSpeed as integer public fuel as integer public sub setSpeed(byVal s as integer) maxSpeed = s end sub public function getSpeed() as integer return maxSpeed end function public sub refuel(byVal x as integer) console.writeline("pumping gas!") fuel = fuel + x end sub public function getFuel() as integer return fuel end function public sub drive() fuel = fuel - 1 end subend class Here class is called car and it has:
-
Unlike
structures, OOP allows you to attach functions and procedures to your code.
This means that not only can you store details about you car (the
attributes), you can also allow for sub routines such as Unit-01/Lecture-08
·
Encapsulation and
Information hiding Encapsulation and
Information hiding ·
By
Information Hiding we mean “Showing
only those details to the outside world which are necessary for the outside
world and hiding all other details from the outside world.” ·
Encapsulation
means “we have enclosed all the characteristics of an object in the object
itself”. ·
Encapsulation
and information hiding are much related concepts (information hiding is
achieved using Encapsulation). ·
Information
Hiding is achieved in Object Oriented Programming using the following
principles: ü All information related to an object is stored
within the object. ü It is hidden from the outside world. ü It can only
be manipulated by the object itself. · Examples of Encapsulation
Consider
the same example of object Ali :
·
A class
encapsulates the name, static attributes and dynamic behaviors into a
"3-compartment box". Once a class is defined, you can seal up the
"box" and put the "box" on the shelve for others to use
and reuse. Anyone can pick up the "box" and use it in their
application. This cannot be done in the traditional procedural-oriented
language like C, as the static attributes (or variables) are scattered over
the entire program and header files. You cannot "cut" out a portion
of C program, plug into another program and expect the program to run without
extensive changes. ·
Encapsulation
is the packing of data and functions into a single component. The features of
encapsulation are supported using classes in most object-oriented programming
languages, although other alternatives also exist. It allows selective hiding
of properties and methods in an object by building an impenetrable wall to
protect the code from accidental corruption. ·
In
programming languages, encapsulation is used to refer to one of two
related but distinct notions, and sometimes to the combination thereof: ü A language
mechanism for restricting access to some of the object's components. ü A language construct
that facilitates the bundling of data with the methods (or other functions) operating on
that data. ·
Member
variables of a class are typically hidden from the outside word (i.e., the
other classes), with ·
This
follows the principle of information hiding.
That is, objects communicate with each others using well-defined interfaces
(public methods). Objects are not allowed to know the implementation details
of others. The implementation details are hidden or encapsulated within the
class. Information hiding facilitates reuse of the class. ·
All C++ programs are composed of following two fundamental elements: ü Program
statements (code): This is the part of a program
that performs actions and they are called functions. ü Program
data: The data is the information of the program which
affected by the program functions. ·
Encapsulation is an Object
Oriented Programming concept that binds together the data and functions that
manipulate the data, and that keeps both safe from outside interference and
misuse. Data encapsulation led to the important OOP concept of data hiding. ·
C++ supports the properties of
encapsulation and data hiding through the creation of user-defined types,
called classes. ·
In computer science,
information hiding is the principle of segregation of the design decisions in a computer program
that are most likely to change, thus protecting other parts of the program
from extensive modification if the design decision is changed. The protection
involves providing a stable interface
which protects the remainder of the program from the implementation (the details that are most likely to
change). ·
Information hiding serves as an effective criterion
for dividing any piece of equipment, software or hardware, into modules of
functionality. For instance a car is a complex piece of equipment. In order
to make the design, manufacturing, and maintenance of a car reasonable, the
complex piece of equipment is divided into modules
with particular interfaces hiding
design decisions. By designing a car in this fashion, a car manufacturer can
also offer various options while still having a vehicle which is economical
to manufacture. ·
Information hiding is one of the most important
principles of OOP inspired from real life which says that all information
should not be accessible to all persons. Private information should only be
accessible to its owner. ·
Encapsulation is
like your bag in which you can keep your pen, book etc. It means this is the
property of encapsulating members and functions. class Bag
{
book;
pen;
ReadBook();
} ·
Encapsulation means hiding the internal details of an
object, i.e. how an object does something. ·
Real Life Examples
of Information Hiding
2.
An email server may have account information
of millions of people but it will share only our account information with us
if we request it to send anyone else accounts information our request will be
refused. 3.
A phone
SIM card may store several phone numbers but we can’t read the numbers
directly from the SIM card rather phone-set reads this information for us and
if the owner of this phone has not allowed others to see the numbers saved in
this phone we will not be able to see those phone numbers using phone. ·
In object oriented programming approach we have
objects with their attributes and behaviors that are hidden from other
classes, so we can say that object oriented programming follows the principle
of information hiding and encapsulation. ·
Encapsulation means to protect sensitive information
in an object by making members protected or private in a class. The user then
has to use functions to change the state of the object, rather than [possibly
incorrectly] modifying the object directly. This helps assure than an object
is always "stable" and can't be corrupted. It also makes classes
much harder to misuse and less likely to cause serious problems in a program. ·
Data hiding is an extreme version of encapsulation
where you not only don't want the user to access the data members, but you
also don't even want them to be able to see what they are. This is typically
accomplished by using a void pointer or a forward declared struct pointer as
the class data.
·
In general, encapsulation is one of the four
fundamentals of OOP (object-oriented
programming). Encapsulation
refers to the bundling of data with the methods that operate on that data.
Encapsulation is used to hide the values or state of a structured data object inside a
class,
preventing unauthorized parties' direct access to them. Publicly accessible
methods are generally provided in the class (so-called getters and setters)
to access the values, and other client classes call these methods to retrieve
and modify the values within the object. Example: class Demo { private int _mark; public int Mark { get { return _mark;
} set { if (_mark
> 0) _mark = value; else _mark = 0; } } }
·
Abstract data types Abstract data types ·
An abstract data type is a class that is meant to serve only as an interface for
derived classes; it cannot be instantiated. ·
They serve as base classes to other classes
that will be instantiated into objects. ·
It is a mathematical model for a certain
class of data
structures that have similar behavior; or for certain data
types of one
or more programming languages that have similar semantics. ·
An ADT is a collection of data and associated operations for
manipulating that data. ·
ADTs support abstraction, encapsulation, and Information hiding. ·
They provide equal attention to data and
operations. ·
An ADT describes a set of objects
sharing the same properties and behaviors ·
The properties of an ADT are its data
(representing the internal state of each object ü double d; -- bits representing
exponent & mantissa are its data or state ·
The behaviors of an ADT are its operations
or functions (operations on each instance) ü sqrt(d) / 2; //operators & functions are its behaviors ·
The data structure can only be accessed with defined
operations.
This set of operations is called interface and is exported by
the entity. An entity with the properties just described is called an abstract
data type (ADT). Figure shows an ADT which consists of an abstract
data structure and operations. Only the operations are viewable from the
outside and define the interface.
Fig. 2 Abstract
Data Type ·
An abstract data type is a model
of a certain kind of data structure e.g. a Stack. A
Stack has push() and pop() operations and that have well-defined behaviour. ·
The abstract data type (ADT) itself refers
to this model, not any particular implementation in any particular
programming language or paradigm. You could implement a Stack
in an object-oriented language, but you could also implement it in a
functional programming language. ·
ADTs allow discussion about the properties of
Stacks, Queues etc that hold for all correct implementations of the ADT. ·
An ADT is a formal description, not code;
independent of any programming language ·
For example, an abstract stack, which is a last-in-first-out
structure, could be defined by three operations: ·
The term abstract data type can also be
regarded as a generalised approach of a number of algebraic structures, such
as lattices, groups, and rings. ·
An abstract data type is defined as a mathematical
model of the data objects that make up a data type as well as the functions
that operate on these objects. There are no standard conventions for defining
them. A broad division may be drawn between "imperative" and
"functional" definition styles. ·
Abstraction
provides
a promise that any implementation of the ADT has certain properties and
abilities; knowing these is all that is required to make use of an ADT
object. The user does not need any technical knowledge of how the
implementation works to use the ADT. In this way, the implementation may be
complex but will be encapsulated in a simple interface when it is actually
used. ·
Code that uses an ADT object will not need to be
edited if the implementation of the ADT is changed. Since any changes to the
implementation must still comply with the interface, and since code using an
ADT may only refer to properties and abilities specified in the interface,
changes may be made to the implementation without requiring any changes in
code where the ADT is used. ·
Different implementations of an ADT, having all the
same properties and abilities, are equivalent and may be used somewhat
interchangeably in code that uses the ADT. ·
Typical
operations Some operations that are often
specified for ADTs (possibly under other names) are
In imperative-style ADT
definitions, one often finds also
The ·
Difference between a language that supports the
implementation of ADTs and an object-oriented language are that
object-oriented languages support inheritance and polymorphism, whereas ADTs
do not necessarily include these features. (Inheritance and polymorphism will
be discussed later.) A language that supports ADTs but is not object-oriented
is sometimes called an object-based language. An object-oriented
language by definition supports ADTs. ·
USER DEFINED ABSTRACT DATA TYPES ü The concept of user-defined abstract data types is
relatively recent. ü They should provide: -
A type definition that allows program units to declare variables of the type
but hides the representation of these variables -
A set of operations for manipulating objects of the type ü An abstract data type is a data type that
satisfies two conditions -
The representation, or definition, of the type and the operations are
contained in a single syntactic unit -
The representation of objects of the type is hidden from the program units
that use the type, so only direct operations possible on those objects are
those provided in the type’s definition ü
Program units
that use a specific abstract data type are called clients of that
type. ü
A benefit of information hiding is increased
reliability. This is because clients
cannot change the underlying representations of objects directly, either
intentionally or by accident, thus increasing the integrity of the object ·
ADTs allows the creation of instances with
well-defined properties and behaviour. In object-orientation ADTs are
referred to as classes. Therefore a class defines properties
of objects
which are the instances in an object-oriented environment. ·
ADTs define functionality by putting main emphasis
on the involved data, their structure, operations as well as axioms and
preconditions. Consequently, object-oriented programming is ``programming
with ADTs'': combining functionality of different ADTs to solve a problem.
Therefore instances (objects) of ADTs (classes) are dynamically created,
destroyed and used. ·
As ADTs provide an abstract view to describe
properties of sets of entities, their use is independent from a particular
programming language. We therefore introduce a notation here which is adopted
from [3]. Each ADT description consists of two parts: ü Data: This part describes the structure of the data used in the ADT in an
informal way. ü Operations: This part describes valid operations for this ADT, hence, it
describes its interface. We use the special operation constructor to
describe the actions which are to be performed once an entity of this ADT is
created and destructor to describe the actions
which are to be performed once an entity is destroyed. For each operation the
provided arguments as well as preconditions and post conditions are given. ·
ADTs allow the creation of instances with
well-defined properties and behavior. In object-orientation ADTs are referred
to as classes.
Therefore a class defines properties of objects which are the instances in an
object-oriented environment. ·
ADTs define functionality by putting main emphasis
on the involved data, their structure, operations as well as axioms and
preconditions. Consequently, object-oriented programming is ``programming
with ADTs'': combining functionality of different ADTs to solve a problem.
Therefore instances (objects) of ADTs (classes) are dynamically created,
destroyed and used. ·
Common
examples of ADTs: –Built-in types : Boolean, integer, real, array –User-defined types: stack,
queue, tree, list
Inline
Function ·
In C++ inline function is powerful
concept that is commonly used with classes. If a function is inline, the
compiler places a copy of the code of that function at each point where the
function is called at compile time. ·
Any change to an inline function could require all clients of the
function to be recompiled because compiler would need to replace all the code
once again otherwise it will continue with old functionality. ·
To inline a function, place the keyword inline before the
function name and define the function before any calls are made to the
function. The compiler can ignore the inline qualifier in case defined
function is more than a line. ·
A function definition in a class definition is an inline function
definition, even without the use of the inline specifier. ·
All the member functions defined inside the class definition are by
default inline, but you can also make any non-class function inline by using
keyword inline with them. ·
Inline functions are actual functions, which are copied everywhere
during compilation, like preprocessor macro, so the overhead of function
calling is reduced. ·
For an inline function, declaration and definition must be done
together. For example, inline void fun(int a) { Return a++; } ·
We must keep inline functions small, small inline functions have
better efficiency. ·
The compiler is unable to perform inlining if the function is too
complicated. So we must avoid big looping conditions inside such functions.
In case of inline functions, entire function body is inserted in place of
each call, so if the function is large it will affect speed and memory badly.
·
Following is an example, which makes use of inline function to return
max of two numbers: #include <iostream> using namespace std; inline int Max(int x, int y) { return (x > y)? x : y; } // Main function for the program int main( ) { cout << "Max (20,10): " << Max(20,10) << endl; cout << "Max (0,200): " << Max(0,200) << endl; cout << "Max (100,1010): " << Max(100,1010) << endl; return 0; } When the above code is compiled and executed, it
produces the following result: Max (20,10): 20Max (0,200): 200Max (100,1010): 1010
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|