ADVANCED DATA STRUCTURES Veera Shekar G UNIT - I Page 1 UNIT - I INTRODUCTION For a C progr ammer it is eas y for hi m to move towards C++ because syntacticall y there is no big difference between C and C++. But you should note that conceptually there is a l ot of difference. C is based on traditi onal approach that is procedure o ri entation but C++ is based on modern approach that is object orientation. Object oriented programming was developed to reduce the inherent limitations oftraditional programming languages. Large programs written in traditional programming languages are complex and prone to errors . Traditional languages like BASIC, Pascal, and C etc. are procedural languages. Programs written in these languages consist of set of instructions. Each instruction tells the computer to do something, read data from keyboard, do some computation, print some output on screen etc. Small programming tasks could be managed with programs consisti ng o f a singl e sequence o f instructions DIFF ERENT STRATEGIES FO R PROBLEM SOLVING The ability of software to do millions of different things is one of reasons for its downfall. Software crisis is result of various factors like less qualitative projects, customer dissat isfaction, over budg et, schedul es no met etc. Software crisi s pred icted three and half decades (first identified in 1968 by NATO) back but never materialized. A general scenario can be seen here that is software failures receive more publicity than successful projects. But truly speaking success rate for software projects is only 10%. The major reasons are unpredictability about time and budget. Response to software crisis is laid foundation for construction of software development models, different programming pri ncip les li ke structured, object o rientation comes in to pictures. There are di fferent strategies for problem so l vin g. Some o f the strategies are 1.Mo noli thi c Programmin g 2.Proc edural Programmin g 3.Structured Programmin g 4.Object Ori ented Programming
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
For a C programmer it is easy for him to move towards C++ because syntactically
there is no big difference between C and C++. But you should note that conceptually
there is a lot of difference. C is based on traditional approach that is procedure orientation
but C++ is based on modern approach that is object orientation.
Object oriented programming was developed to reduce the inherent limitations of
traditional programming languages. Large programs written in traditional programming
languages are complex and prone to errors .
Traditional languages like BASIC, Pascal, and C etc. are procedural languages.
Programs written in these languages consist of set of instructions. Each instruction tells
the computer to do something, read data from keyboard, do some computation, printsome output on screen etc. Small programming tasks could be managed with programs
consisting of a single sequence of instructions
DIFFERENT STRATEGIES FOR PROBLEM SOLVING
The ability of software to do millions of different things is one of reasons for its
downfall. Software crisis is result of various factors like less qualitative projects, customer
dissatisfaction, over budget, schedules no met etc. Software crisis predicted three and
half decades (first identified in 1968 by NATO) back but never materialized. A generascenario can be seen here that is software failures receive more publicity than successfu
projects. But truly speaking success rate for software projects is only 10%. The major
reasons are unpredictability about time and budget. Response to software crisis is laid
foundation for construction of software development models, different programming
principles like structured, object orientation comes into pictures.
There are different strategies for problem solving. Some of the strategies are
Global data that is shared by entire program without any subprograms concept is
called as Monolithic Programming. Traditional BASIC is the best example for monolithic
programming.
To solve a big problem, it is better to divide into smaller pieces and then start
solving it is key fact behind procedural programming. Large programs are divided into subprograms and each sub program is responsible for solving a particular task of the main
problem. All these sub programs are usually share same global data. COBOL is the best
example for procedural programming.
Grouping of related tasks into single unit is called module, group of these modules
solve the large program. This concept is base for structured programming. These modules
will share global data. Pascal and C programming languages are best examples of
According to above equation in procedure oriented Programming structures first
preference given to algorithms and then only focus is given to Data. Where as in object
oriented programming structures first preference is given to Data, then algorithms takencare. The following table shows types of programming techniques and their main
Object oriented programming language the name implies total language based on
objects. An object is any real world entity which is having state, identity and behavior. Fo
example, consider mouse as an object, its current state may be moving, or working , or
picking, or dropping etc. its identity may be its color, company, identification number,
three-button or rolling etc. Mouse behavior is it used for picking and pointing objects onthe screen. State represents current condition of the object, identity refers how it differs
from similar kind of objects, and behavior tells the working principle of that particular
object.
PARADIGMS OF PROGRAMMING LANGUAGES
The term paradigm describes a set of techniques, methods. Theories and standards
that together represent a way of thinking for problems solving according to [wegner
1988], paradigms are “pattern of thought for problem solving”.
Language paradigms were associated with classes of languages. First the paradigmsare defined. Thereafter, programming languages according to different are classified.
The language paradigms are divided into two parts
1. Imperative
2. Declarative
Paradigms as shown in below figure Imperative languages can be further classified into
Procedural and Object oriented approach. Declarative languages can be classified into
functional and logical languages. In figure the examples of languages in each category are
Before elaborating these concepts we should know about the difference betweenobject based and object oriented programming languages. Programming that supports
only encapsulation it is called as Object based programming language . An object based
programming language can be called as Object oriented programming language when it
Class is a blueprint for an object. Similar objects with their common behavior are
grouped together is called as Class. For example peacock, parrot, hen , robin, duck all areobject grouped together is called as birds class. The set of data and its code can be made
as a user defined type with the help of class. Once class is defined, you can create as
many numbers of objects as you want. A class is collection of objects of similar type.
Consider another example a fruit class. Objects under fruit class are mango, banana,
apple and orange. Object of fruit class is declared as follows;
Fruit apple;
More objects at a time if you want to declare you can,
Fruit mango,banana,orange
Data hiding:
Hiding the implementation details from the user is called as data hiding. To drive a
car you should know how to drive it. There is no need of knowing manufacturing details
like how many nuts and bolts used in its manufacturing.
For example, you created an object for employee class as follows.
Employee emp;
There is a function called calGross to calculate the gross salary of an employee. You
can call this function simply as follows
emp.calGross();
Implementation details and calculation details is not required for the user. Just his
purpose is to use that function and get the result. Hiding of this kind of implementation
The essential characteristics of an object that distinguish it from all other kinds of
objects are called data abstraction. With help of data abstractions we can create new data
types that are suitable for applications. In C++,class used for creating user defined data
type that is called as Abstract Data Type(ADT).
Essential characteristics (data and functions) depend on perspective of the viewer.
For example, a technician view of the car is different from its owner. Technician required
characteristics for a car in general working of engine, battery, condition etc. Owners view
is cost, mileage, model etc.
Simply consider a student object. A faculty view for student object is rollno, name,
date of joining, fees particulars etc.
2. Encapsulation
Combination of data and its related functions is called encapsulation. The process
for implementing data abstractions in a programming language comes under as
encapsulation. In C++, this is supported by class. An instance of a class is known as
object. The following equation represents encapsulation
Encapsulation = Data + Operations
In more specific way ,
Encapsulation = [private] Data + [public] Operations
3. Inheritance
The main purpose of object orientation is its reusability that can be achieved
through inheritance. Extending a existing class is called Inheritance. With the help of
inheritance, child class can get all the operations of parent class. In addition to the
existing details a child may have additional data members and operations. Concept of
inheritance is also known as class hierarchy. Inheritance allows the extension and reuse of
existing code without developing from scratch.
For example consider a player class. Further you have different player classes, like
cricket player class, football player class etc. Also cricket player class can be extended toBowler class, Batsman class , Wicket keeper class etc. Player class contains common
things required for all players like player name, country etc. Whereas cricket player class
additional parameters like runs, wickets, catches, average run-rate etc. Football player
class contains additional parameters like goals, points etc.
The following diagram shows this inheritance relationship.
4. Polymorphism
Another important feature of object orientation is polymorphism. In
polymorphism, poly represents many and morph represents forms. Many forms of an
entity called polymorphism. This allows an operator or function responding in differentmanners. For example area() function may compute area of rectangle if you provide
two parameters, or compute area of circle if you have single parameter. Hence, same
area() function works in two different ways. This is called as function overloading
Other forms of polymorphism are operator overloading, dynamic binding (virtua
functions).
Data hiding, data abstraction, encapsulation, inheritance and polymorphism are
Today there are several languages that are implementing object oriented features
Smalltalk is considered as pure object oriented language because in that language
everything is object. In a pure object oriented language all actions are produced by
passing message. Messages are requests made by another object or same object through
operations. C++ can be considered as hybrid language because it has both procedureoriented features along with the object oriented features. There are two reasons for
popularity of C++, one in C++ provides several constructs which fully support object
oriented features. Second reason in C++ is an extension of popular standardized language
that is C.
HISTORY OF C++
Most of traditional approaches for problem solving failed at the later stages of the
project. Once we started solving a problem, if the problem is solved within 6 months toone year then we can’t face major drawbacks of the programming language. Same thing
happens to c programming language. When it was introduced in 1970s everybody thought
that it is a general purpose programming language so it can handle any type of problem.
No other programming language grabbed popularity in the market for a decade (1973-
1983). Major problem encountered when long run large projects based on c failed in the
middle or last stages of project development. It was very difficult for programmers to
modify the entire code after identifying their flaw. So there is a need of alternative
programming language which can handle large projects. In 1970s there were two
important languages in the market with less popularity. They are simula 67 and algol 68.
In Simula 67 and Algol 68 languages key features were very important but the
problem was their basic syntax. Bjarne Strousstrup at AT & T Bell laboratories identified
the solution to find new programming language. Popular programming language C syntax
was already familiar throughout the world, so adding the concepts or key ideas of simula
67 and algol 68 with C results to that new programming language. Many of us knows that
C language was based on B language. So what should be the name of the new language?
Is it D? Strousstrup was not modifying the C language; he was adding other concepts to C
language that means it was an extension language of C. Earlier versions of this language
known as “C with classes”. He wants a new name , a new name came into his mind it was
C++. The reason behind not to put C+ language is C plus is a syntax error in the C
language. Next number to C(according to short circuit operator C= C+1) is C++, so he
confirmed the name as C++. Following gives the hierarchy of development of C++
A structure groups together data elements of different types. A function groups
together program statements that perform a specific action into a named unit. A classgroups together data elements and functions that act on these data elements into a single
logical unit. A class is perhaps the most important user defined data type in C++.
A class description consists of two parts:
a) Class definition of the component members both data and function members.
b) Class implementation or definition of class member functions.
Class is a blueprint for an object. Similar objects with their common behavior are
grouped together is called as Class. For example peacock, parrot, hen, robin, duck all are
object grouped together is called as birds class. The set of data and its code can be madeas a user defined type with the help of class. Once class is defined, you can create as
A class is a set of operations (public) and a set of data (private) representing abstract
model. C++ structures also provide the facility for holding operations along with access
specifiers. Difference between C++ structures and C++ classes is default access specifier
in C++ structure is public and default access specifier in C++ class is private. Generally if
you want to represent traditional structures use the C kind of structure model. To
represent object oriented model encapsulation it is recommended to use class way of
definition. Syntax of class is as follows:
class ClassName
{
access-specifier:
data
operations
access-specifier:
data
operations
-----
------
};
The class definition starts with keyword that follows name of the class. The body of
a class enclosed in curly braces followed by semicolon. Body of class contains data and
operations collectively known as members. Data is defined as variables and those are
called as data members and functions are defined in the body of the class are called asmember functions. All these members are headed under access-specifiers. There are three
different access specifiers in C++;they are public, private and protected.
These access specifiers define the visibility of members.
The private data members can be accessed only with in the class. The public data
members can be accessed with in the class as well as outside the class. The default access