0% found this document useful (0 votes)
31 views160 pages

II PUC Computer Science Textbook 191-480

..

Uploaded by

Asma Khayum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views160 pages

II PUC Computer Science Textbook 191-480

..

Uploaded by

Asma Khayum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 160

Review of C++171 171

Example : Consider the declaration, char a[5];


The element a[0] is allocated at a particular memory location, the element
a[1] is allocated at next memory location and so forth. Since the array is of the type
char, each element requires 1-byte.
Two-dimensional arrays
It is an array in which each element is accessed using 2-subsripts. The
subscripts represent the position of the elements in the array.
The elements of two dimensional arrays are represented as rows and
columns. To identify any element, we should know its row-number and column-
number.
Declaration of two-dimensional array:
Syntax datatype array-name[row-size][column-size];
Example : int a[2][3];
Initialization of two-dimensional arrays
Example : int a[2][3] = {1, 2, 3, 4, 5, 6};
a is a two dimensional array which contains 2 rows and 3 columns and these
assignments would be
a[0][0] = 1 a[0][1] = 2 a[0][2] = 3
a[1][0] = 4 a[1][1] = 5 a[1][2] = 6
If the values are missing in an initialize, they are automatically set to 0.
Example : int b[2][3] = {
{1, 2},
{3}
};
will initialize the first two elements to the first row, the next element to the second
row. The remaining elements are automatically set 0.
b[0][0] = 1 b[0][1] = 2 b[0][2] = 0
b[1][0] = 3 b[1][1] = 0 b[1][2] = 0
Multi-dimensional array
A multidimensional array is an array of n-dimensions. In other words, an
array of arrays is called a multidimensional array. A one-dimensional array of one-
dimensional arrays is called a two-dimensional array; a one-dimensional array to
two-dimensional arrays is called a three-dimensional array and so on.
172 Review of C++

5.9 FUNCTIONS
If the programs are complex and lengthy, they can be modularized into
subprograms. The subprograms are called as functions. The subprograms can be
developed independently, compiled and tested. They can be reused in other
programs also.
A function is a named group of statements developed to solve a sub-problem
and returns always a value to other functions when it is called.
Types of functions
There are two types of functions:
i. Library functions
ii. User-defined functions.
i. Library functions
A standard library is a collection of pre-defined functions and other
programming elements which are accessed through header files.
Header files are the files containing standard functions that our programs
may use. This chapter contains the information about some header files of C++
standard library and some functions of it. The header files should be written within
angled brackets and its functions are included into our programs by #include
directive.
User-defined functions
We can create our own functions or sub-programs to solve our problem.
Such functions are normally referred to as user defined functions.
A user-defined function is a complete and independent program, which
can be used (or invoked) by the main program or by the other sub-programs. The
user-defined functions are written to perform definite calculations, after performing
their task they send back the result to the calling program or sub-program.
Different header files
As said earlier, header files are the files containing standard functions
that our programs may use. C++ contains many header files and is listed below.
stdio.h
This header file contains functions and macros to perform standard I/O
operations. When we include the header file iostream.h, the header file stdio.h is
automatically included into our program.
string.h
This header file declares functions to manipulate strings.
stdlib.h
This header file is used to declare conversion routines, search/sort routines
and other miscellaneous things.
Review of C++173 173
iostream.h
This header file contains C++ streams and i/o routines.
iomanip.h
This header file contains functions and macros for I/O manipulators for
creating parameterized manipulations.
math.h
This header file declares prototypes for the mathematical functions and
error handlers. The functions are used to perform mathematical calculations.
Mathematical library functions
C++ provides many mathematical functions. These functions can be used
in mathematical expressions and statements. The various functions are ceil(), exp(),
fabs(), floor(), log(), pow() etc.
Character and string functions
A character is any single character enclosed within single quotes. Some
functions accept a character as argument. The argument is processed as an int by
using its ASCII code. To use these functions, the header file ctype.h should be
included.
Inputting single character
We can input a character using the function get( ).
char ch; char ch;
cin = get(ch); OR ch = cin.get( );
Outputting single character
cout.put(ch);
put() function is used to display single character.
The general form is
Example : char ch; ch = cin.get(); cout.put(ch);
String functions
A string is sequence of characters enclosed within double quotes. Strings
are manipulated as one-dimensional array of characters and terminated by null
(‘\0’) character. C++ provides many functions to manipulate strings. To use these
functions, the header file string.h should be included.
char string-name[size];
Declaring a string variable
The general form to declare a string is:
Ø string-name is the name of the string variable
174 Review of C++

Ø Size is the number of characters in the string. The size helps the compiler
to allocate required number of memory locations to store the string.
Example : char st[50];
Initializing a string
Like other variables, strings can also be initialized when they are declared.
Example : char s[10] = “Karnataka”;
There are only 9 characters in the string. The null character (‘\0’) is
automatically appended to the end of the string.
Inputting a string
C++ provides the function getline() to read a string.
cin.getline(string, size);
Example cin.getline(st, 25);
Outputting a string
C++ provides the function write() to output a string.
cout.write(string, size);
Example : cout.write(st, 25);
Some string manipulation functions are given below:
strlen() function
This function returns the length of the string. i.e., the number of characters
present in the string, excluding the null character.
The general form is variable = strlen(string);
Example 12.9: l = strlen(“Empress”); Returns 7.
strcat() function
This function is used to concatenate two strings. The process of combining
two strings to form a string is called as concatenation.
strcpy() function
A string cannot be copied to another string by using assignment statement.
The function strcpy() is used to copy a string into another string.
strcmp() function
This function is used to alphabetically compare a string with another string.
This function is case-sensitive. i.e., it treats the uppercase letters and lowercase
letters as different.
strcmpi() function
Review of C++175 175
This function is used to alphabetically compare a string with another string.
This function is not case-sensitive. i.e., it treats the uppercase letters and lowercase
letters as same.
strrev() function
This function is used to reverse the characters in a string.
5.10 USER DEFINED FUNCTIONS
Definition
User-defined function is a function defined by the user to solve his/her
problem. Such a function can be called (or invoked) from anywhere and any number
of times in the program.
Function definition or structure of user-defined function
Function-header
Any user-defined function has the following structure.
return-type-specifier function-name(argument-list with declaration)
{
Local-variable-declarations;
Executable-statement-1;
Body of the function
Executable-statement-2;
………..
Executable-statement-n;
return(expression);
}
Ø Return-type-specifier is the data type of the value return by the function
to anther function when it is called. The return-type-specifier can be char, int, float
or void. The data type void is used when the function return no value to the calling
function.
Ø Function-name is the name of the function. It is an identifier to identify
the particular function in a program.
Ø Argument-list with declaration is the list of arguments or parameters or
variables with their declaration. Each argument should be declared separately.
Each declaration should be separated by comma. The list should be enclosed within
parenthesis.
Ø The complete line is called the function header. Note that there is no
semicolon at the end.
Ø Local-variable declaration is the declaration of the variables that are used
within the function. Since these variables are local to the function, these variables
are called as local variables.
Ø Executable-statements are the statements that perform the necessary
operations to solve the problem. If the function is returning a value, a return
176 Review of C++

statement should be included. Otherwise, return statement is not necessary.


Ø Local declaration and executable statements are together called as body
of the function. The body of the function should be enclosed within the curled
braces.
Calling a function
variable = function-name(argument-list);
OR
variable = function-name();
A function can be called by specifying its name, followed by list of
arguments enclosed within the parenthesis. The arguments should be separated
by commas. If the function does not pass any arguments, then an empty pair of
parenthesis should follow the name of the function.
The function call should be a simple expression such as an assignment
statement or it may be part of a complex expression.
Example : big = biggest(a, b, c);
main() function
In C++, the main() function returns a value of type int to the operating
system. If the program runs successfully, 0 is returned. Otherwise, a non-zero
value is returned to the operating system, indicating that the program contains
errors. If the main() function is not returning a value, the datatype void can be
used as return-type-specifier.
The general form of main() function is:
int main() void main()
{ {
Executable-statements; OR Executable-statements;
return 0; }
}
Returning a value
When a function is called, the statements in the called function are
executed. After executing the statements, the function returns a value to the calling
function. The return statement is used to return a value. A function can return only
one value or none to the calling function, for every function call.
The general form of return statement is:
return(expression); OR return 0;
Function prototypes
Like all variables are declared before they are used in the statements, the
function should also be declared. The function prototype is a declaration of the
Review of C++177 177
function in the calling function.
The general form of function prototype is
return-type-specifier function-name(type arg1, type arg2, ……);
OR
return-type-specifier function-name(type , type , ……);
Example : float volume(int x, float y, float z);
Or float volume(int, float, float);
Types of arguments
A variable in a function header is called an argument. The arguments are
used to pass information from the calling function to the called function.
Actual arguments
The function call statement contains name of the function and list of
arguments to be passed. These arguments or parameters are called as actual
arguments. The arguments can be constants, variables or expressions. Actual
arguments have values stored in them before the function call hence the name
actual.
Example : In the function call g = gcd(a, b);
Formal arguments
The function header contains return-type-specifier, function name and
list of arguments with their declaration. These arguments are called as formal
arguments or dummy arguments. Formal arguments get their values from the actual
arguments.
Example: In the function header int gcd(int x, int y)
x and y are the formal arguments.
Local variables
The variables declared inside function or block is said belong to that block.
These variables are called as Local variables. Values of local variables are accessible
only in that block. The function’s formal arguments are also considered as local
variables.
Global variables
The variables declared outside the function are called as global variables.
These variables are referred by the same data type and same name throughout the
program in both the calling function and called function. Whenever if some variables
are to be treated as same value in both main() and in other functions, it is advisable
to use global variables.
The availability of the values of global variables starts from the point of
definition to the rest of the program.
178 Review of C++

Types of Functions :
There are 5 types of functions.
i. Functions with no arguments and no return values
ii. Functions with arguments and with no return values
iii. Functions with no arguments and with return values
iv. Functions with arguments and with return values
v. Recursive functions
Functions with no arguments and with no return values
In this method, the function simply performs an independent task. The
function does not receive or send any arguments.
Example : void natural()
{
for(int i=1; i <= 10; i++)
cout<<setw(4)<<i;
}
Functions with arguments and with no return values
In this method, the function receives some arguments and does not return
any value.
Example : void average(int x, int y, int z)
{
float sum, avg;
sum = a + b + c;
avg = sum/3.0;
cout<<“Average = “avg<<endl;
}
Functions with no arguments and with return values
In this method, the function receives no arguments but return a value.
Example : int greatest()
{
if(a>b)
return(a);
return(b);
}
Functions with arguments and with return values
In this method, the function receives some arguments and returns a value.
Example : float interest(float p, float t, float r)
{
si = (p * t * r)/100;
return(si);
}
Review of C++179 179
Recursive functions
Recursive function is a function that calls itself. The process of calling a
function by itself is called as recursion.
Recursive functions must have one or more terminating conditions to
terminate recursion. Otherwise, recursion will become infinite.
Passing default arguments to functions
In C++, to call a function we need not pass all the values for the arguments
to a function from the calling function. It allows us to assign default values to the
formal arguments.
Example : Consider the prototype
float interest (float amt, int time, float rate = 0.15);
0.15 is the default value provided to the argument rate.
The function call statement can be as follows:
si = interest( 5000,5 ); // third argument is missing
Default values should be assigned only in the function prototype. It should not be
repeated in the function definition.
Passing constant arguments
In C++, we can declare some arguments as constants. The compiler cannot
modify the arguments marked as constants.
Example : int strlen(const char *p);
int total( constint x, const int y);
Pass by value or Call by value
A function can be called by passing arguments from the calling function
into the called function. Thus the data is transferred through argument list.
Pass by reference or call by reference
We can pass parameters to the function by using reference variables. When
we pass arguments by reference, the formal arguments in the called function become
the aliases to the actual arguments of the calling function. i.e., the called function
is actually uses the original data with a different name.
Passing arrays to functions
To pass an array to a function, we just pass the name of the array to the
function. i.e., we are referring the address of the first element of the array. Using
this address, the function can access all the elements of the array.
Passing structures to functions
We can pass structures to functions as we pass other arguments. Structures
are passed to functions in pass-by-value method. i.e., the function works with copy
of the structures. The function can also return a structure after processing it.
180 Review of C++

Whenever we pass the address of the structure to the function, we should include
the address-of (&) operator.
5.11 Structures
A structure is a collection of simple variables. The variables in a structure can be of
same or different types: Some can be int, some can be float and so on. The data
items in a structure are called the members of the structure.
Defining a structure
The process of defining a structure is equivalent to defining your own data
type.
struct structure-name
{
datatype member-name-1;
datatype member-name-2;
……………
datatype member-name-n;
};
Example: A structure definition to hold employee information.
struct employee
{
int idno;
char name[15];
char designation[10];
float salary;
};
Basic concepts of OOP 181
Chapter 6
BASIC CONCEPTS OF OOP

Objectives:

 Provides an overview of object oriented programming


 To understand concept of objects, classes and other related
terminologies
 To highlight advantages of OOP
 To highlight limitations of OOP
 To identify the application areas
182 Basic concepts of OOP

6.1 Introduction
Object oriented programming is the principle of design and development of
programs using modular approach. Object oriented programming approach
provides advantages in creation and development of software for real life
applications. The advantage is that small modules of program can be developed
in shorter span of time and these modules can be shared by a number of
applications. The basic element of object oriented programming is the data. The
programs are built by combining data and functions that operate on the data. In
this chapter we learn about the basic concepts, advantages and terminologies
used in Object oriented programming. Some of the object oriented programming
languages are C ++, Java, C # and so on.
The object oriented programming methods use data as the main element in
the program. The data is tied to the function that operates on the data and the
other functions cannot modify the data tied to a given function. Thus in object
oriented programming, a problem is decomposed into a number of components
called objects. An object is a collection of set of data known as member data and
the functions that operate on these data a known as member functions or
Methods. The member data are encapsulated in an object and then can be
accessed or modified only by the member functions. An object can be accessed
only if permitted by other member functions. Various objects of a program can
interact with each other by sending messages.
Object oriented programming methods modularize a program by creating
memory area for data and member functions (methods) together as a single
entity. All objects are created according to the specifications of the entity defined.
Object is the basic unit of OOP. To design OO Model, first a set of classes are
defined. A class is a Template from which objects are created. The Template of a
class specifies the data , member functions and their attributes.
Basic concepts of OOP 183

6.2 Basic concepts of object oriented programming


The following are the major characteristics of any object oriented
programming language. They are
 Objects
 Classes
 Data abstraction
 Data encapsulation
 Inheritance
 Overloading
 Polymorphism
 Dynamic binding
 Message passing

6.2.1 Objects
Objects are basic building blocks for designing programs. An object may
represent a person, place or a table of data. An object is a collection of data
members and associated member functions. Each object is identified by a unique
name. Every object must be a member of a particular class.
Ex: Apple, orange, mango are the objects of class fruit.
Objects take up space in memory and have address associated with them.
For example, structure variable in a C program.
At the time of execution of a program, the objects interact by sending messages
to one another. The objects can interact with one another without having to
know the details of data or functions within an object.

Objects

6.2.2 Classes
The objects can contain data and code to manipulate the data. The objects
can be made user defined data types with the help of a class. Therefore objects
184 Basic concepts of OOP

are variables of the type class. A class is a way of


grouping objects having similar characteristics. Once
a class is defined, any number of objects of that class
are created.
For example, planets, sun, moon are members of
class solar system.
Classes are user defined data types. A class can
hold both data and functions.

6.2.3 Data abstraction


Data abstraction permits the user to use an object without knowing its
internal working. Abstraction refers to the process of representing essential
features without including background details or explanations. Classes use the
concept of abstraction and are defined as a list of abstract attributes such as
size, weight and cost, and functions to operate on these attributes.
6.2.4 Data encapsulation
Data encapsulation combines data and functions
into a single unit called class. Data encapsulation will
prevent direct access to data. The data can be accessed
only through methods (function) present inside the
class. The data cannot be modified by an external non-
member function of a class. Data encapsulation
enables data hiding or information hiding.

6.2.5 Inheritance
In OOP, the concept of inheritance provides the
idea of reusability. This means that we can add
additional features to an existing class without
modifying it. Thus the process of forming a new
class from an existing class is known as
Inheritance. The objects of one class acquire the
properties of another class through inheritance.
The existing class is known as base class. The new class is known as derived
class.
The derived class shares some of the properties of the base class. Therefore
a code from a base class can be reused by a derived class. In addition to this the
new class may combine features from two different base classes too.
Basic concepts of OOP 185
In single inheritance, each subclass has only one superclass. In Multiple
inheritance, each subclass has more than one super class.
6.2.6 Overloading
Overloading allows objects to have different meaning depending upon
context. There are 2 types of overloading namely
1. Operator overloading
2. Function overloading
When an existing operator operates on new data type, it is called operator
overloading.
Function overloading means two or more functions have same name ,but
differ in the number of arguments or data type of arguments. Therefore it is said
that (function name) is overloaded. Function overloading therefore is the pro-
cess of defining same function name to carry out similar types of activities with
various data items.
6.2.7 Polymorphism
Polymorphism is a feature of object oriented programming where a function
can take multiple forms based on the type of arguments, number of arguments
and data type of return value.
The ability of an operator and function to take multiple forms is known as
polymorphism.
Example 1.2: Consider the addition operation. In addition of 2 numbers
the result is the sum of 2 numbers.
In addition of 2 strings the operation is string concatenation. When an
operator behaves differently based on operands, then it is said that operator is
overloaded. Similarly when same function is used for multiple tasks in the same
program by changing argument type and number, it is known as function
overloading.
6.2.8 Dynamic Binding
Binding is the process of connecting one program to another. Dynamic
binding means code associated with a procedure call is known only at the time
of program execution routine.
6.2.9 Message Passing
In OOP, processing is done by sending messages to objects. A message for
an object is request for execution of procedure. The request will involve a
procedure (function) in the receiving object that generates desired results.
Message passing involves specifying the name of object, the name of the function
(message) and the information to be sent.
186 Basic concepts of OOP

6.3 Advantages of OOP over earlier programming methods


 The programs are modularized based on the principle of classes and objects.
 Linking code & object allows related objects to share common code. This
reduces code duplication and code reusability.
 Data is encapsulated along with functions. Therefore external non- member
function cannot access or modify data, thus providing data security.
 Easier to develop complex software, because complexity can be minimized
through inheritance.
 The concept of data abstraction separates object specification and object
implementation.
 Creation and implementation of OOP code is easy and reduces software
development time.
 OOP can communicate through message passing which makes interface
description with outside system very simple.
6.4 Limitations of OOP
The main disadvantages of using Object oriented programming are:
 OOP software is not having set standards.
 The adaptability of flow diagrams and object oriented programming using
classes and objects is a complex process.
 To convert a real world problem into an object oriented model is difficult.
 The classes are overly generalized.
6.5 Applications of object oriented programming
Object oriented programming approach is an easier method to design and
implement programs. The programs are easier to upgrade and modify. The
standard class libraries can be used by the programmers so that development
time is minimized. The graphical user interface design for windows operating
system using object oriented programming is the most interesting feature of
programming. The common application areas of Object oriented programming
are:
 Computer graphic applications
 CAD/CAM software
 Object –oriented Database
 User Interface design such as windows
 Real-time systems
 Simulation and Modeling
 Artificial intelligence and expert systems
Basic concepts of OOP 187
Points to remember
 Object oriented programming: Object oriented programming is a
programming paradigm that uses “objects” to design applications and
computer programs. The OOP uses several techniques such as
inheritance, abstraction, modularity, polymorphism and encapsulation.
 Object: Object represents data and associated functions as a single
unit.
 Class: A class is a way of grouping objects having similar characteristics.
 Abstraction: Abstraction refers to the representation of essential features
of an object as a program object. An abstract class defines an interface,
but does not provide implementation details.
 Encapsulation: It is a way of combining data and associated functions
into a single unit. Encapsulation implements abstraction.
 Inheritance: It is the capability of a class to inherit the properties of
another class. The class that inherits the properties from another class
is known as derived or subclass. The class that provides its properties to
subclass is known as base class.
 Polymorphism: It is ability of a function to have same name and multiple
forms. The appropriate function is called automatically by the compiler
depending on the number and type of arguments.
 Message passing: The processing of data in object oriented programming
is carried out by sending messages to objects.
One mark questions:
1. What is the fundamental idea of object oriented programming?
2. What is an object?
3. Define the term class.
4. Define the term data abstraction
5. What is encapsulation?
6. What is meant by function overloading?
7. Define polymorphism
8. What is inheritance?
9. What is a base class?
10. What is a derived class?
11. How are base class and derived class related?
12. Define the term data hiding
Two marks questions:
1. What is the significance of classes in OOP?
2. What is the difference between program module and an object?
3. Mention different types of inheritance.
188 Basic concepts of OOP

4. Mention any two advantages of object oriented programming over


earlier programming methods.
Three mark questions
1. Briefly discuss the classes and objects.
2. Explain inheritance
3. Write short notes on polymorphism.
4. Mention any 4 high level languages that follow object oriented
programming approach.
Five marks answer questions
1. Write the differences between procedural programming and object
oriented programming
2. Explain advantages OOPs
3. Write the disadvantages of object oriented programming
4. Write the real life applications of object oriented programming.
Classes and objects 189

Chapter 7
Classes and objects

Objectives:

 To understand the need of classes.


 To understand the definition and declaration of classes
 To understand the use of members functions inside and outside
classes
 To understand the process of accesing and manipulating the members
of class
 To illustrate use of classes through application of simple programs.
190 Classes and objects

7.1 Introduction
In the previous chapter we have learnt the fundamentals units of an object
oriented programming. In this chapter let us understand the basic units of data
representation for development of any object oriented programming namely
classes and objects.
Real world objects can be represented as data variables in computer domain.
Data variables are identified as account numbers, balance_payment,
Employee_code and so on. When these real world objects are represented as
data in computers, they can also be manipulated or processed by using functions
in C++. For instance account information of a customer in a bank can be updated,
balance_payment can be computed and so on. To combine the data variables
(objects) along with appropriate functions for manipulation of these objects, the
concept of classes were introduced in object oriented programming.
The elementary concept of object oriented programming begins with the
use of classes and objects. A class is a very powerful keyword in C++. A class
declaration defines new user-defined data types that link data and the code that
manipulates the data. In other words, a class combines data elements and
functions (operations) into a single entity.

We have already discussed the concept of combining of data elements into a


group called structures in the previous year. Application of user-defined functions
for performing different operations has also been discussed. Classes combine
both data elements and operations associated with the data.

Let us look at the terminologies in procedural programming language and


object oriented programming.

Procedural Object oriented


programming programming

Variables Objects
User-defined data types Classes
Structure members Instance variables
Functions Methods
Function call Message passing

The data elements in a class are called member data and the functions in
a class are called member functions. This chapter explains classes, objects,
member functions, data hiding, abstraction, encapsulation and how these
features are implemented using classes (further chapters provide details of single
inheritance, multiple inheritances, insight to polymorphism and other functions).
Classes and objects 191

7.2 Definition and declaration of classes and objects:


A class definition is a process of naming a class and data variables, and
interface operations of the class.
A class declaration specifies the representation of objects of the class and
set of operations that can be applied to such objects.
The definition and declaration of a class indicates the following:
 The data variables known as member data of a class, describe the
characteristics of a class.
 Member functions are the set of operations that are performed on the
objects of the class. There may be zero or more member functions in a
class. This is also known as interface.
 The access control specifiers to the class members within a program are
specified.
 Class tagname for external operations for accessing and manipulating
the instance of a class.
The General syntax for defining a class is as follows:
class user_defined_name
{
private:
Member data
Member functions
protected:
Member data
Member functions
public:
Member data
Member functions
};
Keyword class is used to declare a class. User_defined_name is the name
of the class.
Class body is enclosed in a pair of flower brackets. Class body contains
the declaration of its members (data and functions). There are generally three
types of members namely private, public and protected. These are discussed in
the following section.
The class function definition describes how the class functions are
implemented.
192 Classes and objects

Example 7.1: Let us declare a class for representation of bank account.


class account
{
private: //implicit by default
int accno;
char name[20[;
char acctype[4];
int bal _amt;
public: //member functions
void get_data();
void displaydata();
}

In the above example, class name account is a new type identifier that is
used to declare instance of that class type. The class account contains four
member data and two methods or member functions. Both of these member
data are private by default, while both member functions are public by default.
The functions get_data( ) and put_data( ) are used to write instructions to
perform operations on member data. These two functions only can provide access
to get member data from outside the class. Therefore, the data cannot be accessed
by any other function that is not a member of the class account.

Program 7.1 Program to show the use of class and object

#include<iostream.h>
#include<conio.h>
class Test
{
private:
int a,b;
public:
void getnum()
{
cout<<"Enter Numbers for Addition: ";
cin>>a>>b;
}
void show_data()
{
cout<<"Addition of two numbers: "<<(a+b);
}
};
Classes and objects 193

int main()
{
clrscr();
Test a1;
a1.getnum();
a1.show_data();
getch();
return 0;
}

Enter Numbers for Addition: 50 60


Addition of two numbers: 110
In the above program a, b are data members of the class Test. get_num()
and show_add() are member functions. get_num() is used to input data values.
show_add() function is used to add two numbers and display the sum of two
numbers.
7 .3 Access specifiers
An object is defined to be an instance of a class. Every data member of a
class is specified by three levels of access protection for hiding data and function
members internal to the class. The access specifiers define the scope of data.
The access specifers are indicated using the following keywords.
7.3.1 private
private access means a member data can only be accessed by the member
function. Members declared under private are accessible only within the class.
If no access specifier is mentioned, then by default, members are private.
Example 7.2: private:
int x;
float y;
7.3.2 public
public access means that members can be accessed by any function outside
the class also.
Example 7.3: class box
{
int length;
public : int width;
private : int height;
void set_height ( int i )
{
height = i ;
}
194 Classes and objects

int get_height ( )
{
return height ;
}
};
int main()
{
box object;
object.length = 10;
object.width = 20;
object.set _height ( 30); // private variable can be accessed
return 0; only through its public method
}

Thus keyword public identifies both class data and functions that constitute
the public interface for the class. In the above program data member width is a
public variable. set_height() and get_height() are member functions. The private
variable height can be accessed only through the member functions of the class.
Thus keyword public identifies both class data and functions that constitute
the public interface for the class.
7.3.3 Protected
The members which are declared using protected can be accessed only by
the member functions, friends of the class and also by the member functions
derived from this class. The members cannot be accessed from outside. The
protected access specifier is therefore similar to private specifier. The difference
is discussed in detail in further chapters.
7.4. Members of the class
The members of a class can be data or functions. Private and protected
members of a class can be accessed only through the member functions of that
class. No function outside a class can include statements to access data directly.
The public data members of objects of a class can be accessed using direct
member access operator (.)
The syntax for accessing class member is
class-object. member-data
class-object.member-function(arguments);
Example 7.4: class rectangle
{
int l;
int b;
public:
Classes and objects 195

void get_data();
void compute_area();
void display();
};
int main()
{
rectangle rl;
rl.get _data();
rl.compute_data();
r1.display();
return 0;
}
In the above example l and b are data members of class rectangle and
get_data(), compute_data() and display() are member functions. r1 is a class
variable that invokes the member functions.

Program 7.2. Program to show the use of access specifiers with classes and
objects

#include<iostream.h>
#include<conio.h>
class data
{
private:
int day;
int month;
int year;
public:
void date(int dd,int mm,int yy)
{
day = dd;
month = mm;
year = yy;
cout<<"\t"<<day<<"\t"<<month<<"\t"<<year<<endl;
}
};
int main()
{
clrscr();
data date1,date2;
date1.date(7,12,2014);
date2.date(8,12,2014);
date1.date(12,12,2014);
getch();
196 Classes and objects

return 0;
}
In the above program day, month and year are private members and date
is a public function. The output of the above program is as follows.

7.5 Member functions


Member functions can be defined in two places:
 Inside class definition
 Outside class definition
7.5.1 Inside class definition
To define member function inside a class the function declaration within
the class is replaced by actual function definition inside the class. A function
defined in a class is treated as inline function. Only small functions are defined
inside class definition.
Syntax: return_type classname(member function)
Example 7.5: class rectangle
{
int length;
int breadth;
public:
void get_data ()
{
cin>>length;
cin>>breadth;
}
void put_data (void)
{
cout <<length ;
cout <<breadth;
}
};
7.5.2 Outside class definition
A function declared as a member of a class is known as member function.
Member functions declared within a class must be defined separately outside
the class. The definition of member function is similar to normal function. But a
Classes and objects 197

member function has an ‘identity label’ in the header. This label tells the compiler
which class the function belongs to. The scope of the member function is limited
to the class mentioned in the header. Scope resolution operator :: is used to
define the member function.
Syntax: For member function outside a class
return_type classname :: memberfunction(arg1, arg2, …, argn)
{
function body;
}
The member functions have following characteristics:
 Type and number of arguments in member function must be same as
types and number of data declared in class definition.
 The symbol :: is known as scope resolution operator. Usage of :: along
with class name is the header of function definition. The scope resolution
operator identifies the function as a member of particular class.
 Several classes can use same function name. Membership label will resolve
their scope.
 Member function can access private data of a class. A non-member function
cannot.
Example 7.6: The following program segment shows how a member function
is defined outside class.
class operation
{
private :
int a;
int b;
public :
int sum();
int product();
};
int operation::sum()
{
return (a+b) ;
}

int operation (product)


{
return (a*b) ;
}
198 Classes and objects

In the above example sum() and product() are member functions defined
outside class operation. The use of scope resolution operator implies that
these member functions are defined outside the class.
Program 7.3 To use classes using member functions inside and outside
class definition.

# include<iostream.h>
class item // class declaration
{
int number; // private by default
float cost;
public :
void getdata(int a, float b);
void putdata(void) // function defined here
{
cout <<"Number: "<< number << endl;
cout << "Cost: "<< cost << endl;
}
};
// member function outside class definition
void item :: getdata(int a, float b)
{
number = a;
cost = b;
}
// main program
int main()
{
item x; // create object x
x.getdata(250, 10.5);
x.putdata();
return 0;
}
In the above program one member functions putdata() is defined inside
class definition and the other member function getdata() is defined outside
class definition. Here is the output of program.
Number : 250
Cost : 10.5
7.6. Defining objects of a class
When a class is defined, it specifies the type information the objects of the
class store. Once the class is defined an object is created from that class. The
Classes and objects 199

objects of a class are declared in the same manner like any other variable
declaration.
Syntax : class user_defined_name
{
private: //Members
public: // Methods
};
User_defined_name object1, object2, … ;
Example 7.6: class num
{
private:
int x;
int y;
public:
int sum(int p, int q);
int diff(int p, int q);
};
void main()
{
num s1,s2;
s1.sum(200,300);
s2.diff(600,500);
}
Note that an object is an instance of a class template.
Example 7.6 A class to create studentname, rollno, sex, age.
class student
{
int rollno;
char name[20];
char gender;
int age;
public:
void get_data();
void display_data();
};
student obj1, obj2; //Obj1, obj2 are objects of class student
7.7 Arrays as members of classes
So far we have considered primary data values as member of class. Just
like arrays within structures, it is possible to use arrays as member data of class
type.
200 Classes and objects

Example 7.7: class marks


{
int m[5];
int i ;
public:
void setval(void);
void display(void);
};
The array variable m is a private member of class marks. This can be used
by member function setval( ) and display( ) as follows.
void marks :: setval(void)
{
cout<< "Enter marks: "<<endl;
for (i=0;i<5;i++)
cin>>m[i] ;
}
void marks::display(void)
{
cout<< "The marks are : ";
for(i =0;i<5;i++)
cout<<setw(4)<<m[i]<<endl;
}
7.8 Array of objects
An array having class type elements is known as array of objects. An array
of objects is declared after class definition and is defined in the same way as any
other array.
Example 7.8: class employee
{
char name[10] ;
int age;
public:
void getdata();
void dispdata(void);
};
employee supervisor[3] ;
employee sales_executive[5] ;
employee team_leader[10] ;
In the above example, the array supervisor contains 3 objects namely
supervisor[0], supervisor[1],supervisor[2].
Classes and objects 201

Name Age
supervisor[0]
supervisor[1]
supervisor[2]
Storage of data items in an object array

Program 7.4 Program to show the use of array of objects

#include<iostream.h>
#include<conio.h>
class data
{
int rollno, maths, science;
public:
int avg();
void getdata();
void putdata();
};
void data::getdata()
{
cout<<"Enter rollno: ";
cin>>rollno;
cout<<"Enter maths marks: ";
cin>>maths;
cout<<"Enter science marks: ";
cin>>science;
putdata();
}
int data::avg()
{
int a;
a=(maths+science)/2;
return a;
}
void data::putdata()
{
cout<<"Average = "<<avg()<<endl;
}

int main()
{
202 Classes and objects

clrscr();
data stud[3];
for(int i=0;i<3;i++)
stud[i].getdata();
return 0;
}

Enter rollno: 23
Enter maths marks: 56
Enter science marks: 78
Average = 67
Enter rollno: 24
Enter maths marks: 56
Enter science marks: 77
Average = 66
Enter rollno: 12
Enter maths marks: 44
Enter science marks: 89
Average = 66

In the above program stud is an array of objects, data is a class with member
functions getdata() and putdata() defined outside the class definition.
7.9. Objects as function arguments
A function can receive an object as a function argument. This is similar to
any other data type being sent as function argument. An object can be passed to
a function in two ways:
 Copy of entire object is passed to function (pass-by-value).
 Only address of the object is transferred to the function (pass-by-reference).
In pass by value, a copy of the object is passed to the function. The function
creates its own copy of the object and uses it. Therefore changes made to the
object inside the function do not affect the original object.
In pass by reference, when an address of an object is passed to the function,
the function directly works on the original object used in function call. This
means changes made to the object inside the function will reflect in the original
object, because the function is making changes in the original object itself.
Pass-by-reference is more efficient, since it require only to pass the address
of the object and not the entire object.
Classes and objects 203

Program 7.4: Program to show objects as arguments to function.

#include<iostream.h>
#include<conio.h>
class currency
{
int rupee, paise;
int total;
public:
void get_value(int r, int p);
void display(void);
};
void currency::get_value(int r, int p)
{
rupee = r;
paise = p;
total = r*100 + p;
}
void currency::display(void)
{
cout<<rupee<<" Rupees "<<" and "<<paise<<" paise"<<endl;
cout<<"Converted value: "<<total<<" paise";
}
int main()
{
currency c;
c.get_value(5, 75);
c.display();
}

5 Rupees and 75 paise


Converted value: 575 paise

Program 7.5: Program to show objects as arguments to function.

#include <iostream.h>
#include<conio.h>
class rup
{
private:
int n1,n2;
public:
rup():n1(1),n2(1)
204 Classes and objects

{
}
void get()
{
cout<<“enter first number”;
cin>>n1;
cout<<“enter second number”;
cin>>n2;
}
void print()
{
cout<<“product=”<<n1<<endl;
cout<<“product2=”<<n2<<endl;
}
void multi(rup r1,rup r2)
{
n1=r1.n1*r1.n2;
n2=r2.n1*r2.n2;
}
};
int main ()
{
rup r1,r2,r3;
clrscr();
r1.get();
r2.get();
r3.multi(r1,r2);
r3.print();
getch();
return 0;

}
7.10. Differences between structure and class:
In C++, a structure is a class defined with the keyword struct. Its members
and base classes are public by default. A class is defined with the class keyword
Points to remember:
 A class is a user defined data that contains data members and objects
 An object is an instance of a class.
 Members: There are two types of members in a class. They are data member
and member functions.
 Data members are different data variables similar to structure members.
 Data members may be declared in a class as private, public or protected.
Classes and objects 205

 Private members are default by declaration. They are accessible only to


member functions within a class only.
 Public members can be accessed inside as well as outside class definition.
 Member function also known as method, acts on data members in the
class.
 Member functions may be defined inside or outside a class.
 Scope resolution operator :: is used when a global variable exists with the
same name as local variable. This is also used in C++ when member
functions are declared outside the class definition. The function name is
preceded by the class name and scope resolution operator.
 Class members are accessed using dot(.) operator.
 An array having class type elements is known as an array of objects.
 Functions can receive objects as function arguments.
 It is possible to pass objects to a function using pass by value or pass by
reference.

One mark questions:

1. What is a class?
2. What is an object?
3. What are the two types of members referenced in a class?
4. What are data members?
5. What is a member function?
6. Mention the access specifiers used with a class
7. Is it possible to access data outside a class?
8. Which type of data members are accessible outside a class?
9. Which access specifier is implicitly used in a class?
10. Define the term public access
11. Mention the operator used to access members of a class
12. What is the significance of scope resolution operator (::)?
13. How are objects of a class declared? Give an example
14. What is meant by an array of objects?
15. Write an example to show how objects can be used as function
arguments?
Two/Three mark questions
1. Write the differences between class definition and class declaration.
2. Write the syntax and example for class definition.
3. Write the syntax and example for class declaration.
4. What is the significance of using access specifiers? Mention different
access specifiers.
5. Discuss private access specifier with an example.
206 Classes and objects

6. Write short notes on public access specifier.


7. Explain protected access specifer.
8. How are class members referenced? Discuss with suitable example.
9. What is meant by referencing member functions inside class definition
and outside class definition?
10. Discuss how objects of a class are referenced with an example.
11. How can arrays be used as class members. Write an example.
12. How are objects passed as arguments to a function? Give an example.
Five mark questions
1. Explain class definition and class declaration with syntax and example.
2. Describe access specifiers in a class.
3. Explain member functions
a. Inside class definition
b. Outside class definition
4. What are the characteristics of member functions outside a class?
5. Explain how objects of a class can be defined?
6. Illustrate how an array of objects can be defined.
7. Describe how objects can be used as function arguments.
8. Let product list be a linear array of size N where each element of the
array contains following fields : Itemcode, price and quantity. Declare a
class product list with the three data members and member functions to
perform the following :
a. Add values to the product list.
b. Printing the total stock value.
9. A class clock has following members : a. hour b. minute
Create member functions
a. To initialize the data members
b. Display the time
c. To convert hours and minutes to minutes
10. Write a program that receives arrival time, departure time and speed of an
automobile in kilometers/hour as input to a class. Compute the distance
travelled in meters/second and display the result using member
functions.
********
Function overloading 207
Chapter 8

FUNCTION OVERLOADING AND MEMBER FUNCTIONS

Objects:
Ø Need for function overloading
Ø Concept of polymorphism
Ø Application of function overloading through simple examples
Ø Concept of inline functions
Ø Use friend functions
208 Function overloading

8.1 Introduction
In the previous chapter, we have learnt about classes and objects. The
application of classes with data members and member functions is the principle
feature of object oriented programming to develop and simplify programming
methods. Another feature to help for easier programming is polymorphism. The
definition of polymorphism has appeared in chapter 6. Let us recall the definition
once again. Polymorphism refers to “one name having many forms, different
behavior of an instance of an object depending on situations”.
C++ implements polymorphism through function overloading and operator
overloading. The function overloading allows the user to create new abstract
data types. In this chapter we learn about the need for function overloading,
definition and declaration of function overloading and some examples. The
discussion is limited to design of a set of functions that perform essentially the
same thing, but with a different argument list. The selection of overloaded function
depends on matching arguments at the time of compilation. The study in this
chapter is also extended to inline and friend functions.
8.2 Need for function overloading
Function overloading means two or more functions have same name, but
differ in the number of arguments or data type of arguments. Therefore it is said
that (function name) is overloaded. Function overloading therefore is the process
of defining same function name to carry out similar types of activities with various
data items. The advantages of function overloading are:
 When different functions are created for different operations, then user
has to call respective function depending on the situation. Instead, for
different situations if the same function is called with different arguments
using function overloading, then the compiler automatically decides about
the appropriate function by comparing the argument types used in the
call to the function and calls the required function. Thus the code is
executed faster.
 It is easier to understand the flow of information and debug.
 Code maintenance is easy.
 Easier interface between programs and real world objects.
8.3 Definition and declaration of overloaded functions
The main factor in function overloading is a function’s argument list. C++
can distinguish overloaded functions by the number and type of arguments. If
there are two functions having same name and different types of arguments or
different number of arguments, then function overloading is invoked automatically
by the compiler. Function Overloading is also known as Compile time
polymorphism.
Function overloading 209
Example 8.1: int sum(int a, int b);
float sum(float p, float q);
The function sum() that takes two integer arguments is different from the
function sum() that takes two float arguments. This is function overloading.
To overload a function, each overloaded function must be declared and
defined separately.
Example 8.2: int product(int p, int q, int r);
float product(float x,float y, float z);
int product(int p, int q, int r)
{
cout<<“product=”<<p*q*r<<endl;
}
float product(float x,float y,float z)
{
cout<<product=”<<x*x*y*y*z*z<<endl;
}
In this example the function product() is overloaded two times. First time
with three integer values and integer as return value, second time with three
float values and return type being a float. The compiler automatically chooses
the right type of function depending on the number of arguments.
8.4 Restrictions on overloaded functions
Ø Each function in a set of overloaded functions must have different argument
list.
Ø If typedef is used for naming functions, then the function is not considered
as different type.
8.4.1 Calling overloaded functions
The following programming example shows how overloaded functions can
be called.
Program 8.1: To compute volume of cone, cube and cylinder using overloaded
functions.
#include<iostream.h>
#include<conio.h>
class funoverload
{
public:
int volume(int a) // Volume of Cube
{
return a*a*a;
}
210 Function overloading

double volume(double r, double h) // Volume of Cone


{
return (0.33*3.14*r*r*h);
}
double volume(double r, int h) // Volume of Cylinder
{
return (3.14*r*r*h);
}
double volume(double l, double b, double h) //Volume of Cuboid
{
return (l*b*h);
}
};
int main()
{
clrscr();
funoverload f1;
cout<<“Volume of the Cube: “<<f1.volume(10)<<endl;
cout<<“Volume of the Cone: “<<f1.volume(2.0,3.0)<<endl;
cout<<“Volume of the Cylinder: “<<f1.volume(2.0,3)endl;
cout<<“Volume of the Cuboid: “<<f1.volume(5.0,6.0,7.0)<<endl;
return 0;
getch();
}

Volume of the Cube: 1000


Volume of the Cone: 12.4344
Volume of the Cylinder: 37.68
Volume of the Cuboid: 210

The above program finds volume of cube, cuboid, cone or cylinder


depending on the number and type of arguments provided as input to the function
called volume. Therefore though all functions have the same name volume,
appropriate function is selected based on data type of argument list. The compiler
selects the required function through function overloading.
8.5 Other functions in a class
The member functions of a class may be defined inside or outside a class.
One such function defined inside a class is inline function.
Function overloading 211
8.5.1 inline functions
A function call generally involves the complex process of invoking a function,
passing parameters to the function, allocating storage for local variables, thereby
using extra time and memory space. It is possible to avoid these overheads of a
function by using inline function. The inline function is a short function. Compiler
replaces a function call with the body of the function.
The keyword inline is used to define inline functions. The inline function consists
of function call along with function code and the process is known as expansion.
 Inline functions definition starts with keyword inline.
 The inline functions should be defined before all functions that call it.
 The compiler replaces the function call statement with the function code
itself (expansion) and then compiles the entire code.
 They run little faster than normal functions as function calling overheads
are saved.
Advantages of inline functions
 The inline member functions are compact function calls.
 Therefore the size of the object code is considerably reduced.
 The speed of execution of a program increases.
 Very efficient code can be generated.
 The readability of the program increases.
Disadvantage
As the body of inline function is substituted in place of a function call,
the size of the executable file increases and more memory is needed.
Program 8.2: Finding the square of a number using inline functions.
#include <iostream.h>
inline int square (int a)
{
return(a*a);
}
int main( )
{
int x, y;
x=square(5);
cout<<"Square of 5 = "<<x<<endl;
y=square(10);
cout<<"Square of 10 = "<<y<<endl;
return 0;
}
212 Function overloading

Square of 5 = 25
Square of 10 = 100
In the above example square() is an inline function that finds the square
of a number.
Note: The inline function may not work some times for one of the following
reasons:
 The inline function definition is too long or too complicated.
 The inline function is recursive.
 The inline function has looping constructs
The inline function has a switch or goto.
8.5.2 friend functions
We have seen that private and protected members of a class cannot be
accessed from outside the class in which they are declared. In other words non-
member function does not have access to the private data members of a class.
But there could be a situation where two classes must share a common function.
C++ allows the common function to be shared between the two classes by making
the common function as a friend to both the classes, thereby allowing the function
to have access to the private data of both of these classes.
A friend function is a non-member function that is a friend of a class. The
friend function is declared within a class with the prefix friend. But it should be
defined outside the class like a normal function without the prefix friend. It can
access public data members like non-member functions.
Syntax: class class_name
{
public:
friend void function1(void);
friend returntype_specifer function_name(arguments);
};
Example 8.3: class base
{
int val1, val2;
public:
void getdata()
{
cout<<“Enter two values:”;
cin>>val1>>val2;
}
};
Function overloading 213
friend float mean(base ob);
float mean(base ob)
{
return float(ob.val1+ob.val2)/2;
}
In the above example mean() is declared as friend function that computes
mean value of two numbers that are input using getdata() function.
 A friend function although not a member function, has full access right to
the private and protected members of the class.
 A friend function cannot be called using the object of that class. It can be
invoked like any normal function.
 They are normal external functions that are given special access privileges.
 It cannot access the member variables directly and has to use an object
name.membername (Here, is a membership operator).
 The function is declared with keyword friend. But while defining friend
function it does not use either keyword friend or :: operator.

Program 8.3 To indicate the use of friend function.

#include <iostream.h>
class myclass
{
private:
int a,b;
public:
void set_val(int i, int j);
friend int add(myclass obj);
};
void myclass::set_val(int i,int j)
{
a = i;
b = j;
}
int add(myclass obj)
{
return (obj.a+obj.b);
}
214 Function overloading

int main()
{
myclass object;
object.set_val(34, 56);
cout << “Sum of 34 and 56 is “<<add(object)<<endl;
return 0;
}

Sum of 34 and 56 is 90

In the above program segment the function add() is a friend of class


myclass. Since it is not a member of any class, it cannot be called like an
object. Since it is a non-member function, add() should be declared inside a
class under public or private or protected access specifier.
Points to remember
 A function name that has several definitions with respect to the number
of arguments and type of arguments is known as function overloading.
 Function overloading implements polymorphism
 Inline functions are member functions defined inside a class.
 The function code is written along with function definition inside a class
 Friend function is a non member function of a class that has access to
both private and protected access members
Function overloading 215
Review questions

One mark questions


1. What is meant by function overloading?
2. When is function overloading needed?
3. Write an advantage of function overloading.
4. Name one condition for overloading of functions.
5. What is an inline function?
6. Write one advantage of inline function.
7. The inline function is always converted to a code block. True/false
8. What is a friend function?
9. Write an example for friend function declaration.
10. Write one condition for using a friend function.
11. Can the keyword friend be used while declaring a friend function?
12. Overloading a function means using different names to perform the
same operations. True/false
Two marks questions:
1. What is meant by overloading implements polymorphism?
2. Write example for definition and declaration of function overloading
3. What are the restrictions on overloaded functions?
4. What are inline functions? Give an example.
5. Write the advantages of inline functions.
6. Create an inline function to check if a number is prime or not.
7. When are friend functions used? Write syntax for declaration of friend
function.
8. Write any two characteristics of friend function.
Three mark questions:
1. Write any three reasons for function overloading.
2. What are the advantages of inline functions?
3. Write the advantages and disadvantages of inline functions.
4. When is it possible that an inline function may not work?
5. Write any three characteristics of friend function.
Five mark questions:
1. Explain the need for function overloading.
2. Discuss overloaded functions with syntax and example.
3. Explain inline functions with syntax and example.
4. Explain friend functions and their characteristics.
*******
216 Constructors and Destructors

Chapter 9
CONSTRUCTORS AND DESTRUCTORS

Objectives:
 Need for use of constructors

 To identify different types of constructors

 To highlight implicit and explicit declaration of constructors

 To understand the need for constructor overloading

 To understand the need for destructors


Constructors and Destructors
217
9.1 Introduction:
In the previous chapters, we have learnt to use classes, member data and
member functions of a class. The use of polymorphism through function
overloading has simplified object oriented programming further. In real life
applications, sometimes the objects may have to be initialized before using them.
The initialization is normally done by a member function which initializes data
members to pre-defined values. In this chapter we discuss about special member
functions that are used to initialize and destroy the objects of a class automatically
so that memory utilization can be optimized.
It is sometimes convenient if an object can initialize itself when it is first
created, without the need to make a separate call to a member function. This is
possible with special member functions. Automatic initialization is carried out
using such a special member function called constructor. Generally, we can
say, a constructor constructs the data members of an object. i.e., initialized a es
a value to the data members.
“A constructor is a special member function that is used in classes to
initialize the objects of a class automatically”.
A constructor is a member function of a class with the same name as that
of the class. A constructor is defined like other member functions of a class. It
can be defined either inside the class definition or outside the class definition.
It is called constructor because it constructs the values of data members of
the class. The following rules are used for writing a constructor function:
1. A Constructor always has name that is same as the class name of which
they are the members. This will help the compiler to identify that they are
the constructors.
2. There is no return type for constructors (not even void). Since, the
constructor is called automatically by the system, there is no program for
it to return anything to; a return value would not make sense.
3. A constructor should be declared in public section.
4. A constructor is invoked automatically when objects are created.
Constructors can have default arguments.
5. It is not possible to refer to the address of the constructors.
6. The constructors make implicit calls to the operators new and delete when
memory allocation is required.
218 Constructors and Destructors

9.2 Declaration of constructor inside the class definition


Example 9.1: class test
{
int m, x, y;
public:

test() //constructor inside the class definition


{
x = 0; y = 0;
}
};

Program 9.1: Use of constructor.


#include <iostream.h>
#include <iomanip.h>
class counter
{
private:
int a;
public:
counter()
{
a = 0;
}
void inc_count()
{
a++;
}
int get_count()
{
return a;
}
};
int main()
{
counter c1, c2;
cout<<“c1 = “<<c1.get_count();
cout<<“c2 = “<<c2.get_count();
c1.inc_count();
c2.inc_count();
cout<<“c1 = “<<c1.get_count()<<endl;
cout<<“c2 = “<<c2.get_count()<<endl;
}
Constructors and Destructors
219
The above code segment contains a constructor with the name counter(),
which is same as the class name. This constructor initializes the class variable a
to 0. When, in main(), an object for the counter class is defined will invoke or
call the constructor counter() and initialize the variable a to 0. i.e., in main() we
are defining two instances for the class counter(), c1 and c2. Now, the constructor
counter will be called twice automatically, once for object c1 and once for object
c2.
Note that when a constructor is declared for a class, initialization of class
objects is done automatically.
9.3 Types of constructors:
There are three types of constructors, namely:
1. Default constructor
2. Parameterized constructor
3. Copy constructor
9.3.1 Default Constructor
A constructor which does not accept any arguments is called a zero
argument constructor. It is also known as default constructor. The default
constructors are useful when the objects are need to be created without having
to type initial values. Default constructor simply allocated memory to data
members of objects. Features of default constructor are
 For every object created, this constructor is automatically called.
 All objects of a class are initialized to same set of values by the default
constructor.
 If different objects are to be initialized with different values, it cannot be
done using default constructor.
Note:
 When a user-defined class does not contain an explicit constructor,
compiler automatically invokes default constructor.
 Declaring a constructor with arguments, hides default constructor.

Syntax: classname::classname //constructor without arguments


{ }

Example 9.2: student() { }


220 Constructors and Destructors

Program 9.2: To show initialization of an object using default constructor.


#include<iostream.h>
#include<iomanip.h>
class x
{
private:
int a, b;
public:
x() { a=10, b=20; }
void display()
{
cout<<a<<setw(5)<<b<<endl;
}
};
void main()
{
x obj1,obj2;
obj1.display();
obj2.display();
}
10 20
10 20

Program 9.3: To show the default initialization of constructor member function


using scope resolution operator.
#include<iostream.h>
#include<string.h>
#include <ctype.h>
class student
{
private:
int regno;
char name[20];
public:
student(); //constructor
void display();
};
student : :student()
{
regno = 0;
strcpy(name, "Book");
}
Constructors and Destructors
221
void student::display()
{
cout<<"Reg. No: "<<regno<<endl;
cout<<"Name: "<<name<<endl;
}
void main()
{
student s1;
cout<<"Use of default constructor:"<<endl;
s1.display();
}
Use of default constructor:
Reg. No: 0
Name: Book

The above program shows the use of default constructor student that
initializes the members automatically as soon as they are created.
Disadvantages of default constructor:
 When many objects of the same class are created, all objects are initialized
to same set of values by default constructor
 It is not possible to initialize different objects with different initial values
using default constructor.
9.3.2 Parameterized Constructors:
A constructor that takes one or more arguments is called parameterized
constructor. Using this constructor, it is possible to initialize different objects
with different values.
Parameterized constructors are also invoked automatically, whenever
objects with arguments are created. The parameters are used to initialize the
object.
Features of parameterized constructors:
 The parameterized constructors can be overloaded.
 For an object created with one argument, constructor with only one
argument is invoked and executed.
 The parameterized constructor can have default arguments and default
values.
Invoking constructors
A constructor is automatically invoked by C++ compiler with an object
declaration. The constructors can be invoked through the following methods:
222 Constructors and Destructors

1. Explicit call
2. Implicit call
3. Initialization at the time of declaration with = operator
Explicit call
In explicit call, declaration of an object is followed by assignment operator,
constructor name and argument list enclosed in parenthesis.
Program 9.4: To use parameterized constructor through explicit call
#include<iostream.h>
class num
{
private:
int a,b;
public:
num(int p, int q) {a = p,b = q;}
void display()
{
cout<<"a = "<<a<<" and b = "<<b<<endl;
}
};
void main()
{
num obj1=num(10,20);
num obj2=num(40,50);
cout<<"First construction: ";obj1.display();
cout<<"Second construction: ";obj2.display();
}

First construction: a = 10 and b = 20


Second construction: a = 40 and b = 50

In the above program code the objects obj1 and obj2 are called explicitly
by using parameterized constructor.
Implicit call
An Implicit call means the declaration of the object is followed by argument
list enclosed in parentheses.
Program 9.5: Program to intialise the data members using implicit declaration
#include<iostream.h>
class num
{
private:
Constructors and Destructors
223
int a,b;
public:
num(int m, int n)
{ a = m, b = n; }
void display()
{
cout<<"a= "<<a<<" b= "<<b<<endl;
}
};
void main()
{
num obj1(10,20);
num obj2(40,50);
obj1.display();
obj2.display();
}
a=10 b= 20
a=40 b=50
Note that:
1. One parameterized constructor num(int m, int n) is defined inside the
class.
2. For each object, different values are passed from function main(). Therefore
different objects can be initialized to different values.
Initialization of objects during declaration with assignment operator
This method is used for the constructor with exactly one argument. In this
method declaration is followed by assignment operator and value to be initialized.
Program 9.6: To initialize objects using assignment operator
#include<iostream.h>
class num
{
private:
int a;
public:
num(int m) { a = m; }
void display()
{
cout<<a<<endl;
}
};
void main()
{
224 Constructors and Destructors

num obj1=100;
num obj2=200;
cout<<"Object1 = ";obj1.display();
cout<<"Object2 = ";obj2.display();
}

Object1 = 100
Object2 = 200

In the above example, the constructors obj1 and obj2 are initialized using
= operator.
Note that
1. This method is applicable only to the constructors that have exactly one
parameter.
2. If a class contains at least one parameterized constructor, then necessary
arguments have to be passed to the constructor in the declaration itself.
3. If user does not want to pass arguments, then default constructor must be
created in the class.
Example 9.1: num obj1(10,20);
num obj2; //error
num() { } //This default constructor must be included in
//class num to avoid errors
The above example shows that it is necessary to use a default constructor
num() to avoid error during the declaration of num obj2;
9.3.3 Copy constructor
Copy constructor is a parameterized constructor using which one object
can be copied to another object. Copy constructors are used in the following
situations.
 To initialize an object with the values of already existing objects.
 When objects must be returned as function values
 To state objects as by value parameters of a function.
Copy constructor can accept a single argument of reference to same class
type. The argument must be passed as a constant reference type.
Syntax: classname :: classname(classname &ptr)
Example 9.2: x::x(x &ptr)
Constructors and Destructors
225
Here, x is a class name and ptr is a pointer to a class object x.
If there is more than one argument present in the copy constructor, it
must contain default arguments.
Note that:
1. Copy constructor is not invoked explicitly.
2. Copy constructor is invoked automatically when a new object is created
and equated to an already existing object in the declaration statement
itself.
Example 9.3: x a1; //default constructor
x a2 = a1; //copy constructor
a1.display();
The above example shows the use of copy constructor to create a new
object a2 using existing object a1.
3. When a new object is declared and existing object is passed as a parameter
to it in the declaration, then also copy constructor is invoked.
Example 9.4: x a1(100,200);//parameterized constructor
x a2(a1); //copy constructor is invoked for
//object a2 with a1 as parameter
4. When an object is passed to a function using pass-by-value, copy constructor
is automatically called.
Example 9.5: void test( x )
{
—————
—————
}
main()
{
x a;
test(a); //copy constructor is invoked
}
5. Copy constructor is invoked when an object returns a value.
Example 9.6:
class measure
{
int feet;
float inches;
measure sum(measure&);
};
226 Constructors and Destructors

measure measure::sum(measure &m)


{
feet=feet+m.feet;
inches=inches+m.inches;
if(inches>=12)
{
feet++;
inches=inches-12;
}
return measure(feet,inches);
}

Program 9.7: To find factorial of a number using copy constructor

#include<iostream.h>
class copy
{
int var;
public:
copy(int temp)
{
var = temp;
}
int calculate()
{
int fact, i;
fact = 1;
for(i = 1; i <= var; i++)
fact = fact * i;
return fact;
}
};
void main()
{
int n;
cout<<"Enter the number: ";
cin>>n;
copy obj(n);
copy cpy = obj;
cout<<"Before copying: "<<n<<"! = "<<obj.calculate()<<endl;
cout<<"After copying: "<<n<<"! = "<<cpy.calculate()<<endl;
}
Constructors and Destructors
227
Enter the number: 5
Before copying: 5! = 120
After copying: 5! = 120

9.4 Constructor Overloading


Constructors are used to initialize the data members of a class. We can
use constructors also to specify the input values for the data members. But the
default constructor cannot be used for this purpose. So, we have to overload the
default constructor. Overloading a constructor means specifying additional
operation by passing the required arguments. Depending on the requirement
one can define any number of overloaded constructors in a single class.
Depending on the type and number of arguments passed, the compiler decides
which version of the constructor to invoke during object creation. The following
example has overloaded constructor that is used to specify the input values for
the data members of the class.
Program 9.8: To find the simple interest

#include<iostream.h>
class simpleinterest
{
private:
float p, r, t, si;
public:
simpleinterest() //default constructor
{ }
simpleinterest(float x, float y, float z)//parameterized
{ //constructor
p = x;
r = y;
t = z;
}
void computesi()
{
cout<<"Simple interest is :"<< (p * r * t)/100.0;
}
};
void main()
{
simpleinterest si1, si2(10000.0, 12.0, 2.0);
si2.computesi();
}

Simple interest is 2400.0


228 Constructors and Destructors

In the above code segment, there are two constructors, one is


simpleinterest() and another one is simpleinterest(float x, float y, float z). The
first one is the default constructor and the second one is the three-argument
constructor. When we overload a constructor in a class, then it is the job of the
programmer to define too the default constructor.
9.5 Destructors:
As we have seen, a constructor, the special member function, will be
automatically called when an object is defined for a class. In the same way, a
destructor will be called automatically when an object is destroyed. Destroying
an object means, de-allocating all the resources such as memory that was allocated
for the object by the constructor. A destructor is a special member function that
will be executed automatically when an object is destroyed. It will have, like
constructor, the name same as that of the class but preceded by a tilde (~).
Syntax: class classname
{
private:
//data variables
//method
public:
classname(); //constructor
~classname(); //destructor
}
Example 9.7: When we want to define a destructor for a class, then we can do
this as shown below.
class counter
{
private:
int counter;
public:
counter( ) //Constructor
{
counter = 0;
}
~counter() //Destructor
{ }
};
In the above example the object counter is automatically destroyed
destructors
 The destructor name is same as that of class. The first character must be
tilde (~).
Constructors and Destructors
229
 Destructors do not have a return value. Destructor can never return a
value.
 They take no arguments. Therefore destructors cannot be overloaded.
 The most common use of destructors is to de-allocate memory that was
allocated for the object by the constructor. Also, they are declared public.
Examplem 9.8: class account
{
private:
float balance;
float rate;
public:
account(); //constructor
~account(); //destructor
};

Program 9.9: To show the use of destructor


#include<iostream.h>
#include<conio.h>
#include <iomanip.h>
class num
{
private:
int x;
public:
num();
void display();
~num();
};
num::num()
{
cout<<“ In constructor: \n”;
x=100;
}
num::~num()
{
cout<<“in destructor”<<endl;
}
void num::display()
{
cout<<“Value of x = ”<<x<<endl;
}
int main()
{
clrscr();
230 Constructors and Destructors

num a;
a.display();
getch();
return 0;
}

In constructor:
Value of x = 100
In destructor

In the above program after executing the program, before the control is
transferred out of the function main(), the destructor ~string() is invoked for
each object a, so that memory allotted for data member is de allotted. The object
y and p are deleted using the destructor.
Points to remember:
 A Constructor is a special member function that is executed automatically
whenever an object of a class is created.
 Constructors should have either public or protected access.
 The three types of constructors are default constructor, parameterized
constructor and copy constructor.
 A constructor that does not take any arguments is a default constructor.
 A constructor that takes one or more arguments is called parameterized
constructor.
 Parameterized constructor can be invoked by explicit or implicit call.
 Copy constructor is a parameterized constructor using which one object
can be copied to another object.
 Constructor overloading means passing arguments to the constructor.
 Destructors are member functions that destroy an object automatically.
One mark questions:
1. What is a constructor?
2. Write one reason which defines the need to use a constructor.
Simplify
3. What should be the access parameters for constructor declaration?
4. Can a constructor return a value to a calling function?
5. How many types of constructors are there?
6. What is a default constructor?
7. What is the drawback of default constructor?
8. Is it possible to overload a default constructor?
Constructors and Destructors
231
9. What is a parameterized constructor?
10. Write any one feature of parameterized constructor.
11. Name two methods through which constructors can be invoked.
12. What is an explicit call?
13. What is an implicit call with reference to constructors?
14. When is =used with constructors?
15. What is a copy constructor?
16. Write the syntax for declaration of copy constructor.
17. Can a copy constructor be invoked explicitly?
18. What is meant by constructor overloading?
19. What is a destructor?
20. Which operator is used with destructor?
Two marks questions:
1. What is a constructor? Give an example
2. Why are constructors needed in a program? Justify
3. Write the syntax and example for default constructor.
4. Mention the features of parameterized constructors.
5. Which are the different methods through which constructors are invoked?
6. Write an example to show the use of parameterized constructor through
explicit call.
7. When is copy constructor used in a program?
8. Write syntax and example for copy constructor.
Three marks questions:
1. Mention three types of constructors
2. What are the features of default constructors?
3. What are the disadvantages of default constructor?
4. Write short notes for constructor overloading.
Five marks questions:
1. Write the rules for writing a constructor function.
2. Explain default constructor with syntax and example.
3. Explain parameterized constructor with syntax and example.
4. Explain with an example to show how a constructors is invoked explicitly.
5. Explain the features of copy constructor.
6. Explain destructors with syntax and example.
*****
232 Inheritance

CHAPTER - 10
INHERITANCE

OBJECTIVES

 To understand the concepts of inheritance.


 Usage of inheritance.
 The role of visibility mode.
 Levels of inheritance.
 Concepts of virtual base class.
 Concepts of abstract class.
 Constructors and destructors in derived class.
Inheritance 233

10.1 Introduction:
Inheritance is another important aspect of object oriented programming.
C++ supports this concept. C++ classes can be used in several ways. This is basically
done by creating new classes, reusing the properties of existing one.
C++ allows the user to create a new class (derived class) from an existing
class (base class). The derived class inherits all features from a base class and it
can have additional features of its own.

Inheritance is the capability of one class to inherit properties from


another class.

 Base Class:


It is the class whose properties are inherited by another class. It is also called Super Class.
 Derived Class:
It is the class that inherits properties from base class (es).It is also called Sub Class.
Inheritance has following advantages:
1. Reusing existing code
2. Faster development time
3. Easy to maintain
4. Easy to extend
5. Memory utilization
The following example illustrates the needs of inheritance.
Suppose X is a class already defined and we need to redefine another
class Y having same properties of X and in addition to its own. Suppose if we use
direct option without using inheritance, it has following problems.
1. Code written in X is repeated again in Y which leads to unnecessary wastage
of memory space.
2. Testing to be done separately for both class X and class Y leads to wastage
of time.
The above problems can be solved by using the concept of inheritance.
If we reuse the code of X even in Y without rewriting it. The class Y inherits
all the properties of X. The class X is called Base class and the class Y is called
derived class.
10.3.1 Defining derived classes
When you declare a class, you can indicate what class it derives from by
writing a colon after the class name, the type of derivation (public or private or
protected) and the class from which it derives.
234 Inheritance

class derived_class_name : visibility_mode base_class_name


{
// …….
//members of the derived class
};
Where, class  keyword
derived_class_name  Name of the derived class
:  shows the derivation from the base class
visibility mode  Specifies the type of derivation
base_class  Name of the base class
The body of the derived class contains its own data members and member
function. Like base class definition, the derived class definition must be terminated
by a semicolon.
If no visibility mode is specified, then by default the visibility mode is
considered private.
Note that all members of the class except private are inherited. Following
are some examples of derived class definition.
10.3.2 public derived class
class father // Base class
{
private:
char name[50];
int age;
public:
char caste[50];
int boys;
int girls;
void readdata();
void printdata();
};
class son: public father //public derived class
{
private:
char company[50];
float salary;
public:
void getdata();
void display();
};
Similarly we can write private derived class.
Inheritance 235

10.3.3 private derived class


class son: private father // private derived class
{
private:
char company[50];
float salary;
public :
void getdata();
void display();
};
Similarly we can write protected derived class as:
10.3.4 protected derived class
class son: protected father // protected derived class
{
private:
char company[50];
float salary;
public :
void getdata();
void display();
};
10.4 Visibility mode
The visibility mode (private, public and protected) in the definition of the
derived class specifies whether features of the base class are privately derived or
publicly derived or protectedly derived. The visibility mode basically controls the
access specifier to be for inheritable member of base class in the derived class.
The role of visibility modes:

10.4.1 Public Inheritance


This is the most used inheritance mode. In this
 The public members of a base class become public members of the derived
class.
236 Inheritance

 The private members of a base class can not be inherited to the derived
class.
 The protected members of a base class stay protected in a derived class.
class subclass: public superclass
class student //base class
{
private:
int rollno;
char name[50];
float per;
public:
void input();
void output();
};
class comp: public student //publicly derived class
{
private:
int marks;
public:
void read();
void write();
};
10.4.2. Private Inheritance
 The public members of a base class become the private members of the
derived class.
 The private members of a base class cannot be inherited to the derived
class.
 The protected members of a base class stay protected in a derived class.
10.4.3. Protected Inheritance
 The public members of a base class become protected in a derived class.
 The private members of a base class cannot be inherited to the derived
class.
 The protected members of a base class stay protected in a derived class.
class subclass : protected superclass
10.5 Levels of Inheritance
A derived class extends its features by inheriting some or all the properties
from its base class and adding new features of its own. While inheriting, the derived
class can share properties from:
Inheritance 237
 Only one class
 More than one class
 More than one level
Based on this relationship, inheritance can be classified into five forms.
1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance.
5. Hybrid inheritance
10.5.1 Single Inheritance
If a class is derived from a single base class, it is called as single inheritance.

Base Father

Derived class Son

10.5.2 Multilevel Inheritance


The classes can also be derived from the classes that are already derived.
This type of inheritance is called multilevel inheritance.

Base Grand Father

Derived class-1 Father

Derived class-2 Son

Derived class-n
238 Inheritance

10.5.3 Multiple Inheritance


If a class is derived from more than one base class, it is known as multiple
inheritance.

Base-1 Base - 2 Base - n King Queen

Prince
Derived class

10.5.4 Hierarchical Inheritance


If a number of classes are derived from a single base class, it is called as
hierarchical inheritance.

Base

Derived class Derived class Derived class

Example for Hierarchical Inheritance

Staff

Lecturers Office Staff Group-D

10.5.5 Hybrid Inheritance


Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance.
Inheritance 239

Base
Hierarchical inheritance

Derived class-1 Derived class-2

Derived class Multilevel inheritance

Program to illustrate single level inheritance


#include<iostream.h>
#include<conio.h>
class base
{
private:
int rollno;
char name[20];
public:
void read()
{
cout<<“Enter Roll No and name “<<endl;
cin>> rollno >>name;
}

void display()
{
cout<<“roll no: “<< rollno <<endl;
cout<<“name : “<<name <<endl;
}

};
class derive: public base
{
private:
int m1;
int m2;
int t;
public:
void read1()
{
cout<<“enter first marks and second marks: ”<<endl;
240 Inheritance

cin>>m1>>m2;
t = m1+m2;
}
void display1()
{
cout<<"First marks = "<<m1<<endl;
cout<<"Second marks = "<<m2 <<endl;
cout<<"Total marks = "<<t<<endl;
}

};
void main()
{
derive ob;
clrscr();
ob.read();
ob.read1();
ob.display();
ob.display1();
getch();
}

Enter Roll No and name


1234 Ravindra
Enter first marks and second marks: 100 100
Roll no : 1234
Name : Ravindra
First marks = 100
Second marks = 100
Total marks = 200

10.6 Relationship between classes


10.6.1. Virtual base classes:
Consider a situation where the program design would require one base
class (call it A) and two derived classes namely B and C, which are inherited from
the base class A. Further, derived class D is created from B and C. See the
figure.
In the public inheritance, B and C inherit one copy of the base class data,
where as derived class D inherited from B and C get two copies of the base class
data.
Now suppose a member function of D now wants to access the data members
of the base class an ambiguity problem of which of the two copies is to access will
Inheritance 241
be encountered. The compiler will generate an error message for this ambiguity.
To overcome this problem, the derived class B and C should be declared as
virtual.

Base class - A

derived class – C: get one


derived class –B : get one copy of base class data
copy of base class data
derived class – D
get 2 copies of
base class data

When two or more objects are derived from a common base class,
we can prevent multiple copies of the base class being present in an object
derived from those objects by declaring the base class as virtual when it is
being inherited. Such a base class is known as virtual base class. This
can be achieved by preceding the base class name with the word virtual.

Example: class A
{
————
————
};
class B: virtual public A
{
————
————
};
class C: virtual public A
{
————
————
};
class D: public B, public C
{
————
————
};
242 Inheritance

10.6.2 Abstract classes


An abstract class is one that is not used to create objects. An abstract
class is designed only to act as a base class (to be inherited by other classes). It
is a design concept in program development and provides a base upon which
other classes may be built. In the previous example, the class A is an abstract
class since it was not used to create any objects.
10.6.3 Constructors in derived classes
As we know, the constructors play an important role in initializing objects.
We did not use them earlier in the derived classes for the sake of simplicity. One
important thing to note here is that, as long as no base class constructor takes
any arguments, the derived class need not have a constructor function. However,
if any base class contains a constructor with one or more arguments, then it is
mandatory for the derived class to have a constructor and pass the arguments to
the base class constructors. Remember, while applying inheritance we usually
create objects using the derived class. Thus, it makes sense for the derived class
to pass arguments to the base class constructor. When both the derived and
base classes contain constructors, the base constructor is executed first and
then the constructor in the derived class is executed.
In case of multiple inheritances, the base classes are constructed in the
order in which they appear in the declaration of the derived class. Similarly, in a
multilevel inheritance, the constructors will be executed in the order of
inheritance.
Since the derived class takes the responsibility of supplying initial values
to its base classes, we supply the initial values that are required by all the
classes together, when a derived class object is declared. How are they passed
to the base class constructors so that they can do their job? C++ supports a
special argument passing mechanism for such situations.
The constructor of the derived class receives the entire list of values as its
arguments and passes them on to the base constructors in the order in which
they are declared in the derived class. The base constructors are called and
executed before executing the statements in the body of the derived constructor.
Example: Use of constructor in single inheritance
class base_class
{
protected:
public:
base_class() // base class constructor
{
}
};
Inheritance 243
class derived_class: public base_class
{
protected:
public :
derived _class() // derived class constructor
{
}
};
10.6.4 Destructors in derived classes
If the constructors are called down the line from the base to the derived
class, the destructors are called just in the reverse order. That is from the derived
class up to the base class.
Points to remember:
 Inheritance: It is the capability of one class to inherit properties from
another class.
 Base Class: It is the class whose properties are inherited by another class.
It is also called Super Class.
 Derived Class: It is the class that inherits properties from base class(es). It
is also called Sub Class.
 Advantages of inheritance
 Reusing existing code.
 Faster development time.
 Easy to maintain.
 Easy to extend.
 Memory utilization
 All members of the class except private are inherited.
 The visibility mode (private, public protected) in the definition of the derived
class specifies where features of the base class are privately derived or
publicly derived or protected derived. The visibility mode basically controls
the access specifier to be for inheritable member of base class in the
derived class.
 Inheritance can be classified into five forms.
 Single inheritance
 Multilevel inheritance
 Multiple inheritance
 Hierarchical inheritance
 Hybrid inheritance
244 Inheritance

 Single Inheritance: If a class is derived from a single base class, it is called


as Single Inheritance.
 Multilevel Inheritance: The classes can also be derived from the classes
that are already derived. This type of inheritance is called multilevel
inheritance.
 Multiple Inheritance: If a class is derived from more than one base class,
it is known as multiple inheritance.
 Hierarchical Inheritance: If a number of classes are derived from a single
base class, it is called as hierarchical inheritance.
 Hybrid inheritance: Hybrid Inheritance is combination of Hierarchical and
Multilevel Inheritance.
 In the publicly derived class, the public and protected members remain
public and protected.
 In the privately derived class, the public and protected members of the
base class become private members.
 In the protected derived class, the public and protected members of the
base class become protected members.
 Virtual base class: When two or more objects are derived from a common
base class, we can prevent multiple copies of the base class being present
in an object derived from those objects by declaring the base class as
virtual when it is being inherited. Such a base class is known as virtual
base class. This can be achieved by preceding the base class’ name with
the word virtual.
 An abstract class is one that is not used to create objects. An abstract
class is designed only to act as a base class (to be inherited by other
classes).
Inheritance 245

Review questions

One marks questions:

1. What is inheritance?
2. How to implement inheritance?
3. What is base class?
4. What is derived class?
5. What is public access specifier?
6. What is private access specifier
7. Mention any one advantage of inheritance?
8. Is inheritance possible in c?
9. What is the use of protected access specifier?
10. What is the use of public access specifier?
11. What is the use of private access specifier?
12. What is singe inheritance?
13. What is multilevel inheritance?
14. What is hierarchical inheritance?
15. What is hybrid inheritance?
16. What is multiple inheritance?
17. What is virtual base class?
18. What is an abstract class?
19. When is it necessary to use inheritance?
20. What is visibility mode?
Two marks questions:

1. How to implement inheritance?


2. What is the difference between public and private access specifier?
3. Mention any 2 advantages of inheritance?
4. Mention any 2 types of inheritance?
5. What is the difference between inheritance and polymorphism?
6. What is singe inheritance? Give an example.
7. What is multilevel inheritance? Give an example.
8. What is hierarchical inheritance? Give an example.
9. What is hybrid inheritance? Give an example.
10. What is multiple inheritance? Give an example.
11. What is virtual base class? Give example.
12. What is an abstract class?
13. Which are the components which cannot be inherited?
14. Explain Single Inheritance with a suitable C++ program.
15. Explain the requirements of a virtual base class.
16. When is it necessary to use inheritance?
17. What is visibility mode? What is its role?
246 Inheritance

18. How does inheritance influence the working of constructors?


19. How does inheritance influence the working of destructors?

Three marks questions:

1. What is the difference between public and private access specifier


with respect to inheritance?
2. What are the advantages of inheritance?
3. What are the types of inheritance?
Five marks questions:
1. What is the difference between public and private and protected
access specifier?
2. What are the advantages of inheritance?
3. What are the types of inheritance? Explain any 2.
4. What is virtual base class? Give example.
5. Which are the components which can not be inherited?
6. Explain singe inheritance with a suitable C++ program.
7. Explain the requirements of a virtual base class.
8. What is visibility mode? What is its role with respect to
inheritance?
9. How does inheritance influence the working of constructors and
destructors?
******
Pointer 247

CHAPTER-11

POINTERS

Objectives:

 To understand the concepts of pointers.


 Usage of pointers.
 The role of pointers in array, strings, structures.
 Concepts of dynamic and static allocation of memory.
 Relationship between pointers and functions.
 Relationship between pointers and objects.
Pointer
248
I11.1ntroduction:
When writing a program, you declare the necessary variables that you will
need in order to accomplish your work. When declaring variables, you are simply
asking the computer to reserve a set amount of space in its memory for a particular
object you want to use. When you declare a variable, the computer reserves an
amount of space for that variable, and uses the variable’s name to refer to that
memory space. This will allow you to store the value of that variable, in that
space. Indeed, the computer refers to that space using an address. Therefore,
everything you declare has an address, just like the address of your house. You
can find out what address a particular variable is using.
Pointers are a powerful concept in C++ and have the following advantages.
 It is possible to write efficient programs.
 Memory is utilized properly.
 Dynamically allocate & deallocate memory.
 Easy to deal with hardware components.
 Establishes communication between program and data.
I12.Memory representation of pointers.
Before understanding the concept of pointers it is necessary to know the
memory organization. Memory is organized as an
Address Location array of bytes. A byte is basic storage and
0 accessible unit in memory. Each byte is identifiable
1 by a unique number called address. Suppose we
2 have 1KB of memory, since 1KB=1024 bytes, the
3 memory can be viewed as an array of locations of
— size 1024 with the subscript range (0 to 1023). 0
1022 represents the address of first location; 1
1023 represents the address of second location; and so
on 1023 represents the address of last location.

We know that variables are declared before they are used in a program.
Declaration of a variable tells the compiler to perform the following.
 Allocate a location in memory. The number of location depends on data
type.
 Establish relation between address of the location and the name of the
variable.
Consider the declaration, int num;
Pointer 249
This declaration tells the compiler to reserve a location in memory. We know
that the size of int type is two byte. So the location would be two bytes wide.
Address num
100 15
101

In the above figure, num is the variable that stores the value 15 and address
of num is 100. The address of a variable is also an unsigned integer number. It
can also be retrieved and stored in another variable.
Pointer:
A pointer is a variable that holds a memory address, usually the location
of another variable in memory.
11.3 Declaration and Initialization of pointer:
The general form is, data-type *variable_name;
data-type is any valid data type supported by C++ or any user defined type and
variable_name is the name of pointer variable. The presence of * indicates that it
is a pointer variable.
Defining a Pointer Variable:
int *iptr; iptr is declared to be pointer variable of int type.
float *fptr; fptr is declared to be pointer variable of float type.
char *cptr; cptr is declared to be pointer variable of character type.
Pointer Variables Assignment:
We can assign the address of a variable to a pointer variable as follows:
int num = 25;
int *iptr;
iptr = &num;
In the above example, the variable num (=25) is assigned to pointer variable iptr.
11.4 The address-of operator (&):
& is a unary operator that returns the memory address of its operand. For
example, if var is an integer variable, then &var is its address. This operator has
the same precedence and right-to-left associativity as the other unary operators.
You should read & operator as “the address-of” which means &var will be
read as “the address of var”.
Example: int num = 25;
Pointer
250
int *iptr;
iptr = &num; // The Address of Operator &
11.5 Pointer operator or Indirection Operator (*):
The second operator is Indirection Operator *, and it is the complement of
&. It is a unary operator that returns the value of the variable located at the
address specified by its operand.
Example:
int num = 25;
int *iptr; //Pointer operator (Indirection Operator *):
iptr = &num;
The following program executes the above two operations
#include <iostream>
#include <iomanip.h>
void main( )
{
int var;
int *ptr;
int val;
var = 3000;
ptr = &var;
val = *ptr;
cout << “Value of var: “ << var << endl;
cout << “Value of ptr: “ << ptr << endl;
cout << “Value of val: “ << val << endl;
}
Value of var: 3000
Value of ptr: 0xbff64494
Value of val: 3000
11.6 Pointer Arithmetic:
As you understood, pointer is an address which is a numeric value.
Therefore, you can perform arithmetic operations on a pointer just as you can
on a numeric value.
There are four arithmetic operators that can be used on pointers: ++, —, +, and
. (dot operator).
Following operations can be performed on pointers.
 We can add an integer value to a pointer.
 We can subtract an integer value from a pointer,
Pointer 251
 We can compare two pointers, if they point the elements of the same array
 We can subtract one pointer from another pointer if both point to the
same array.
 We can assign one pointer to another pointer provided both are of same
type.
Following operations cannot be performed on pointers.
 Addition of two pointers.
 Subtraction of one pointer from another pointer when they do not point to
the same array.
 Multiplication of two pointers.
 Division of two pointers.
Example:
a. Suppose if p is an integer pointer then p++ will increment p by 2 bytes.
Each time a pointer is incremented by 1, it points to the memory location
of the next element of its base type.
b. Suppose if p is a char pointer then p++ will incremented p by 1-byte.
c. p-- each time a pointer is decremented by 1, it points to the memory
location of the previous element of its base type.
d. p=p + integer value.
p=p - integer value.
11.7 Pointers and Arrays:
There is a close relationship between arrays and pointers in C++.
Consider the declaration. int a[6];
The elements of the array can be referred to in the program as a[0], a[1],
…. , a[9]. When the program is compiled, the compiler does not save the addresses
of all the elements, but only the address of the
first element, a[0]. When the program needs to
access any element, a[i], it calculates its address
A[0] A[1] A[2] A[3] A[4] by adding i units to the address of a[0]. The
number of bytes in each “unit” is, in our example,
equal to the sizeof(int). i.e., 2. In general, it is equal
to the number of bytes required to store an element of the array.
The address of a[0] can be explicitly obtained using the & (address-of)
operator. i.e., &a[0]. Since the data type of a[0] is int, the data type of &a[0] is, as
usual, int* (pointer to int).
Pointer
252
C++ allows us to use the name of the array a, without any subscript, as
another name for &a[0].
The following example shows the relationship between pointer and one-
dimensional array.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main( )
{
int a[10], i, n;
cout<<"How many elements? ";
cin>>n;
cout<<"Enter array elements: ";
for(i=0; i<n; i++)
cin>>*(a+i);
cout<<"The given array elements are ";
for(i=0; i<n; i++)
cout<<setw(4)<<*(a+i);
getch();
}
How many elements? 5
Enter array elements: 1 2 3 4 5
The given array elements are 1 2 3 4 5
11.8 Array of pointers:
As we know that there is an array of integers, array of float, similarly,
there can be an array of pointers. Since we know that pointer is a variable which
stores address of another variable, an array of pointers means that it is a collection
of addresses.
The example below shows the array of pointers.
int *iptr[5];
int i=10, j=20, k=30, l=40, m=50;
iptr[0] = &i; *iptr[0] = 10;
iptr[1] = &j; *iptr[1] = 20;
iptr[2] = &k; *iptr[2] = 30;
iptr[3] = &l; *iptr[3] = 40;
iptr[4] = &m; *iptr[4] = 50;
Pointer 253
11.9 Pointers and Strings:
We have already discussed that there is a close relationship between array
and pointers. Similarly there is also a close relationship between strings and
pointers in C++. String is sequence of characters ends with null (‘\0’) character.
Suppose we have declared an array of 5 elements of the data type character.
char s[5];
char *cptr;
cptr = s;
Here, s is array of characters (strings). cptr is character pointer to string. s
also represents character pointer to string.
The elements of the array can be referred to in the program as s[0], s[1],
…. , s[5]. When the program is compiled, the compiler does not save the addresses
of all the elements, but only the name of the array. Here, s gives the base
address of the array. i.e., the address of the first character in the string variable
and hence can be regarded as pointer to character. Since we know that string
always end with null character, it is enough for us to know the starting address
of a string to be able to access entire string. The number of bytes allocated for a
string is determined by the number of characters within string.
Let us now consider a string constant “HELLO”. s is pointer to the
memory location where ‘H’ is stored. Here, s can be viewed as a character array
of size 6, the only difference being that a can be reassigned another memory
location.
char s[5] = “Hello”; H E L L O \0
s[0] s[1] s[2] s[3] s[4] s[5]
Here, s gives address of ‘H’.
*a gives ‘H’
a[0] gives ‘H’
a++ gives address of ‘E’
*a++ gives ‘E'
11.10 Pointers as Function Parameters.
A pointer can be a parameter. It works like a reference parameter to allow
change to argument from within the function.
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
swap(&num1, &num2);
Pointer
254
11.11 Pointers and Structures
We can create pointers to structure variables.
struct student
{
int rollno;
float fees;
};
student s;
student *sp = &s;
(*sp).rollno = 104;
The above statements can be written using the operator -> as
ptr -> member:
sp -> rollno = 104;
11.12. Memory allocation of pointers (Dynamic and Static )
The compiler allocates the required memory space for a declared variable.
For example, integer variable it reserves 2- bytes, float variable it reserves 4-
bytes, character variable it reserves 1-byte and so on. Therefore every data and
instruction that is being executed must be allocated some space in the main or
internal memory. Memory allocation is done in two ways:
 Static allocation of memory
 Dynamic allocation of memory
11.12.1 Static allocation of memory
In the static memory allocation, the amount of memory to be allocated is
predicted and pre known. This memory is allocated during the compilation itself.
All the variables declared normally, are allocated memory statically.
Example: int a; //Allocates 2 bytes of memory space during the
//compilation time.
11.12.2 Dynamic allocation of memory ( new and delete)
In the dynamic memory allocation, the amount of memory to be allocated
is not known. This memory is allocated during run-time as and when required.
C++ supports dynamic allocation and deallocation of objects using the
new and delete operators. These operators allocate memory for objects from a
pool called the free store. The new operator calls the special function operator
new and the delete operator calls the special function operator delete.
We can allocate storage for a variable while program is running by using
new operator. Dynamic allocation is perhaps the key to pointers. It is used to
allocate memory without having to define variables and then make pointers
Pointer 255
point to them. Although the concept may appear confusing, it is really simple.
The following codes demonstrate how to allocate memory for different variables.
To allocate memory of type integer, int *iptr = new int;
int *pNumber;
pNumber = new int;
The first line declares the pointer, pNumber. The second line then allocates
memory for an integer and then makes pNumber point to this new memory.
Here is another example, this time using a double:
double *pDouble;
pDouble = new double;
To allocate memory for array, double *dptr = new double[25];
To allocate dynamic structure variables or objects,
student sp = new student; //student is tag name of structure
The formula is the same every time, so you can’t really fail with this bit.
What is different about dynamic allocation, however, is that the memory you
allocate is not deleted when the function returns, or when execution leaves the
current block. So, if we rewrite the above example using dynamic allocation, we
can see that it works fine now:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int *p;
void SomeFunction()
{
// make p pointer point to a new integer
p = new int;
*p = 25;
}
void main()
{
SomeFunction(); // make pPointer point to something
cout<<"Value of *p: "<<*p;
} Output Value of *p: 25

When SomeFunction is called, it allocates some memory and makes p


point to it. This time, when the function returns, the new memory is left intact,
so p still points to something useful.
Pointer
256
delete pointer:
Memory that is dynamically allocated using the NEW operator can be freed using
delete operator. THe delete operator calls the operator delete functio, which
frees memory back to the avalable pool.
Releasing Dynamic Memory
Use delete function to free dynamic memory as: delete iptr;
To free dynamic array memory, delete [ ] dptr;
To free dynamic structure, delete student;

Static allocation of memory Dynamic allocation of memory

Memory is allocated before the Memory is allocated during the


execution of the program begins. execution of the program.
(During Compilation) Memory Bindings are established
No memory allocation or deallocation and destroyed during the Execution.
actions are performed during Allocated only when program unit is
Execution. active.
Variables remain permanently Implemented using data segments.
allocated.

Implemented using stacks and


heaps.
11.13 Free store( heap memory)
Free store is a pool of unallocated memory heap given to a program that is
used by the program for dynamic allocation during execution.
11.14 Memory Leak
If the objects, that are allocated memory dynamically, are not deleted
using delete, the memory block remains occupied even at the end of the program.
Such memory blocks are known as orphaned memory blocks. These orphaned
memory blocks when increase in number, bring adverse effect on the system.
This situation is called memory leak.
11.15 Self Referential Structure
The self referential structures are structures that include an element that
is a pointer to another structure of the same type.
Pointer 257
struct node
{
i n t d a t a ;

n o d e * n e x t ;

11.16 Pointers and functions


A function is named unit of a group of program statements designed to
perform a specific task and returns single value. There is a close relationship
between pointers and functions. We know that a function uses arguments in
order to carry its assignment. The arguments are usually provided to the function.
When necessary, a function also declares its own variable to get the desired
return value. Like other variables, pointers can be provided to a function, with
just a few rules. When declaring a function that takes a pointer as an argument,
make sure you use the asterisk for each argument. When calling the function,
use the references to the variables. The function will perform its assignment on
the referenced variable(s). After the function has performed its assignment, the
changed value(s) of the argument(s) will be preserved and given to the calling
function. To pass pointer arguments, use the asterisks when declaring the
function and use the ampersand (&) when calling the function.
Invoking of function can be done by following two methods:
 By passing the references.
 By passing the pointers.
11.16.1. Invoking functions by passing the references
When parameters are passed to the functions by reference, the formal
parameters become reference (or aliases) to the actual parameters in the calling
function. This means that invoking the called function does not create its own
copy of original values, rather than, it refers to the original values by different
names i.e., their references. Thus the called function works with the original
data and any change in the values gets reflected to the data.
The call by reference method is useful in situation where the values of the
original variable are to be changed using a function. Say, for instance a function
is to be invoked that swap two variables that are passed by references. The
following example program explains it.
Program to swap the values of two variables using pass-by-reference
method:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void main()
{
Pointer
258
void swap(int &, int &);
int a=10, b=30;
cout<<“Original values: “;
cout<<“a = “<<a<<“ and b = “<<b<<endl;
swap(a,b);
cout<<“values after swapping: “;
cout<<“a = “<<a<<“ and b = “<<b<<endl;
getch();
}
void swap( int &x , int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}
void swap( int &x , int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}

Original values: a = 10 and b = 30


Values after swapping: a = 30 and b = 10
In the above program the function swap( ) creates reference x for first
incoming integer and reference y for second incoming integer. Thus the original
values are worked with, but by using the names x and y. Notice that the, function
call statement is simple one. i.e., swap(a, b);
But the function declaration (prototype) and definition include the
reference symbol &. The function declaration and definition, both start as: void
swap( int &x , int &y)
Therefore, by passing the references the function works with the original
values (i.e., the same memory area in which original values are stored) but in
case alias names to refer to them. Thus the values are not duplicated. The same
happens when pointers are passed but in different manner.
11.16.2. Invoking functions by passing the pointers
When the pointers are passed to the function, the addresses of actual
arguments in the calling function are copied into formal arguments of the called
function. This means that using formal arguments ( the addresses of original
Pointer 259
values) in the called function, we can make changes into the actual arguments
of the calling function, therefore here also, the called function does not create
own copy of original values rather, it refers to the original values by the addresses(
passed through pointers) it receives.
To swap two values, we have seen how the passing references method
works. The same can be achieved by passing addresses through pointers. The
following example program explains it.

Program to swap values of two variables using pass by references method:


#include <iostream.h>
#include <conio.h>
void main()
{
void swap(int *x int *y);
int a=10,b=30;
cout<<“Original values: “;
cout<<“a = “<<a<<“ and b=”<<b<<endl;
swap(&a, &b);
cout<<“values after swapping: “;
cout<<“a = “<<a<<“ and b = “<<b<<endl;
getch();
}
void swap( int *x , int *y)
{
int temp;
temp= *x;
*x = *y;
*y = *temp;
}
Original values: a = 10 and b = 30
Values after swapping: a = 30 and b = 10
The above program invokes swap( ) by passing addresses of a and b. i.e.,
swap(&a , &b); Here, &a and &b pass the addresses of a and b respectively.
The function definition receives the incoming addresses in corresponding pointers
x and y. Notice the function declaration.
void swap( int *x, int *y);
Thus, we have seen that using call-by-reference, in both ways, we are able
to return more than one value at a time (we sent back changed values of two
Pointer
260
variables a and b), which are not possible using ordinary return statement. A
return statement can return only one value from a function at a time.
11.17 Memory Comes, Memory Goes
There’s always a complication and this one could become quite serious,
although it’s very easy to remedy. The problem is that although the memory
that you allocate using dynamic allocation is conveniently left intact, it actually
never gets deleted automatically. That is, the memory will stay allocated until
you tell the computer that you’ve finished with it. The upshot of this is that if
you don’t tell the computer that you’ve finished with the memory, it will be
wasting space that other applications or other parts of your application could be
using. This eventually will lead to a system crash through all the memory being
used up, so it’s pretty important, freeing the memory when you’ve finished with
it is very simple:
11.18 Pointers and objects
As we know that there is pointer to variables, pointer to strings, pointer to
structures, similarly there is pointer to objects. The pointers pointing to objects
are referred to as object pointers.
Declaration of pointers to objects
class_name *object-pointer;
Here, class_name is the name of an already defined class and object-
pointer is the pointer to an object of this classtype.
Example: employee *eptr;
Here, employee is an already defined class. When accessing members of a
class using an object pointer, the arrow operator (->) is used instead of dot (.)
operator.
The following program illustrates how to access an object given a pointer to it.
#include<iostream.h>
#include <iomanip.h>
#include<conio.h>
class emp
{
private:
int empno;
char name[20];
float salary;
public:
void get();
void display();
Pointer 261
};
void emp::get()
{
cout<<“Enter employee number: “;
cin>>empno;
cout<<“Enter employee name: “;
cin>>name;
cout<<“Enter employee salary: “;
cin>>salary;
}
void emp::display()
{
cout<<“Employee number: “<<empno<<endl;
cout<<“Employee name: “<<name<<endl;
cout<<“Employee salary: “<<salary;
}
void main( )
{
emp e, *ep;
ep = &e;
clrscr( );
ep->get( );
ep->display( );
getch( );
}
Enter employee number: 2505
Enter employee name: Harshini
Enter employee salary: 6500.00
Employee number: 2505
Employee name: Harshini
Employee salary: 6500.00
The given program is self referential. Here, *ep is pointer to an object.
11.19 this pointer
Every object in C++ has access to its own address through an important
pointer called this pointer. The this pointer is an implicit parameter to all member
functions. Therefore, inside a member function, this may be used to refer to the
invoking object.
Pointer
262
Friend functions do not have a this pointer, because friends are not
members of a class. Only member functions have this pointer.
Points to Remember:
 Pointers are a powerful concept in C++ and have following advantages.
 It is possible to write efficient programs
 Memory is utilized properly
 Dynamically allocate & de allocate-memory
 Easy to deal with hardware components
 Establishes communication between program and data
 A pointer is a variable that holds a memory address, usually the location
of another variable in memory.
 Following operations that can be performed over pointers.
 We can add an integer value to a pointer.
 We can subtract an integer value from a pointer,
 We can compare two pointers. if they point the elements of the
same array
 We can subtract one pointer from another pointer if both point to
the same array.
 Following operations that cannot be performed over pointers.
 Addition of two pointers
 Subtraction of one pointer from another pointer when they do not
point to the same array
 Multiplication of two pointers
 Division of two pointers
 There is a close relationship between arrays and pointers in C++.
 C++ allows us to use the name of the array a , without any subscript, as
another name for &a[0].
 Array of pointers means that it is a collection of address.
 There is also a close relationship between strings and pointers in C++.
 A pointer can be a parameter. It works like a reference parameter to allow
change to argument from within function
 We can create pointers to structure variables
 In the static memory allocation, the amount of memory to be allocated is
predicted and pre known.
Pointer 263
 In the dynamic memory allocation, the amount of memory to be allocated
is not known. This memory is allocated during run-time as and when
required.
 We can allocate storage for a variable while program is running by using
new operator.
 Use delete to free dynamic memory.
 Free store is a pool of unallocated heap memory given to a program that is
used by the program for dynamic allocation during execution.
 Memory leak: If the objects, that are allocated memory dynamically, are
not deleted using delete, the memory block remains occupied even at the
end of the program. Such memory blocks are known as orphaned memory
blocks. These orphaned memory blocks when increase in number, bring
adverse effect on the system. This situation is called memory leak
 The self referential structures are structures that include an element that
is a pointer to another structure of the same type.
 There is a close relationship between pointers and functions. We know
that a function uses arguments in order to carry its assignment.
 Invoking of function can be done by following two methods.
 By passing the references
 By passing the pointers
 The pointers pointing to objects are referred to as object pointers.
 Every object in C++ has access to its own address through an important
pointer called this pointer.
Pointer
264
Review Questions
One marks questions.
1. What do you mean by pointer?.
2. Mention any one advantage of pointer?
3. What is address operator?
4. What is pointer operator?
5. How to declare pointer?
6. How to initialize pointer?
7. What is static memory?
8. What is dynamic memory?
9. What is free store?
10. Write a definition for a variable of type pointer to float.
11. What is new operator in C++?
12. What is delete operator in C++?
Two marks questions.
1. What do you mean by pointer? Explain with example.
2. Mention any 2 advantages of pointer?
3. What is address operator? Give example.
4. What is pointer operator? Give example.
5. How to declare pointer? Give example.
6. How to initialize pointer? Give example.
7. What is static memory?
8. What is dynamic memory?
9. What is free store?
10. Illustrate the use of “self referential structures” with the
help of example.
11. What is new operator in C++?
12 What is delete operator in C++?
13. What is array of pointers? Give example.
Pointer 265

Three marks questions:


1. What are the advantages of pointer?
2. How dynamic memory allocation is different from static memory
allocation.
3. What is new operator in C++? Give example.
4. What is delete operator in C++? Give example.
5. Show the general form new and delete operator in C++?
6. What is array of pointers? Give example.
7. What is the relationship between array and pointers? Give example.
8 What is the relationship between string and pointers? Give example.
9. What is the relationship between structures and pointers? Give
example.
10. What is the relationship between object and pointers? Give example.
Five marks questions:
1. Show the general form new and delete operator in C++?
2. What is the relationship between object and pointers? Give example.
3. Explain with example by passing the reference.
4. Explain with example by passing the pointers.
*****
266 Data file handling

CHAPTER 12

DATA FILE HANDLING

OBJECTIVES

 To understand the concepts of files.


 Usage of types of files.
 The role of text and binary files.
 Concept of opening and closing of files
 Concept of input and output operations in text files.
 Concept of input and output operations in binary files.
 Concept of file pointers and their manipulations.
Data file handling 267

12.1 Introduction:
We have already used the cin and cout for handling the input and ouput
operations. They are used to accept inputs through keyboard and display outputs
on the screen. C++ provides a rich set of operations for both unformatted and
formatted I/O operations. In C++, these IO operations are implemented through
iostream library.
The C++ standard libraries provide an extensive set of input/output
capabilities which we will see in subsequent topics. This chapter will discuss
very basic and most common I/O operations required for C++ programming.
C++ I/O occurs in streams, which are sequences of bytes. If bytes flow
from device like a keyboard, a disk drive, or a network connection etc. to main
memory, this is called input operation and if bytes flow from main memory to a
device like a display screen, a printer, a disk drive, or a network connection etc.,
this is called output operation.
Most computer programs work with files. This is because files help in
storing information permanently. Word processors create document files.
Database programs create files of information. Compilers read source files and
generate executable files. So, we see, it is the files that are mostly worked with,
inside programs. A file itself is a bunch of bytes stored on some storage device
like tape or magnetic disk etc.
In C++, file input/output facilities are implemented through a header file
of C++ standard library. This header file is fstream.h.
In C++, a file, at its lowest level, is interpreted simply as a sequence, or
stream of bytes. In C++, file I/O library manages the transfer of these bytes. At
this level, the notion of a data type is absent.
On the other hand, file, at user level, consists of a sequence of possibly
intermixed data types-characters, arithmetic values and class objects.
The fstream library predefines a set of operations for handling files related
input and output. It defines certain classes that help to perform file input and
output. For example, ifstream class ties a file to the program for input. ofstream
class ties a file to the program for output and fstream classifies a file to the
program for both input and output. File manipulation and related operations
using streams are the topics we are going to discuss in this chapter.
268 Data file handling

12.2 fstream.h header file


The C++ input/output operations are very much similar to the console
input and output operations. The file operations also make use of streams as an
interface between the programs and the files.
A stream is a general name given to a flow of data at the lowest level; data
is just the binary data without any notion of data type. Different streams are
used to represent different kinds of data flow such as whether data is flowing
into the memory or out of the memory. Each stream is associated with a particular
class, which contains member functions and definitions for dealing with that
particular kind of data flow. For example, the ifstream class represents input
disk files.
The stream that supplies data to the program is known as input stream. It
reads the data from the file and hand it over to the program. The stream that
receives data from the program is known as output stream. It writes the received
data to the file. Following figure shows it.

The information / data stored under a specific name on a storage


device, is called a file.
Stream refers to a sequence of bytes
Data file handling 269

fig: Stream class hierarchy for I/O operations:


The fstream base is derived from the basic class ios. The class ifstream is
derived from both istream and fstream base and similarly, the class ofstream is
derived from both the ostream and fstream base. Both ifstream and ofstream act
as base classes for fstream class. The class filebuf is derived from class streambuf.
The complete class hierarchy is shown above.Central to file handling are three
classes. They are ifstream, ofstream and fstream.
That is why, if you include fstream.h file in your file handling program,
you need not to include iostream.h file as classes of fstream.h inherit from
iostream.h only.The functions of these classes have summarized in the below
table.
12.2.1 Classes for file stream operation
Classes Meanings

filebuf It sets the file buffers to read and write.

It provides the facilities for file operations and consists of open(


fstreambase ) and close( ) member functions. This is base class for fstream,
ifstream, ofstream.

ifstream Stream class to read from files. It provides input operations


for file. It inherits the function get( ), getline( ),read( ) and
functions supporting random access (seekg( ) and tellg( )) from
istream class defined inside iostream.h file.

ofstream Stream class to write on files. It provides output operations


for file. It inherits the function put( ), write( )and functions
270 Data file handling

supporting random access (seekp( ) and tellp ()) from


ostream class defined inside iostream.h file.

Stream class to both read and write from/to files.It


fstream provides support for simultaneous input and output
operations. It inherits all the functions from istream
and ostream classes through iostream class defined
inside iostream.h file.

12.3 Types of data Files:


Files are used to store data or information permanently for future use.
Depending on how data are stored and retrieved, the files are classified into two
types. They are Text file and Binary file.
12.3.1 Text file:
It is a file that stores information in ASCII characters. In text files, each
line of text is terminated with a special character known as EOL (End- of-line)
character or delimiter character. When this EOL character is read or written,
certain internal translations take place.
12.3.2 Binary file:
It is a file that contains information in the same format as it is held in
memory. In binary files, no delimiters are used for a line and no translations
occur here.
12.4 Opening and Closing files:
In C++, while opening a file, we need the stream like input, output and
input_output. To create an input stream you must declare the stream to be of
class ifstream. To create an output stream you must declare the stream to be of
class ofstream. Streams that will be performing both input and output operations
must be declared as class fstream.
Opening a file can be accomplished in two ways:
 Opening file using constructor
 Opening file using open( ) member function
The first method is preferred when a single file is used with a stream.
However for managing multiple files with the same stream, the second method
is preferred.
12.4.1 Opening file using constructor:
The syntax of opening a file for output purpose only using an object of
ofstream class and the constructor is as follows:
ofstream ofstream _object(“file_name”);
Data file handling 271

ofstream _object is an object of type ofstream and “file name” is any valid
identifier of a file to be opened for output purpose only.
Example: ofstreamfout(“results.dat”); //output only
ofstreamfout(“text.dat”); //output only
fout is declared to be an object of ofstream type and it is made to represent
the file results.dat and text.dat opened for output purpose only.
The syntax of opening a file for input purpose only using an object of
ifstream class and the constructor is as follows.
ifstream ifstream _object(“file_name”);
ifstream _object is an object of type ifstream and “file name” is any valid
identifier name of a file to opened for input purpose only.
Example: ifstream fin(“results.dat”); //input only
ifstream fin(“text.dat”); //input only
fin is declared to be an object of ifstream type and it is made to represent
the file results.dat and text.dat opened for input purpose only.
12.4.2 Opening file using open( ):
The syntax for opening a file for output purpose only using an object of
ofstream class and open( ) member function is as follows:
ofstream-object.open(“filename”)
ofstream-object is an object of type ofstream and “filename” is any valid
name of a file to be opened for output purpose only.
Example: ofstream ofile;
ofile.open(“data1”);
ofile.open(“text.dat”);
ofile is declared to be an object of ofstream type and is made to represent
the file data1 or text.dat opened for output purpose only.
The syntax for opening a file for input purpose only using an object of
ifstream class and open( ) member function is as follows:
ifstream-object.open(“filename”)
ifstream-object is an object of type ifstream and “file name” is any valid
name of a file to be opened for input purpose only.
Example: ifstream ifile;
ifile.open(“data1”);
ifile.open(“text.dat”);
272 Data file handling

ifile is declared to be an object of ifstream type and is made to represent


the file data1 or text.dat opened for output purpose only.
If we have to open a file for both input and output operations, we use
objects of fstream class. We know that the class fstream is derived from both
ifstream and ofstream. As a result objects of the class can invoke all the members
of the function of its base class.
The syntax for opening a file, an object of type fstream class and the
constructor is as follows:
fstream fstream-object(“filename”, mode)
The syntax for opening a file an object of type fstream class and the open
() member function is as follows:
fstream-object.open(“filename”,mode)
The need of mode is provided in the following table:
12.4.3. Concepts of file modes (in, out, app modes)
File mode parameter Meaning Stream type
ios::app Append to end of file ofstream
ios::in open file for reading only ifstream
ios::out open file for writing only ofstream
ios::ate Open file for updation and move ifstream ,
the file pointer to the end of file ofstream
ios:binary Opening a binary file ifstream ,
ofstream
ios::noreplace Turn down opening if the file
already exists ofstream
ios::nocreate Turn down opening if the file
does not exists ofstream
ios::trunc On opening, delete the contents of file ofstream

All these flags can be combined using the bitwise operator OR (|).
Example: fstream fout(“text.dat”,ios::out); open text.dat in output mode
fstream fin(“text.dat”,ios::in); open text.dat in input mode
fstream file;
file.open (“example.bin”, ios::out | ios::app | ios::binary);
If we want to open the file “example.bin” in binary mode to add data we
could do it by the above call to member function.
Data file handling 273

12.4.4. Closing File:


We learn that opening a file establishes the linkage between a stream
object and an operating system file. After the intended operations are done with
the file, the file should be safely saved on the secondary storage for later retrieval.
This is done by the member function close() The function on its execution removes
the linkage between the file and the stream object.
Syntax: stream_object.close();
fout.close();
fin.close();
12.5. Input and output operation on text files:
We know that a text file consists of a group of ASCII values. The data in
text files are organized into lines with new line character as terminator. Text
files need following types of character input and output operations:
 put() function
 get() function
put(): The put() member function belongs to the class ofstream and writes a
single character to the associated stream.
Syntax: ofstream_object.put(ch);
ch is character constant or char variable. The function writes ch onto the
file represented by ofstream_object.
char ch = ‘a’;
ofstream fout(“text.txt”);
fout.put(ch);
Write the character stored in ch onto the file represented by the object
fout. i.e., text.txt.
get( ): The get() member function belongs to the class ifstream and the function
get() reads a single character from the associated stream.
Syntax: ifstream_object.get(ch);
ch is character constant or char variable. The function reads ch from the
file represented by the ifstream_object into the variable ch.
char ch = ‘a’;
ifstream fin(“text.txt”);
fin.get(ch);
Reads a character into the variable ch the current byte position from the
file represented by the object fin, i.e., text.txt.
274 Data file handling

String I/O:
The getline( ) function:
It is used to read a whole line of text. It belongs to the class ifstream.
Syntax: fin.getline(buffer, SIZE);
Reads SIZE characters from the file represented by the object fin or till
the new line character is encountered, whichever comes first into the buffer.
Example: char book[SIZE];
fstream fin;
fin.getline(book, SIZE);
Input and output operation on binary files:
The binary files are of very much use when we have to deal with database
consisting of records. Since the records usually comprise heterogeneous data
types, the binary files help optimize storage space and file I/O would be faster
when compared to text files. Binary files needs following types of input and
output operations.
 write() member function
 read() member function
write( ): The write() member function belongs to the class ofstream and which
is used to write binary data to a file.
Syntax: ofstream_object.write((char *) &variable, sizeof(variable));
fout is an object of type ofstream. The function requires two arguments.
The first argument ofstream_object is the address of the variable, the contents of
which are written to the file and the second argument is the size of the variable.
The address of the variable is type casted to pointer to char type. It is because
the write function does not bother to know the type of variable. It requires data
in terms of only bytes. The function writes the contents of variable to the file
represented by the object fout.
Example: student s;
ofstream fout(“std.dat”,ios::binary);
fout.write((char*) &s, sizeof(s));
Write the contents of the object s to the file std.dat.
read( ): The read() member function belongs to the class ifstream and which
is used to read binary data from a file.
Syntax: ifstream_object.read((char *) &variable, sizeof(variable));
ifstream_object is an object of type ifstream. The function requires two
arguments. The first argument is the address of the variable, the contents of
which are read from the file and the second argument is the size of the variable.
Data file handling 275

The address of the variable is type casted to pointer to char type. It is because
the read function does not bother to know the type of variable. It requires data
in terms of only bytes. The function reads a record from the file represented by
the object fin to the object std.
Example: student s;
ifstream fin(“std.dat”,ios::binary);
fin.read((char*) &s, sizeof(s));
Read a student record from the file std.dat into the object s.
12.6. Detecting end of file:
While reading the contents of a file, care has to be taken to see to it that
the operation does not cross the end of file. The ios class provides a member
function by name eof(), which helps in detecting the end of file. Once the end of
file is detected with the use of eof() member function, we can stop reading further.
eof() returns true (non zero) if end of file is encountered while reading;
otherwise return false(zero).
if(fin.eof())
{
statements;
}
This is used to execute set statements on reaching the end of the file
represented by the object fin.
while (!fin.eof())
{
statements;
}
This is used to execute set statements as long as the end of the file fin is
not reached.
12.7. File pointers and their manipulation:
In C++, the file I/O operations are associated with the two file pointers,
known as get pointer and the put pointer. These are synonymous for input
pointer and output pointer respectively. They are useful in traversing the opened
file while reading or writing.
 ifstream, like istream, has a pointer known as the get pointer that
points to the element to be read in the next input operation.
 ofstream, like ostream, has a pointer known as the put pointer that
points to the location where the next element has to be written.
276 Data file handling

When an input or output operation is performed, the appropriate pointer


is automatically advanced. So the programmer need not bother about
incrementing the file pointer to the next location inside the file for the next
action.
There are three modes under which we can open a file:
 Read only mode
 Write only mode
 Append mode
When a file is opened in a read only mode, the get pointer is automatically
set to the very first byte (0th byte) of the file. This helps to read the file contents
from the beginning (The bytes in a file is numbered starting from zero).
Similarly, when a file is opened in a write only mode, the contents of file
are erased (if it exists) and the put pointer is set to the first byte of the file, so
that we can write data from the beginning. In some situations, it would be
necessary for us to add new data (or text) to the existing file. In this case we have
to open the file in append mode. When a file is opened in a append mode, the
put pointer moves to the end-of-file, so that we write new data from that location.
These internal stream pointers that point to the reading or writing locations
within a stream can be manipulated using the following member functions:
 seekg()
 seekp()
 tellg()
 tellp()
seekg():
Move the get pointer to a specified location from the beginning of a
file. There are two types:
 seekg(long);
 seekg(offset, seekdir);
The seekg(long) moves the get pointer to a specified location from the
beginning of a file.
Example: inf.seekg(20) ;
The above example tells that the get pointer points to 20th byte in a file
from 0th byte.
The seekg(offset, seekdir) has two arguments: offset and seekdir. The offset
indicates the number of bytes the get pointer is to be moved from seekdir
position.
Data file handling 277

The offset takes long data type and seekdir (direction for seeking the offset
position inside a file) takes one of the following three seek direction constants.
These constants are defined in ios class.
Constant Meaning
ios::beg Offset specified from the beginning of the file
ios::cur Offset specified from the current position of the get pointer
ios::end Offset specified from the end of the file
Examples: inf.seekg(0, ios::beg);
Move the get pointer to the 0th byte (i.e., beginning of the file).
inf.seekg(20, ios::beg);
Moves the get pointer to the 20th byte (i.e., from current position of the file
in forward direction).
inf.seeg(-20, ios::beg );
The above example tells that the get pointer points to 20th byte in a file
from end of file in backward direction.
seekp():
Move the put pointer to a specified location from the beginning of a file.
There are two types:
 seekp(long);
 seekp(offset, seekdir);
The seekp(long) moves the put pointer to a specified location from the
beginning of a file.
Example: inf.seekg(20) ;
The above example tells that the put pointer points to 20th byte in a file
from 0th byte.
The seekp(offset, seekdir) has two arguments offset and seekdir. The offset
indicates the number of bytes the put pointer is to be moved from seekdir
position.
The offset takes long data type and seekdir (direction for seeking the offset
position inside a file) takes one of the following three seek direction constants.
These constants are defined in ios class (see above table).
Examples: inf.seekp(0, ios::beg);
Move the put pointer to the 0th byte (i.e, beginning of the file) for writing.
inf.seekg(20, ios::beg) ;
278 Data file handling

Move the put pointer to the 20th byte (i.e, from current position of the file
in forward direction) for writing.
inf.seekg(-20, ios::beg) ;
The above example tells that the put pointer points to 20th byte in a file
from end of file in backward direction.
tellg( ) member function:
The ifstream class provides the member function name tellg(). The purpose
of the function is to return current position of the get pointer.
Syntax: int position;
position = fin.tellg();
tellp() member function:
The ifstream class provides the member function name tellp( ); The purpose
of the function is to return current position of the put pointer.
Syntax: int position;
position = fin.tellp();
Basic operation on binary file in C++

The basic Operation on Binary File in C++ are


1. Searching.
2. Appending data.
3. Inserting data in sorted files.
4. Deleting a record
5. Modifying data
Points to remember:
 File: The information / data stored under a specific name on a storage
device, is called a file.
 Stream: It refers to a sequence of bytes.
 ifstream: Stream class to read from files. It provides input operations
for file.
 ofstream: Stream class to write on files. It provides output operations
for file.
 fstream: Stream class to both read and write from/to files.
 Text file: It is a file that stores information in ASCII characters.
Data file handling 279

 Binary file: It is a file that contains information in the same format as it is


held in memory.
 File can be opened in two ways :
a. Opening file using constructor: Useful when a single file used with
stream.
b. Opening file using open( ): Useful for managing multiple files with
the same stream.
 The classes defined with inside fstream.h derive from classes under
iostream.h.
 File can be closed using function close ( ).
 File modes: Describes how a file is to be used.
 ios::in: Open file for reading only.
 ios::out: Open file for writing only.
 ios::app: Append to end of file.
 The put() member function belongs to the class ofstream and writes a
single character to the associated stream.
 The get() member function belongs to the class ifstream and the function
get() reads a single character from the associated stream.
 getline(): It is used to read a whole line of text. It belongs to the class
ifstream.
 The write() member function belongs to the class ofstream and which is
used to write binary data to a file.
 The read() member function belongs to the class ifstream and which is
used to read binary data from a file.
 eof():Helps in detecting the end of file.
 eof(): returns true (non-zero) if end-of-file is encountered while reading,
otherwise return false(zero).
 seekg():Moves the get pointer to a specified location from the beginning of
a file.
 The seekg(long) moves the get pointer to a specified location from the
beginning of a file.
 seekp():Moves the put pointer to a specified location from the beginning
of a file.
280 Data file handling

 tellg(): The ifstream class provides the member function name tellg(); The
purpose of the function is to return current position of the get pointer.
 tellp(): The ifstream class provides the member function name tellp(); The
purpose of the function is to return current position of the put pointer.

One marks questions:


1. Which header file is required for file handling functions in C++.
2. What is stream?
3. Name the streams generally used for file I/O.
4. What are output streams?
5. What are input streams?
6. Mention the methods of opening file within C++ program.
7. Write the member functions belonging to fstream class.
8. What is ifstream class
9. What is ofstream class.
10. Write the member functions belonging to ofstream class.
11. Write the member functions belonging to ifstream class.
12. Name the stream classes supported by C++ for file input.
13. Name the stream classes supported by C++ for output.
14. Mention the file modes.
15. What is ios :: in?
16. What is ios::out?
17. Mention the types of file.
18. What is text file.
19. What is binary file.
20. What is the use of write ( ) function.
21. What is the use of writeln ( ) function.
22. What is the use of get ( ) function.
23. What is the use of put ( ) function.
24. What is the use of getline ( ) function.
25. What is the use of read ( ) function.
26. What is the use of seekp ( ) function.
27. What is the use of seekg ( ) function.
28. What is the use of eof ( ) function.
Two marks questions:
1. What is stream? Name the streams generally used for file I/O.
2. What are input and output streams?
3. Mention the methods of opening file within C++ . Discuss any one.
4. Write the member functions belonging to fstream class.
5. Differentiate between ifstream class and ofstream class.
6. Differentiate between read ( ) and write ( ).
7. Differentiate between get ( ) and getline ( ).
Data file handling 281

8. Write the member functions belonging to ofstream class.


9. Write the member functions belonging to ifstream class.
10. Name the stream classes supported by C++ for file input and output.
11. What are the advantages of saving data in Binary form
Three marks questions:
1. Mention the methods of opening file within C++ program. Discuss.
2. Differentiate between ifstream class and ofstream class.
3. Differentiate between read ( ) and write ( ).
4. Differentiate between get ( ) and getline ( ).
5. Name the stream classes supported by C++ for file input and output.
6. Mention the types of file. Explain any one.
7. What are the advantages of saving data in 1.Binary form. 2. Text form.
Five marks questions:
1. What are input and output streams?
2. What is significance of fsream.h header file.
3. Mention the methods of opening file within C++, Discuss.
4. Differentiate between ifstream class and ofstream class.
5. Differentiate between read ( ) and write ( ) with example.
6. Differentiate between get ( ) and getline ( ) with example.
7. Explain any three file modes.
8. Differentiate between ios::in and ios::out.
*****
282 Data Concepts

CHAPTER 13

DATABASE CONCEPTS

OBJECTIVES

 To understand the concept of database.


 Various terms used in database.
 To understand various data model.
 To understand how the keys are used in database
 Data warehouse and datamining.
Data Concepts 283

13.1 Introduction :
The large and complex data which are collected, entered, stored and accessed
based on the users needs are in the form of queries. Unique softwares are
developed and used, which are highly secured and complex. This chapter will
illustrate the database concepts.
Today the database is used everywhere and in all walks of life. The database
is becoming the backbone of all the softwares from standalone, client-server,on-
line, mainframe, supercomputers etc. For exam pl e use in business,
government agency, service organization etc. Wherever the data and information
is required, there the database software is used and the results are presented in
the form of reports or graphical representation, which are easier for
understanding, so that furture activities can be carried out based on these reports.
One of the area where database is used can be schools; for example. The Fees
payment software. The basic requirements for new student admission are Name,
student id, admission no, father name, class, section, amount, date of entry into
the school are entered and saved. Some of the activities can be admission fees/
annual fees, monthly fees, late payment etc., created and the respective reports
are generated.
DATABASE
284 Data Concepts

13.2 APPLICATION OF DATABASE:


Databases are widely used. Here are some representative applications:
1. Banking: For customer information, accounts, and loans, and banking
transactions.
2. Water meter billing : The RR number and all the details are stored in
the database and connected to the server based works.
3. Rail and Airlines: For reservations and schedule information. Airlines
were among the first to use databases in a geographically distributed
manner terminals situated around the world accessed the central
database system through phone lines and other data networks.
4. Colleges : For student information, course registrations, and grades.
5. Credit card transactions: For purchases on credit cards and generation
of monthly statements.
6. Telecommunication: For keeping records of calls made, generating
monthly bills, maintaining balances on prepaid calling cards, and storing
information about the communication networks.
7. Finance: For storing information about holdings, sales, and purchases
of financial instruments such as stocks and bonds.
8. Sales: For customer, product, and purchase information.
9. Manufacturing: For management of supply chain and for tracking
production of items in factories, inventories of items in warehouses/
stores, and orders for items.
10.Human resources: For information about employees recruitment, salaries,
payroll taxes and benefits, and for generation of paychecks.

13.3 Origin of data

The fact is something that is really occurred or is actually the case. The
things that are happening and happened in real form or virtual form are
considered to be fact. The fact is pursued by sense organs.

Data: Data is a collection of facts, figures, statistics, which can be


processed to produce meaningful information.

The process of converting fact to data will be the first task, for any person
in database concepts. The human intervention is mandatory for converting from
fact to data form. The data may be in the form of letters, numbers, symbols,
images, sound, video etc. The origin of fact can be from the organization/within
or outside organization or any part of universe. For example the marks obtained
by the student in the exam is 80, 80 is the fact, on the marks card the 80
entered will be in numberic symbols which is data.
Data Concepts 285

The different forms of data and its representation is illustrated. One such form
is sound represented using musical notes (software generated), these notes is
stored in the form of bytes in the sound file(software digitized). In the
hardware data is stored in the form of bits.

Fig. 13.1 Different forms of data


Information: Information is processed data with some definite mean-
ing. Information represents facts, figures, or statistics, which have proper
meaning.

For example, in the marks card of the student total marks, percentage
and the result are processed data known as the information.

13.4 Evolution of Database - Manual File systems


Historically, When the file management came into existence, such systems
were often manual, paper-and-pencil systems. The papers within these systems
were organized to facilitate the expected use of the data. Typically, this was
accomplished through a system of file folders and filing cabinets. As long as a
collection of data was relatively small and an organization’s users had few reporting
requirements, the manual system served its role well as a data repository.
However, as organizations grew and as reporting requirements became more
complex, keeping track of data in a manual file system became more difficult.
Therefore, companies looked to computer technology for help.
286 Data Concepts

Computerized File systems

Initially, the computer files within the file system were similar to the manual
files. The description of computer files requires a specialized vocabulary.
Every discipline develops its own terminology to enable its practitioners to
communicate clearly.
Differences between Manual and Computerized data processing

Manual Data processing Computerized Electronic Data


processing
The Volume of the data, which can be The volume of data which can be
processed, is limited in a desirable time. processed can be very large.
Manual data processing requires large Reasonable less amount of paper is
quantity of paper used.
The speed and accuracy at which the job The job executed is faster and
is executed is limited. Accurate.
Labour cost is high. Labour cost is economical.
Storage medium is paper Storage medium is Secondary
storage medium.

13.5 DATA PROCESSING CYCLE

The way information is processed in a computer information management system.


The information processing cycle consists of five specific steps:

1. Data input– This is any kind of data- letters, numbers, symbols, shapes,
images or whatever raw material put into the computer system that needs
processing. Input data is put into the computer using a keyboard, mouse or
other devices such as the scanner, microphone and the digital camera. In general
data must be converted to computer understandable form (English to machine
code by the input devices).

2. Data processing – The processing is a series of actions or operations from the


input data to generate outputs. some of the operations are classification based
on some condition, calculation, sorting, indexing, accessing data, extracting
part of filed/attribute, substring etc., convertion of data into information by the
central processing unit. For example; when the computer adds 4+4=8 that is an
act of processing.

3. Storage - Data and information not currently being used must be stored so it
can be accessed later. There are two types of storage; primary and secondary
storage. Primary storage is the computer circuitry that temporarily holds data
Data Concepts 287

waiting to be processed (RAM) and it is inside the computer. Secondary storage


is where data is held permanently. A floppy disk, hard disk or CD- ROM is
examples of this kind of
DATAPROCESSINGCYCLE storage.

4 . O u t p u t – T h e
result(information)
obtained after processing
the data must be
presented to the user in
user understandable form.
The result may be in form
of reports(hardcopy/
softcopy). Some of the
output can be animated
Fig. 13.2 Data Processing cycle
with sound and video/
picture.

5. Communication – Computers nowadays have communication ability which


increases their power. With wired or wireless communication connections, data
may be input from afar, processed in a remote area and stored in several different
places and then be transmitted by modem as an e-mail or posted to the website
where the online services are rendered.
13.6 Database terms :

File : File is basic unit of storage in computer system. The file is the large
collection of related data.

Database: A Database is a collection of logically related data organized in a way


that data can be easily accessed, managed and updated.
Tables : In Relational database, a table is a collection of data elements orga-
nized in terms of rows and columns. A table is also considered as convenient
representation of relations. Table is the most simplest form of data storage.
Below is an example : Employee table
Emp_ID NAME AGE SALARY
1 RAJAPPA 43 45000.00
2 ALEX 37 56444.00
Table : Employee, 3 RAMESH 55 56000.00
Columns :Emp_Id,NAME,AGE,SALARY 4 IMRAN 28 60000.00
Rows :There are four rows. Fig 13.3 Table with rows and columns
288 Data Concepts

Fig 13.4 Different database terms

Fig 13.5 Single persons details


Records: A single entry in a table is called a Record or Row. A Record in a
table represents set of related data. Following is an example of single record.
Tuple :Records are also called the tuple.
Fields : Each Columns is identified by a distinct header called attribute or field
Domain :Set of values for an attribute in that column.
An Entity is an object such as a table or Form An Entity Relationship is how
each table link to each other
13.7 Data types of DBMS(Data types in DBMS)
1. Integer – Hold whole number without fractions.
2. Single and double precision – Seven significant value for a number.
3. Logical data type-Store data that has only two values true or false.
4. Characters – Include letter, number, spaces, symbols and punctuation.
Characters fields or variables store text information like name, address, but
size will be one byte.
5. Strings – Sequence of character more than one. Fixed length is 0 to 63Kb and
dynamic strings length range from 0 to 2 billion characters.
6. Memo data type – Store more than 255 characters. A memo fields can store
up to 65536 characters. Long documents can store OLE objects.
7. Index fields –Used to store relevant information along with the documents.
The document input to an index field is used to find those documents when
needed. The programs provides up to 25 user definable index fields in an index
set. Name drop-down look-up list, Standard, auto-complete History list.
8. Currency fields – The currency field accepts data in dollar form by default.
9. Date fields -The date fields accepts data entered in date format.
10. Text fields – Accepts data as an alpha-numeric text string.
Data Concepts 289

13.8 DBMS-DATABASE MANAGEMENT SYSTEM

A DBMS is a software that allows creation,


definition and manipulation of database. DBMS is
actually a tool used to perform any kind of
operation on data in database. DBMS also provides
protection and security to database. It maintains
data consistency in case of multiple users. Here
are some examples of popular DBMS, MySql,
Oracle, Sybase, Microsoft Access and IBM DB2 etc.

The primary goal of a DBMS is to provide a way to store and retrieve database
information that is both convenient and efficient.

Features of Database System


Database approach came into existence due to the overcome of the drawbacks of
file processing system. In the database approach, the data is stored at a central
location and is shared among multiple users. Thus, the main advantage of DBMS
is centralized data management. The
centralized nature of database system
provides several advantages, which overcome
the limitations of the conventional file
processing system. These advantages are
listed here.
 Controlled data redundancy: During
database design, various files are integrated
and each logical data item is stored at central
location. This eliminates
replicating(duplication) the data item in
Fig 13.6 Features of DBMS different files, and ensures consistency and
saves the storage space. Note that the redundancy in the database systems
cannot be eliminated completely as there could be some performance and
technical reasons for having some amount of redundancy. However, the DBMS
should be capable of controlling this redundancy in order to avoid data
inconsistencies.
 Enforcing data integrity: In database approach, enforcing data integrity is
much easier. Data integrity refers to the validity of data and it can be compromised
290 Data Concepts

in a number of ways. Data integrity constraints can be enforced automatically


by the DBMS, and others may have to be checked by the application programs.
 Data sharing: The data stored in the database can be shared among multiple
users or application programs. Moreover, new applications can be developed to
use the same stored data. Due to shared data, it is possible to satisfy the data
requirements of the new applications without having to create any additional
data or with minimal modification.
 Ease of application development: The application programmer needs to
develop the application programs according to the users’ needs. The other issues
like concurrent access, security, data integrity, etc., are handled by the RDBMS
itself. This makes the application development an easier task.
 Data security: Since the data is stored centrally, enforcing security constraints
is much easier. The RDBMS ensures that the only means of access to the database
is through an authorized channel only. Hence, data security checks can be
carried out whenever access is attempted to sensitive data. To ensure security, a
RDBMS provides security tools such as user codes and passwords. Different
checks can be established for each type of access (addition, modification, deletion,
etc.) to each piece of information in the database.
 Multiple user interfaces: In order to meet the needs of various users having
different technical knowledge, DBMS provides different types of interfaces such
as query languages, application program interfaces, and graphical user interfaces
(GUI) that include forms-style and menu-driven interfaces. A form-style interface
displays a form to each user and user
interacts using these forms. In menu-
driven interface, the user interaction
is through lists of options known as
menus.
 Backup and recovery: The
RDBMS provides backup and
recovery subsystem that is responsible
for recovery from hardware and
software failures. For example, if the
Fig 13.7 Different Layers Database failure occurs in between the
transaction, the RDBMS recovery subsystem either reverts back the database to
the state which existed prior to the start of the transaction or resumes the
transaction from the point it was interrupted so that its complete effect can be
recorded in the database.
Data Concepts 291

Note : Why a Spreadsheet Is Not a Database


While a spreadsheet allows for the creation of multiple tables, it does not support
even the most basic data-base functionality such as support for
self-documentation through metadata, enforcement of data types or domains to
ensure consistency of data within a column, defined relationships among tables,
or constraints to ensure consistency of data across related tables. Most users
lack the necessary training to recognize the limitations of spreadsheets for these
types of tasks.

13.9 Data Abstraction


The DBMS architecture describes how data in the database is viewed by the
users. It is not concerned with how the data is handled and processed by the
RDBMS. The database users are provided with an abstract view of the data by
hiding certain details of how data is physically stored. This enables the users
to manipulate the data without worrying about where it is located or how it is
actually stored.
In this architecture, the overall database description can be defined at three
levels, namely, internal, conceptual, and external levels and thus, named three-
level RDBMS architecture. This architecture is proposed by ANSI/SPARC
(American National Standards Institute/Standards Planning and Requirements
Committee) and hence, is also known as ANSI/SPARC architecture.
· Internal level: Internal level is
the lowest level of data
abstraction that deals with the
physical representation of the
database on the computer and
thus, is also known as physical
level. It describes how the data
is physically stored and
organized on the storage
medium. At this level, various
aspects are considered to
achieve optimal runtime
performance and storage space
utilization. These aspects include
storage space allocation
techniques for data and indexes,
access paths such as indexes,

Fig 13.8 Different Levels of Database


292 Data Concepts
data compression and encryption techniques, and record placement.
Conceptual level: This level of abstraction deals with the logical structure of the entire database
and thus, is also known as logical level. Conceptual level describes what data is stored in the
database, the relationships among the data and complete view of the user’s requirements without
any concern for the physical implementation. That is, it hides the complexity of physical storage
structures.
The conceptual view is the overall view of the database and it includes all the
information that is going to be represented in the database.
External level: External level is the highest level of abstraction that deals with
the user’s view of the database and thus, is also known as view level. In general,
most of the users and application programs do not require the entire data stored
in the database. The external level describes a part of the database for a particular
group of users. It permits users to access data in a way that is customized according
to their needs, so that the same data can be seen by different users in different
ways, at the same time. In this way, it provides a powerful and flexible security
mechanism by hiding the parts of the database from certain users, as the user is
not aware of existence of any attributes that are missing from the view.
DBMS users : The broad classification of dbms users are
1. Application programmers and system analysts: System analsysts determine
the requirement of end users; especially naive, parametric end users, and develop
specifications for transactions that meet these requirements.Application
programmers implement these parameters in programs.
2. End users : People who require access to the database for querying updating
and generating reports. The database exists primarily for/their use.
3. Database Administrator (DBA): DBA is responsible for authorization access to
the database for coordinating and
monitoring its use, and for
acquiring the needed software
and hardware resources.
4. Database designers: Database
designers are responsible for
identifying the data to be stored
in the database for choosing
appropriate structures to
represent and store the data.
Schema objects are database
objects that contain data or govern or perform operations on data.
Data Concepts 293

13.10 Data Independence:


Data Independence is an ability of a database to modify a schema definition at
one level without affecting a schema in the next higher level (the ability to change
the conceptual schema without affecting the external schemas or application
programs). Data independence occurs because when the schema is changed at
one level, the schema at next level remains unchanged and only the mapping
between the two levels is changed. Two types of data independence are:
1. Physical Data Independence. 2. Logical Data Independence

Fig 13.9 Data Independence


Physical Data independence
All schemas are logical and actual data is stored in bit format on the disk. Namely
storage medium: Hard disk (all the files will be stored), floppies, drum, tapes, SD
etc., System designs choose to organize, access and process records and files in
different ways depending on the type of application and the needs of users. The
three commonly used file organizations used in dbms/rdbms data processing
applications are sequential, direct and indexed sequential access method(ISAM).
The selection of a particular file organization depends upon the application used.
To access a record some key field or unique identifying value that is found in
every record in a file is used.

Serial File Organization : With serial file organization, records are arranged
one after another, in no particular order-other than, the chronological order in
which records are added to the file. Serial organization is commonly found in
the transaction data. Where records are created in a file in the order in which
transaction takes place. Serial file organization provides advantages like fast
294 Data Concepts

access to next records in sequence, stored in economical storage media and


easy to do the file backup facility, updating is slowly in this file organization.

Sequential File organization : Records are stored one after another in an


ascending or descending order determined by the key field of the records.
Example payroll file, records are stored in the form of employee id. Sequentially
organized files that are processed by computer systems are normally stored on
storage media such as magnetic tape, punched cards, or magnetic disks. To
access these records the computer must read the file in sequence from the
beginning. The first record is read and processed first, then the second record in
the file sequence, and so on. To locate a particular record, the computer program
must read in each record in sequence and compare its key field to the one that
is needed. The retrieval search ends only when the desired key matches with
the key field of the currently read record. On an average, about half of the file
has to be searched to retrieve the desired record from a sequential file.

Fig 13.10 File organisation


Data Concepts 295

Random/Direct Access File Organization: Direct access file organization allow


immediate direct access to individual records on the file. The record are stored
and retrieved using a relative record number, which gives the position of the
record in the file. This type of organization also allows the file to accessed
sequentially. The primary storage in a CPU truly provides for direct access.
There are some devices outside the CPU which can provide the direct feature;the
direct access storage devices have the capability of directly reaching any location.
Although there are several types of storage devices including discs and other
mass storage.
Self(direct) Addressing: Under self-direct addressing, a record key is used as
its relative address. Therefore, anyone can compute the record’s address from
the record key and the physical address of the first record in the file.
Advantage is self-addressing no need to store an index.
Disadvantages are, the records must be of fixed length, if some records are
deleted the space remains empty.
Random access method : Records are stored on disk by using a hasing algorithm.
The key field is fed through hashing algorithm and a relative address is created.
This address gives the position on the disk where the record is to be stored. The
desired records can be directly accessed using randomizing procedure or hashing
without accessing all other records in the file. Randomizing procedure is
characterized by the fact that records are stored in such a way that there is no
relationship between the keys of the adjacent records. The technique provide for
converting the records key number to a physical location represented by a disk
address through a computational procedure.
Advantages : The access to, and retrieval of a records is quick and direct.
Transactions need not be stored and placed in sequence prior to processing
Best used for online transaction.
Disadvantages: Address generation overhead is involved for accessing each record
due to hashing function.
May be less efficient in the use of storage space than sequentially organized
files.
Indexed Sequential Access Method(ISAM): ISAM is the hybrid between
sequential and direct access file organization. The records within the file are
stored sequentially but direct access to individual records is possible through an
index. Indexing permit access to selected records without searching the entire
file.

Advantages: ISAM permits efficient and economical use of sequential processing


techniques when the activity ratio is high.
Permits direct access processing of records in a relatively efficient way when the
activity ratio is low.
296 Data Concepts

Cylinder 1 track index Cylinder 2 track index


Cylinder Highest
Track Highest Track Highest
Record key
Record key Record key
in the cylinder
in the cylinder in the cylinder
1 84
1 84 1 95
2 250
2 250 2 110
3 398
3 398 3 175
4 479
4 479 4 250

Fig 13.11 Table describing ISAM

Disadvantages: Files must be stored in a direct-access storage device. Hence


relatively expensive hardware and software resources are required.
Access to records may be slower than direct file.
Less efficient in the use of storage space than some other alternatives.

Different types of Architecture

The design of a Database Management System highly depends on its architecture.


It can be centralized or decentralized or hierarchical. DBMS architecture can be
seen as single tier or multi-tier. N-tier architecture divides the whole system
into related but independent n modules, which can be independently modified,
altered, changed or replaced.
Database architecture is logically divided into three types.
1. Logical one-tier In 1-tier architecture,
2. Logical two-tier Client / Server architecture
3. Logical three-tier Client/Server architecture

Logical one-tier In 1-tier architecture


In 1-tier architecture, DBMS is the only entity where user directly sits on DBMS
and uses it. Any changes done
here will directly be done on
DBMS itself. It does not provide
handy tools for end users and
preferably database designer
and programmers use single
tier architecture.
Fig 13.12 Logical one tier
architecture
Data Concepts 297

Logical two-tier Client / Server architecture

Two-tier Client / Server Architecture

Two-tier Client / Server architecture is used for User Interface program and
Application Programs that runs on client side. An interface called ODBC (Open
Database Connectivity) provides an API that allows client side program to call
the DBMS. Most DBMS vendors provide ODBC drivers. A client program may
connect to several DBMS's. In this architecture some variation of client is also
possible for example in some DBMS's more functionality is transferred to the
client including data dictionary, optimization etc. Such clients are called Data
server.

Fig 13.13 2-Level Tier Architecture


298 Data Concepts

Three-tier Client / Server Architecture


Three-tier Client / Server database architecture is commonly used architecture
for web applications. Intermediate layer called Application server or Web Server
stores the web connectivity software and the business logic (constraints) part of
application used to access the right amount of data from the database server.
This layer acts like medium for sending partially processed data between the
database server and the client.

Fig 13.14 3-Level Tier Architecture


 Database (Data) Tier: At this tier, only database resides. Database along with
its query processing languages sits in layer-3 of 3-tier architecture. It also
contains all relations and their constraints.
 Application (Middle) Tier: At this tier the application server and program,
which access database, resides. For a user this application tier works as
abstracted view of database. Users are unaware of any existence of database
beyond application. For database-tier, application tier is the user of it.
Database tier is not aware of any other user beyond application tier. This tier
works as mediator between the two.
 User (Presentation) Tier: An end user sits on this tier. From a users aspect
this tier is everything. He/she doesn't know about any existence or form of
database beyond this layer. At this layer multiple views of database can be
provided by the application. All views are generated by applications, which
reside in application tier. Multiple tier database architecture is highly
modifiable as almost all its components are independent and can be changed
independently.

13.11 Database Model

A database model or simply a data model is an abstract model that describes


how the data is represented and used. A data model consists of a set of data
Data Concepts 299

structures and conceptual tools that is used to describe the structure (data
types, relationships, and constraints) of a database.

A data model not only describes the structure of the data, it also defines a set
of operations that can be performed on the data. A data model generally
consists of data model theory, which is a formal description of how data may
be structured and used, and data model instance, which is a practical data
model designed for a particular application. The process of applying a data
model theory to create a data model instance is known as data modeling.

The main objective of database system is to highlight only the essential


features and to hide the storage and data organization details from the user.
This is known as data abstraction. A database model provides the necessary
means to achieve data abstraction.

A Database model defines the logical design of data. The model describes the
relationships between different parts of the data.
In history of database design, three models have been in use.
* Hierarchical Model
* Network Model
* Relational Model
13.11.1 Hierarchical Model

The hierarchical data model is the oldest type of data model, developed by IBM
in 1968. This data model organizes the data in a tree-like structure, in which
each child node (also known as dependents) can have only one parent node.
The database based on the hierarchical data model comprises a set of records
connected to one another through links. The link is an association between two
or more records. The top of the tree structure consists of a single node that does
not have any parent and is called the root node.

The root may have any number of dependents; each of these dependents may
have any number of lower level dependents. Each child node can have only one
parent node and a parent node can have any number of (many) child nodes. It,
therefore, represents only one-to-one and one-to-many relationships. The
collection of same type of records is known as a record type.

For simplicity, only few fields of each record type are shown. One complete
record of each record type represents a node.
300 Data Concepts

In this model each entity has only one parent but can have several children . At
the top of hierarchy there is only one entity which is called Root.
Hierarchical Model Example

Fig 13.15 Hierarchical Model

Advantage Dis-advantage

The hierarchical data model is that The main drawback of this model is
the data access is quite predictable that the links are ‘hard coded’ into
in the structure and, therefore, both the data structure, that is, the link is
the retrieval and updates can be permanently established and cannot
highly optimized by the DBMS. be modified. The hard coding makes
the hierarchical model rigid. In
addition, the physical links make it
difficult to expand or modify the
database and the changes require
substantial redesigning efforts.

13.11.2 Network Model

The first specification of network data model was presented by Conference on


Data Systems Languages (CODASYL) in 1969, followed by the second specification
in 1971. It is powerful but complicated. In a network model the data is also
represented by a collection of records, and relationships among data are
represented by links. However, the link in a network data model represents an
association between precisely two records. Like hierarchical data model, each
record of a particular record type represents a node. However, unlike hierarchical
data model, all the nodes are linked to each other without any hierarchy. The
main difference between hierarchical and network data model is that in
Data Concepts 301

hierarchical data model, the data is organized in the form of trees and in network
data model, the data is organized in the form of graphs.
In the network model, entities are organized in a graph, in which some
entities can be accessed through several path

Fig 13.16 Network Model of database


Network Model of database

Advantage Dis-advantage

The network data model is that a The network data model is that it
parent node can have many child can be quite complicated to maintain
nodes and a child can also have all the links and a single broken link
many parent nodes. Thus, the can lead to problems in the
network model permits the modeling database. In addition, since there are
of many-to-many relationships in no restrictions on the number of
data. relationships, the database design
can become complex.

13.11.3 Relational Model


The relational data model was developed by E. F. Codd in 1970. In the relational
data model, unlike the hierarchical and network models, there are no physical
links. All data is maintained in the form of tables (generally, known as relations)
consisting of rows and columns. Each row (record) represents an entity and a
column (field) represents an attribute of the entity. The relationship between
the two tables is implemented through a common attribute in the tables and not
by physical links or pointers. This makes the querying much easier in a relational
database system than in the hierarchical or network database systems. Thus,
302 Data Concepts

the relational model has become more programmer friendly and much more
dominant and popular in both industrial and academic scenarios. Oracle, Sybase,
DB2, Ingres, Informix, MS-SQL Server are few of the popular relational DBMSs.

In this model, data is organized in two-dimensional tables called relations. The


tables or relation are related to each other.
Relational Model of database

Fig 13.17 Relational Model of database


Basic Rules for the Realational Datamodel
13.12 Codd’s Rule
E.F Codd was a Computer Scientist who invented Relational model for Database
management. Based on relational model, Relation database was created. Codd
proposed 13 rules popularly known as Codd’s 12 rules to test DBMS’s concept
against his relational model. Codd’s rule actually define what quality a DBMS
requires in order to become a Relational Database Management System(RDBMS).
Till now, there is hardly any commercial product that follows all the 13 Codd’s
rules. Even Oracle follows only eight and half out(8.5) of 13. The Codd’s 12 rules
are as follows.
Rule zero
This rule states that for a system to qualify as an RDBMS, it must be
able to manage database entirely through the relational capabilities.
Rule 1 : Information rule
All information(including meta-deta) is to be represented as stored data
in cells of tables. The rows and columns have to be strictly unordered.
Rule 2 : Guaranteed Access
Each unique piece of data(atomic value) should be accessible by :
Table Name + primary key(Row) + Attribute(column).
NOTE : Ability to directly access via POINTER is a violation of this rule.
Rule 3 : Systemetic treatment of NULL
Data Concepts 303

Rule 3 : Systemetic treatment of NULL


Null has several meanings, it can mean missing data, not applicable or
no value. It should be handled consistently. Primary key must not be
null. Expression on NULL must give null.
Rule 4 : Active Online Catalog
Database dictionary(catalog) must have description of Database.
Catalog to be governed by same rule as rest of the database. The same
query language to be used on catalog as on application database.
Rule 5 : Powerful language
One well defined language must be there to provide all manners of access
to data. Example: SQL. If a file supporting table can be accessed by any
manner except SQL interface, then its a violation to this rule.
Rule 6 : View Updation rule
All view that are theoretically updatable should be updatable by the system.
Rule 7 : Relational Level Operation
There must be Insert, Delete, Update operations at each level of relations. Set
operation like Union, Intersection and minus should also be supported.
Rule 8 : Physical Data Independence
The physical storage of data should not matter to the system. If say, some file
supporting table were renamed or moved from one disk to another, it should
not effect the application.
Rule 9 : Logical Data Independence
If there is change in the logical structure(table structures) of the database the
user view of data should not change. Say, if a table is split into two tables, a
new view should give result as the join of the two tables. This rule is most
difficult to satisfy.
Rule 10 : Integrity Independence
The database should be able to con-force its own integrity rather than using
other programs. Key and Check constraints, trigger etc should be stored in
Data Dictionary. This also make *RDBMS* independent of front-end.
Rule 11 : Distribution Independence
A database should work properly regardless of its distribution across a
network. This lays foundation of distributed database.
Rule 12 : Non-subversion rule
If low level access is allowed to a system it should not be able to subvert or
bypass integrity rule to change data. This can be achieved by some sort of
looking or encryption.
304 Data Concepts

13.13 Logical database concepts : Normalization, Entities, attributes, relations


13.13.1 Normalization Rule: Normalization is the process of organizing data in
a database. This includes creating tables and establishing relationships between
those tables according to rules designed both to protect the data and to make
the database more flexible by eliminating redundancy and inconsistent
dependency.
There are a few rules for database normalization. Each rule is called a “normal
form.” If the first rule is observed, the database is said to be in “first normal
form.” If the first three rules are observed, the database is considered to be in
“third normal form.” Although other levels of normalization are possible, third
normal form is considered the highest level necessary for most applications.
As with many formal rules and specifications, real world scenarios do not always
allow for perfect compliance. In general, normalization requires additional tables
and some customers find this cumbersome. If you decide to violate one of the
first three rules of normalization, make sure that your application anticipates
any problems that could occur, such as redundant data and inconsistent
dependencies.
Normalization rule are divided into following normal form.
1. First Normal Form
2. Second Normal Form
3. Third Normal Form
4. BCNF
First Normal Form (1NF)
A row of data cannot contain repeating group of data i.e each column must have
a unique value. Each row of data must have a unique identifier i.e *Primary key*.
For example consider a table which is not in First normal form
Student Table :

You can clearly see here that student name Daryl is used twice in the table and
subject *maths* is also repeated. This violates the *First Normal form*. To reduce
above table to *First Normal form* break the table into two different tables
Data Concepts 305

*New Student Table :*


S _id S _N am e S _S u b ject
401 D aryl M ath s
402 R am esh P h ysics
403 R ak sh an a M ath s
404 N ak sh atria C om p u ter S cien ce
*Subject Table :*
Fig 13.18 1NF Student table with S-id,S_name,S_subject

Suject_id S_id S_Subject


35 401 Maths
35 401 Maths
33 403 Physics
34 404 Chemistry
41 405 Computer Science

In Student table concatenation of subject_id and student_id is the Primary key.


Now both the Student table and Subject table are normalized to first normal
form

Second Normal Form (2NF)

A table to be normalized to Second Normal Form should meet all the needs of
First Normal Form and there must not be any partial dependency of any column
on primary key. It means that for a table that has concatenated primary key,
each column in the table that is not part of the primary key must depend upon
the entire concatenated key for its existence. If any column depends only on
one part of the concatenated key, then the table fails Second normal form. For
example, consider a table which is not in Second normal form.

Library Table : Fig 13.19 Student table with 1NF rule rewritten
Library_id S_Name Issue_id Issue_name Book_detail
101 RAMU 10 Rakesh C++
102 RAMU 11 Rakesh Java
103 Zama 12 Gopal MATHS
104 SATISH 13 Gopal MATHS

In Library table concatenation of Library_id and issue_id is the primary key.


This table is in First Normal form but not in Second Normal form because there
are partial dependencies of columns on primary key. S_Name is only dependent
on Library_id, Issue_name is dependent on Issue_id and there is no link between
Book_detail and S_name.
306 Data Concepts

To reduce Library table to Second Normal form break the table into
following three different tables.
Library_id S_Name
Library_id S_Name
101 RAMU
102 RAMU
103 Zama
104 SATISH

Issue_Detail Table :

Issue_id Issue_name
10 Rakesh
11 Rakesh
12 Gopal
13 Gopal

Book_Detail Table :

Library_id Issue_id Book_detail


101 10 C++
102 11 Java
103 12 MATHS
104 13 MATHS

Now all these three table comply with Second Normal form.
Library_id S_Name Issue_Detail Table :

Library_id S_Name Issue_id Issue_name


101 RAMU 10 Rakesh
102 RAMU 11 Rakesh
103 Zama 12 Gopal
104 SATISH 13 Gopal
Book_Detail Table :
Library_id Issue_id Book_detail
101 10 C++
102 11 Java
103 12 MATHS
104 13 MATHS

Fig 13.20 2NF Library Tablewith primary key and Issue key
Data Concepts 307

Now all these three table comply with Second Normal form.

Third Normal Form (3NF)

Third Normal form applies that every non-prime attribute of table must be
dependent on primary key. The transitive functional dependencyshould be
removed from the table. The table must be in Second Normal form.
For example, consider a table with following fields.

Student_Detail Table :

Student_id Student_name DOB Street city State Pin

In this table Student_id is Primary key, but street, city and state depends upon
pin. The dependency between pin and other fields is called transitive dependency.
Hence to apply 3NF, we need to move the street, city and state to new table, with
pin as primary key.

New Student_Detail Table :

Student_id Student_name DOB pin

Address Table :

pin Street city state


The advantage of removing transitive dependency is,

* Amount of data duplication is reduced.


* Data integrity achieved.

Boyce and Codd Normal Form (BCNF)


BCNF is a higher version of the Third Normal form. This form deals with certain
type of anamoly that is not handled by 3NF. A 3NF table which does not have
multiple overlapping candidate keys is said to be in BCNF.
 When a relation has more than one candidate key, anomalies may result
even though the relation is in 3NF.

 3NF does not deal satisfactorily with the case of a relation with overlapping
candidate keys

 i.e. composite candidate keys with at least one attribute in common.


308 Data Concepts

 BCNF is based on the concept of a determinant.

 A determinant is any attribute (simple or composite) on which some other


attribute is fully functionally dependent.

 A relation is in BCNF is, and only if, every determinant is a candidate key.

Consider the following relation and determinants.

R(a,b,c,d)

a,c -> b,d

a,d -> b

Here, the first determinant suggests that the primary key of R could be changed
from a,b to a,c. If this change was done all of the non-key attributes present in
R could still be determined, and therefore this change is legal. However, the
second determinant indicates that a,d determines b, but a,d could not be the
key of R as a,d does not determine all of the non key attributes of R (it does not
determine c). We would say that the first determinate is a candidate key, but the
second determinant is not a candidate key, and thus this relation is not in
BCNF (but is in 3rd normal form).
„A relation R(X) is in Boyce–Codd Normal Form if for every non-trivial functional
dependency Y->Z defined on it, Y contains a key K of R(X). That is, Y is a
superkey for R(X).

13.13.2 Entity-Relationship Diagram


ER-Diagram is a visual representation of data that describes how data is
related to each other.

Fig 13.21 Entity-Relationship Symbols


Data Concepts 309

Components of E-R Diagram

The E-R diagram has three main components.

1) Entity

An Entity can be any object, place, person or class. In E-R Diagram, an entity is
represented using rectangles. Consider an example of an Organization.
Employee, Manager, Department, Product and many more can

be taken as entities from an Organization.


Entity In E-R Diagram

Fig 13.22 Entity and example


Weak entity is an entity that depends on
another entity. Weak entity doen’t have key
attribute of their own. Double rectangle
represents weak entity.
Fig 13.23 Entity with loan and payment

2) Attribute

An Attribute describes a property or characteristic of an entity. For


example, Name, Age, Address etc. can be attributes of a Student. An attribute is
represented using eclipse.
Attribute
A ttrib u te A ttrib u te ex a m p le

Fig 13.24 Attribute with example


310 Data Concepts

Key Attribute Key attribute


represents the main characteristic
of an Entity. It is used to represent
Primary key. Ellipse with
underlying lines represent Key
Attribute.
Fig 13.25 Attribute key with primary key

Composite Attribute : An
attribute can also have their own
attributes. These attributes are
known as Composite attribute.

composite attribute example

Fig 13.26 Composite attribute

3) Relationship
A Relationship describes relations between entities. Relationship is
represented using diamonds.
Relationship Relationship example

Fig 13.27 Relationship with example

There are three types of relationship that exist between Entities.


* Binary Relationship
* Recursive Relationship
* Ternary Relationship
Data Concepts 311

Binary Relationship

Binary Relationship means relation between two Entities. This is further


divided into three types.
1. One to One : This type of relationship is rarely seen in real world.

one-to-one example

Fig 13.28 1:1 Relationship with example


The above example describes that one student can enroll only for one course
and a course will also have only one Student. This is not what you will usually
see in relationship.

2. One to Many : It reflects business rule that one entity is associated with
many number of same entity. For example, Student enrolls for only one Course
but a Course can have many Students.

One to Many

Fig 13.29 1:M Relationship with example


312 Data Concepts

The arrows in the diagram describes that one student can enroll
for only one course.

3. Many to Many : The above diagram represents that many students can
enroll for more than one courses.

Many to Many

Fig 13.30 M:M Relationship with example


PARTICIPATION CONSTRAINTS

 Total Participation: Each entity in the entity is involved in the relationship.


Total participation is represented by double lines.
 Partial participation: Not all entities are involved in the relation ship.
Partial participation is represented by single line.

Fig 13.31 Total partipation and partial participation


Data Concepts 313

13.13.3 Cardinality

Cardinality specifies how many instances of an entity


relate to one instance of another entity.

Ordinality is also closely linked to cardinality. While


cardinality specifies the occurrences of a relationship,
ordinality describes the relationship as either
mandatory or optional. In other words, cardinality
specifies the maximum number of relationships and
ordinality specifies the absolute minimum number of
relationships.
Fig 13.32 Cardinality

Fig 13.33 Cardinality Notation with examples


Cardinality NotationCardinality specifies how many instances of an entity relate
to one instance of another entity.Ordinality is also closely linked to cardinality.
While cardinality specifies the occurances of a relationship, ordinality describes
the relationship as either mandatory or optional. In other words, cardinality
specifies the maximum number of relationships and ordinality specifies the
absolute minimum number of relationships. When the minimum number is
zero, the relationship is usually called optional and when the minimum number
is one or more, the relationship is usually called mandatory.
314 Data Concepts

Fig 13.34 Cardinality Notation


Recursive Relationship

Fig 13.35 Recursive Relationship


Data Concepts 315

Generalization Specialization

Generalization is a bottom-up *Specialization* is opposite to


approach in which two lower level Generalization. It is a top-down
entities combine to form a higher approach in which one higher level
level entity. In generalization, the entity can be broken down into two
higher level entity can also combine lower level entity. In specialization,
with other lower level entity to make some higher level entities may
further higher level entity. not have lower-level entity sets at all.

Fig13.36 Generalization & Specialization

When an Entity is related with itself it is known


as Recursive Relationship.

Fig13.37 Aggregration
Aggregration : Aggregration is a process when
relation between two entity is treated as a single
entity. Here the relation between Center and Course, is acting as an Entity in
relation with Visitor.

13.14 Keys
The word “key” is used in the context of relational database design. They are
used to establish and identify relation between tables. The key is a set of one or
more columns whose combined values are unique among all occurrences in a
given table.
316 Data Concepts

Fig 13.38 Database terms with example

There are various types of relational keys:


1. Candidate key(ck): A Candidate key is any set of one or more columns
whose combined values are unique among all occurrences(ie tuples or
rows). Since a null value is not guaranteed to be unique, no component of
a candidate key is allowed to be null.
2. Primary key(pk): Primary key is a
candidate key that is most appropriate to
become the main key of the table. Primary
key is a key that uniquely identify each
record in a table. Example student_id,
Employee_id, Bank_acoount_no,
Transfer_certificate_id, Driving_licence_no
etc., by which only one value can exist, no dupilcation can exist.
Data Concepts 317

3. Alternate key/Secondary key(sk): The alternate keys of any table are simply
those candidate keys which are not currently selected as the primary key.
An alternative key is a function of all candidate keys minus the primary
key.
5. Super Key : A superkey is basically all sets of columns for which no two
rows share the same values for those sets. An attribute or set of attributes
that uniquely identifies a tuple within a relation/table. Super Key is a
superset of Candidate key.
6. Foreign key(fk) :A foreign key is a field in a relational table that matches
the primary key column of another table. The foreign key can be used to
cross-reference tables.

Table Employees
Employee_id Name Age city Salary Car_loan_id
1 Rajappa 42 Tumkur 42000 585

Table BMWcars
Car_load_id Model Loanamount EMI Noof EMI Balance
585 Basic 1800000 80000 225 1000000

Fig 13.39 Foreign key with example

1.Composite Key : Key that consists of two or more attributes that uniquely
identify an entity occurrence is called
Composite key. But any attribute
that makes up the Composite key is
not a simple key in its own.
Example: Consider a Relation or
Table R1. Let A,B,C,D,E are the
attributes of this relation.

Fig.14.47 Composite Key


318 Data Concepts

R(A,B,C,D,E)
A?BCDE This means the attribute 'A' uniquely determines the other attributes
B,C,D,E.
BC?ADE This means the attributes 'BC' jointly determines all the other attributes
A,D,E in the relation.
Primary Key :A
Candidate Keys :A, BC
Super Keys : A,BC,ABC,AD
Note : ABC,AD are not Candidate Keys since both are not minimal super keys.

13.15 Relational Algebra


In order to implement a DBMS, there must exist a set of rules which state how
the database system will behave. For instance, somewhere in the DBMS must
be a set of statements which indicate than when someone inserts data into a
row of a relation, it has the effect which the user expects. One way to specify this
is to use words to write an ‘essay’ as to how the DBMS will operate, but words
tend to be imprecise and open to interpretation. Instead, relational databases
are more usually defined using Relational Algebra.

Relational Algebra is :
 the formal description of how a relational database operates
 an interface to the data stored in the database itself
 the mathematics which underpin SQL operations
Operators in relational algebra are not necessarily the same as SQL operators,
even if they have the same name. For example, the SELECT statement exists in
SQL, and also exists in relational algebra. These two uses of SELECT are not the
same. The DBMS must take whatever SQL statements the user types in and
translate them into relational algebra operations before applying them to the
database.
Terminology
 Relation – a set of tuples.
 Tuple – a collection of attributes which describe some real world entity.
 Attribute – a real world role played by a named domain.
 Domain – a set of atomic values.
 Set – a mathematical definition for a collection of objects which
contains no duplicates.
Data Concepts 319

Operators – Write
 INSERT – provides a list of attribute values for a new tuple in a relation.
This operator is the same as SQL.
 DELETE – provides a condition on the attributes of a relation to
determine which tuple(s) to remove from the relation. This operator is the
same as SQL.
 MODIFY – changes the values of one or more attributes in one or more
tuples of a relation, as identified by a condition operating on the attributes of
the relation. This is equivalent to SQL UPDATE.
Operators – Retrieval
There are two groups of operations:
 Mathematical set theory based relations:
UNION, INTERSECTION, DIFFERENCE, and CARTESIAN PRODUCT.

Fig.13.40 Relational Algebra

 Special database operations:


SELECT (not the same as SQL SELECT), PROJECT, and JOIN.
Relational SELECT
SELECT is used to obtain a subset of the tuples of a relation that satisfy
a select condition.
For example, find all employees born after 14th Feb 2014:
SELECTdob ‘14/feb/2014’(employee)
Relational PROJECT
The PROJECT operation is used to select a subset of the attributes of a relation by
specifying the names of the required attributes.
For example, to get a list of all employees surnames and employee numbers:
PROJECT
surname,empno
(employee)
320 Data Concepts

SELECT and PROJECT


SELECT and PROJECT can be combined together. For example, to get a list of
employee numbers for employees in department number 1:
PROJECT EMPNO
(SELECT DEPTNO
=1 (EMPLOYEE))

MAPPING THIS BACK TO SQL GIVEN:

SELECT EMPNO
FROM EMPLOYEE
WHERE DEPTNO=1;
Fig.13.41 Select and project
Figure : Mapping select and project
Set Operations – semantics
Consider two relations R and S.
 UNION of R and S

the union of two relations is a relation that includes all the tuples that are
either in R or in S or in both R and S. Duplicate tuples are eliminated.

 INTERSECTION of R and S

the intersection of R and S is a relation that includes all tuples that are
both in R and S.

 DIFFERENCE of R and S

the difference of R and S is the relation that contains all the tuples that are
in R but that are not in S.

SET Operations – requirements

For set operations to function correctly the relations R and S must be union
compatible. Two relations are union compatible if

 they have the same number of attributes

 the domain of each attribute in column order is the same in both R and
S.
Data Concepts 321

UNION Example

Fig.13.42 Union

INTERSECTION Example

Fig.13.43 Intersection

DIFFERENCE Example

Fig.13.44 Difference
322 Data Concepts

CARTESIAN PRODUCT
The Cartesian Product is also an operator which works on two sets. It is
sometimes called the CROSS PRODUCT or CROSS JOIN.
It combines the tuples of one relation with all the tuples of the other relation.
CARTESIAN PRODUCT example

Fig.13.45 Cartesian product


Figure : CARTESIAN PRODUCT
JOIN Operator
JOIN is used to combine related tuples from two relations:
 In its simplest form the JOIN operator is just the cross product of the
two relations.
 As the join becomes more complex, tuples are removed within the cross
product to make the result of the join more meaningful.
 JOIN allows you to evaluate a join condition between the attributes of
the relations on which the join is undertaken.
The notation used is
R JOINjoin condition S JOIN Example

Figure : JOIN Fig.13.46 Join


Data Concepts 323

Natural Join

Invariably the JOIN involves an equality test, and thus is often described as an
equi-join. Such joins result in two attributes in the resulting relation having
exactly the same value. A ‘natural join’ will remove the duplicate attribute(s).

 In most systems a natural join will require that the attributes have the
same name to identify the attribute(s) to be used in the join. This may
require a renaming mechanism.

 If you do use natural joins make sure that the relations do not have two
attributes with the same name by accident.

OUTER JOINs

Notice that much of the data is lost when applying a join to two relations. In
some cases this lost data might hold useful information. An outer join retains
the information that would have been lost from the tables, replacing missing
data with nulls.

There are three forms of the outer join, depending on which data is to be kept.

 LEFT OUTER JOIN – keep data from the left-hand table

 RIGHT OUTER JOIN – keep data from the right-hand table

 FULL OUTER JOIN – keep data from both tables

OUTER JOIN example 1

Fig.13.47 Outer Join left/right OUTER JOIN (left/right)


324 Data Concepts

JOIN example 2

Fig.13.48 Outer Join Full OUTER JOIN (full)


Consider the following SQL to find which departments have had employees on
the ‘Further Accounting’ course.
SELECT DISTINCT dname
FROM department, course, empcourse, employee
WHERE cname = ‘Further Accounting’
AND course.courseno = empcourse.courseno
AND empcourse.empno = employee.empno
AND employee.depno = department.depno;
The equivalent relational algebra is
PROJECTdname (department JOINdepno = depno (
PROJECTdepno (employee JOINempno = empno (
PROJECTempno (empcourse JOINcourseno = courseno (
PROJECTcourseno (SELECTcname = ‘Further Accounting’ course) )) )) ))
Symbolic Notation

From the example, one can see that for complicated cases a large amount of
the answer is formed from operator names, such as PROJECT and JOIN. It is
therefore commonplace to use symbolic notation to represent the operators.

 SELECT ->ó (sigma)

 PROJECT -> ð(pi)

 PRODUCT -> ×(times)

 JOIN -> |×| (bow-tie)


Data Concepts 325

 UNION -> *” (cup)

 INTERSECTION -> )”(cap)

 DIFFERENCE -> - (minus)

 RENAME ->ñ (rho)

Usage

The symbolic operators are used as with the verbal ones. So, to find all
employees in department 1:

SELECTdepno = 1(employee)
becomes ódepno = 1(employee)
Conditions can be combined together using ^ (AND) and v (OR). For example,
all employees in department 1 called ‘URS’:

SELECTdepno = 1 ^ surname = ‘URS’(employee)


becomes ódepno = 1 ^ surname = ‘URS’(employee)
The use of the symbolic notation can lend itself to brevity. Even better, when
the JOIN is a natural join, the JOIN condition may be omitted from |x|. The
earlier example resulted in:

PROJECTdname (department JOINdepno = depno (


PROJECTdepno (employee JOINempno = empno (
PROJECTempno (empcourse JOINcourseno = courseno (
PROJECTcourseno (SELECTcname = ‘Further Accounting’
course)))))))
becomes

ðdname (department |×| (


ðdepno (employee |×| (
ðempno (empcourse |×| (
ðcourseno (ócname = ‘Further Accounting’ course) ))))))
326 Data Concepts

Rename Operator

The rename operator returns an existing relation under a new name.


ñA(B) is the relation B with its name changed to A. For example, find the
employees in the same Department as employee 3.

Ñemp2.surname,emp2.forenames (
óemployee.empno = 3 ^ employee.depno = emp2.depno (
employee × (ñemp2employee) ) )
Derivable Operators

 Fundamental operators:ó, ð, ×, *”, -, ñ


 Derivable operators: |×|,)”
Equivalence

A|×|cB Ô! ða1,a2,…aN(óc(A × B))


 where c is the join condition (eg A.a1 = B.a1),

 and a1,a2,…aN are all the attributes of A and B without repetition.

C is called the join-condition, and is usually the comparison of primary and


foreign key. Where there are N tables, there are usually N-1 join-conditions. In
the case of a natural join, the conditions can be missed out, but otherwise
missing out conditions results in a ptimizat product (a common mistake to
make).

Equivalences

The same relational algebraic expression can be written in many different


ways. The order in which tuples appear in relations is never significant.

 A ×B Ô! B × A

 A )” B Ô! B )” A

 A *”B Ô! B *” A

 (A – B) is not the same as (B – A)


Data Concepts 327

 óc1 (óc2(A)) Ô! óc2 (óc1(A)) Ô! óc1 ^ c2(A)

 ða1(A) Ô! ða1(ða1,etc(A))

where etc represents any other attributes of A.

 many other equivalences exist.


While equivalent expressions always give the same result, some may be much
easier to evaluate that others.

When any query is submitted to the DBMS, its query ptimizat tries to find the
most efficient equivalent expression before evaluating it.

Comparing RA and SQL


Relational algebra: SQL:
 Is closed (the result of every  Is a superset of relational algebra
expression is a relation)  Has convenient formatting
 Has a rigorous foundation features, etc.
 Has simple semantics  Provides aggregate functions
 Is used for reasoning, query  Has complicated semantics
optimization, etc.  Is an end-user language

Any relational language as powerful SQL is a superset of relational


as relational algebra is called algebra, it is also relationally
relationally complete. complete.

A relationally complete language can


perform all basic, meaningful
operations on relations.

13.16 Data warehouse


A data ware house is a repository of an organization’s electronically stored data.
Data warehouse are designed to facilitate reporting and supporting data analysis.
The concept of data warehouses was introduced in late 1980’s. The concept was
introduced to meet the growing demands for management information and
analysis that could not be met by operational systems.

Separate computer databases began to be built that were specifically designed


to support management information and analysis purposes. These data warehouse
328 Data Concepts

were able to bring in data from a range of different data sources, such as,
mainframe computer, minicomputer, as well as personal computer and office
automation software such as spreadsheets and integrate this information in a
single place. This capability, coupled with user-friendly reporting tools, and
freedom from operational impacts has led to a growth of this type of computer
system.

Data ware house have evolved though several fundamental stages like:

Offline operational databases – Data warehouse in this initial stage are developed
by simply copying the database of an operational system to an off-line server
where the processing load of reporting does not impact on the operational
system’s performance.

Offline data warehouse – Database warehouses in this stages of evolution are


updated on regular time cycle(usually daily, weekly or monthly) form operational
systems and the data is stored in a integrated reporting-oriented data structure.

Real Time data warehouse – Data warehouses are updated on transaction or


event basis, event time an operational system performs a transaction.

Integrated data warehouses – Data warehouses used to generate activity or


transactions that are passed back into the operational systems for use in the
daily activity of the organization.

Components of data warehouses

Data Sources: Data sources refer to any electronic repository of information that
contains data of interest for management use or analytics. From mainframe(IBM
DB2,ISAM,Adabas, etc.), client-server databases (e.g Oracle database, Informix,
Microsoft SQL Server etc.,), PC databases (e.g Microsoft Access), and ESS and
other electronic store of data. Data needs to be passed from these to systems to
the data warehouse either on the transaction-by-transaction basis for real-time
data warehouses or on a regular cycle(e.g daily or weekly) for offline data
warehouses.

Data transformation: The data transformation layer receives data from the data
sources, cleaned and standardizes and loads it into the data repository. This is
often called “staging” data as data often passes through a temporary database
whilst it is being transformed. This activity of transformation data can be
performed either by manually created code or a specific type of software could
be used called an Extract, Transform and load(ETL) tool.
Data Concepts 329

Reporting : The data in the data warehouses must be available to the


organization’s staff if the data warehouses is to be useful. There are a very large
number of applications that perform this function or reporting can be custom-
developed. Some are Bussiness intelligence tools, Executive information systems,
Online Analytical processing(OLAP) Tools, Data Mining etc.,

Metadata: Metadata or “Data about data” is used to inform operators and uses of
the data warehouses about its status and the information held within the data
warehouses.

Operations : Data warehouses operations comprises of the processes of loading,


manipulating and extracting data from the data warehouse. Operations also
cover users management security, capacity management and related functions.

Optional components: In addition the following components also exist in some


data warehouses: 1. Dependent data marts. 2. Logical data marts. 3. Operational
data store.
Advantages of data ware houses:
1. Enhance end-user access to reports and analysis of information.
2. Increases data consistency.
3. Increases productivity and decreases computing costs.
4. Able to combine data from different sources, in one place.
5. Data warehouses provide an infrastructure that could support changes to
data and replication of the changed data back into the operational systems.
Disadvantages
Extracting, cleaning and loading data could be time consuming.
Data warehouses can get outdated relatively quickly.
Problems with compatibility with systems already in place.
Providing training to end-users.
Security could develop into a serious issue, especially if the data warehouses is
internet accessible.
A data warehouses is usually not static and maintenance costs are high.
13.17 Data Mining : Data mining is concerned with the analysis and picking
out relevant information. It is the computer, which is responsible for finding the
patterns by identifying the underling rules of the features in the data.

Data mining analysis tends to work form the data up and the best techniques
are those developed with an orientation towards large volumes of data, making
use of as much of the collected data as possible to arrive at reliable conclusions
and decisions.
330 Data Concepts

The analysis process starts with a set of data, uses a methodology to develop an
optimal representation to the structure of the data during which time knowledge
is acquired. Once knowledge has been acquired this can be extended to larger
sets of data working on the assumption that the larger data set has a structure
similar to the sample data. Again this is analogous to a mining operation where
large amounts of low grade materials are sifted through in order to find something
of value.
Some of the data mining software’s are SPSS, SAS, Think Analytics and G-Sat
etc.
The phases start with the raw data and finish with the extracted knowledge
which was acquired as a result of the following stages:
Selection- Selecting or segmenting the data according to some criteria e.g. all
those people who won a car, in this way subsets of the data can be determined.
Preprocessing – This is the data cleaning stage where certain information is
removed which deemed unnecessary and may slow down queries for e.g.: gender
of the patient. The data is reconfigured to ensure a consistent format as there is
a possibility of inconsistent formats because the data is drawn from several
sources e.g. gender may be recorded as F or M also as 1 or 0.
Transformation – The data is not merely transferred, but transformed. E.g.:
demographic overlays commonly used in market research. The data is made
useable and navigable.
Data mining- This stage is concerned with the extraction of patterns from the
data. A pattern can be defined as given a set of facts(data) F, a language L, and
some measure of certainty C a pattern is a statement S in L that describes
relationships among a subset Fs of F with a certainly c such that S is simpler in
some sense than the enumeration of all the facts in Fs.
Interpretation and Evaluation – The patterns identified by the system are
interpreted into knowledge which can be used to support human decision-making
e.g. prediction and classification tasks, summarizing the content of a database
or explaining observed phenomena.
Summary
> The basic concepts of database that can be used by various users to store
and retrieve thae data in standardized format.
> DBMS features, parts,problems and solutions.
>Three database structures.
> Enitity relations.
> Various relationships.
> Keys
> Database warehouse, Data mining

You might also like