Bit2203 Advanced Object-Oriented Programming Lectures Sep 2021
Bit2203 Advanced Object-Oriented Programming Lectures Sep 2021
ADVANCED OBJECT-
ORIENTED
PROGRAMMING
Prerequisite:
DIT 0301 Object-Oriented Programming
Course Purpose
• To enable the student to understand advanced
object-oriented programming concepts
Contact Hours
45 CONTACT HOURS
Learning Outcomes
• Upon completion of the course the students
should be able to:
1) Demonstrate understanding of advanced
concepts of OOP.
2) Exhibit skills in advanced object-oriented
programming
Course Description
Introduction to threads and processes.
Exceptions and Exception handling.
Operator overloading.
Function and class templates.
Standard template libraries(STL).
Dynamic and static binding.
Files; streams, file input stream and file
output streams, pipes.
Java OOPs Concepts
Anthony@2014
The Object-Oriented Programming (OOP) Approach
Thinking Objects
• The newer programming languages use a different
approach. With OOP, programs are based on real
world objects.
• This method of programming is based on creating
programming code that emulates real world
entities, thus the word Object.
• In OOP, instead of focusing on solving the
problem, you focus and think in terms of the
Objects that will play an important role in the
program.
Anthony@2014
The Object-Oriented Programming (OOP) Approach
Anthony@2014
Introduction OOPs Concepts
• We will learn about basics of OOPs concepts
here.
• Object Oriented Programming is a paradigm
that provides many concepts such
as inheritance, data binding,
polymorphism etc.
• The programming paradigm where everything
is represented as an object, is known as truly
object-oriented programming language.
Anthony@2014
Intro to Object Oriented
• Object Oriented Programming is a
programming method that combines:
a) Data
b) Instructions for processing that data
into a self-sufficient ‘object’ that can be used
within a program or in other programs.
Anthony@2014
Advantage of Object Oriented
Programming
Anthony@2014
OOPs (Object Oriented Programming System)
• Object means a real world entity such as pen,
chair, table etc. Object-Oriented Programming is
a methodology or paradigm to design a program
using classes and objects.
• It simplifies the software development and
maintenance by providing some concepts
– Object
– Class
– Inheritance
– Polymorphism
– Abstraction
– Encapsulation
Anthony@2014
Object
• Objects are the items that represent real-world
entities, such as a person, place or thing in a
program.
• In a program an Object is a software
representation of a real-world entity.
• Any entity that has state and behavior is known
as an object. For example: chair, pen, table,
keyboard, bike etc. It can be physical and logical.
• Object: is a bundle of related
variables and functions (also known methods).
Anthony@2014
Object
• Objects share two characteristics: They
have State and Behavior.
– State: State is a well defined condition of an item. A
state captures the relevant aspects of an object
– the state is the set of values of an object's variables
at any particular time
– Behavior: Behavior is the observable effects of an
operation or event,
– the behaviour is implemented as methods.
Examples 1:
Object: House
State: Current Location, Color, Area of House etc
Behavior: Close/Open main door.
Anthony@2014
Object
• Examples 2:
Object: – Car
State: Color, Make
Behavior: Climb Uphill,
Accelerate, SlowDown etc
• Note: Everything a
software object knows
(State) and can do
(Behavior) is represented
by variables and methods
(functions) in the object
respectively.
Anthony@2014
Class
• Collection of objects is called class. It is a logical
entity.
• A class is a prototype that defines the variables
and the methods common to all objects of a
certain kind.
• Member Functions operate upon the member
variables of the class.
• An Object is created when a class is instantiated.
Anthony@2014
Class
• How to create an Object?
– An object is created when a class is instantiated
• Declaring an Object of class :
ClassName Objectname;
• Object definition is done by calling the class
constructor
Anthony@2014
Class
• Constructor: A special member function which
will be called automatically to initialize the data
member of a class whenever object is
instantiated.
Memory space is allocated only when a class is
instantiated i.e. when an object is created
• Defining a variable :
E.g.
GraduationCourse mycourse= new
GraduationCourse();
Anthony@2014
Inheritance
• When one object acquires all or some of the
properties and behaviours of parent object i.e.
known as inheritance.
• The process by which one class acquires the
properties and functionalities of another class.
• Inheritance provides the idea of reusability of
code and each sub class defines only those
features that are unique to it.
• It is used to achieve runtime polymorphism.
Anthony@2014
Inheritance
• Inheritance is a mechanism of defining a new
class based on an existing class.
• Inheritance enables reuse of code. Inheritance
also provides scope for refinement of the
existing class. Inheritance helps in specialization
• The existing (or original) class is called the base
class or super class or parent class. The new
class which inherits from the base class is called
the derived class or sub class or child class.
• Inheritance implements the “Is-A” or “Kind Of/
Has-A” relationship.
Anthony@2014
Inheritance Example
• Consider below two classes -
Anthony@2014
Inheritance Example
• Points:
1) Both the classes share few
common properties and
methods. Thus repetition of
code.
2) Creating a class which
contains the common methods
and properties.
3) The
classes Teacher and OfficeStaff c
an inherit the all the common
properties and methods from
below Employee class
Anthony@2014
Inheritance Example
• 4) Add individual methods and properties to it
Once we have created a super class that defines
the attributes common to a set of objects, it can
be used to create any number of more specific
subclasses
5) Any similar classes like Engineer, Principal can
be generated as subclasses from the Employee
class.
6) The parent class is termed super class and the
inherited class is the sub class
Anthony@2014
Inheritance Example
7) A sub class is the specialized version of a
super class – It inherits all of the instance
variables and methods defined by the super
class and adds its own, unique elements.
8) Although a sub class includes all of the
members of its super class it can not access
those members of the super class that have
been declared as private.
9) A reference variable of a super class can be
assigned to a reference to any sub class derived
from that super class
i.e. Employee emp = new Teacher();
Anthony@2014
Polymorphism
• The ability to change form is known as polymorphism.
• Polymorphism is a feature that allows one interface to be
used for a general class of actions.
• When one task is performed by different ways i.e. known
as polymorphism.
• It’s an operation may exhibit different behavior in different
instances. The behavior depends on the types of data used
in the operation.
• It plays an important role in allowing objects having
different internal structures to share the same external
interface.
• Polymorphism is extensively used in implementing
inheritance. Anthony@2014
Polymorphism
Anthony@2014
Polymorphism
• For example: to draw something e.g. shape or
rectangle etc.
• Another example can be to speak something e.g.
cat speaks meaw, dog barks woof etc.
• Types of Polymorphism
1) Static Polymorphism
2) Dynamic Polymorphism
• In java, we use method overloading and method
overriding to achieve polymorphism.
Anthony@2014
Abstraction
• The purpose of abstraction is to hide
information that is not relevant or rather show
only relevant information and to simplify it by
comparing it to something similar in the real
world.
• Hiding internal details and showing
functionality is known as abstraction. For
example: phone call, we don't know the internal
processing.
• In java, we use abstract class and interface to
achieve abstraction.
Anthony@2014
Encapsulation
• Encapsulation means the localization of the
information or knowledge within an object.
• Binding (or wrapping) code and data together
into a single unit is known as encapsulation.
• Encapsulation in Java is the process of wrapping
up of data (properties) and behavior (methods)
of an object into a single unit; and the unit here
is a Class (or interface). Encapsulate in plain
English means to enclose or be enclosed in or as
if in a capsule.
• For example: capsule, it is wrapped with
different medicines.
Anthony@2014
Encapsulation
• Encapsulation is also called as “Information
Hiding”.
1) Objects encapsulate data and implementation
details. To the outside world, an object is a black
box that exhibits a certain behavior.
2) The behavior of this object is what which is
useful for the external world or other objects.
3) An object exposes its behavior by means of
methods or functions.
4) The set of functions an object exposes to
other objects or external world acts as the
interface of the object.
Anthony@2014
Encapsulation
• We can expose our operations hiding the details
of what is needed to perform that operation.
• We can protect the internal state of an object by
hiding its attributes from the outside world (by
making it private), and then exposing them
through setter and getter methods.
Now modifications to the object internals are
only controlled through these methods.
Anthony@2014
Benefits of Encapsulation
• 1) The functionality where in we can change the
implementation code without breaking the code of
others who use our code is the biggest benefit of
Encapsulation.
2) Here in encapsulation we hide the implementation
details behind a public programming interface. By
interface, we mean the set of accessible methods our
code makes available for other code to call—in other
words, our code’s API.
3) By hiding implementation details, We can rework
on our method code at a later point of time, each
time we change out implementation this should not
affect the code which has a reference to our code, as
our API still remains the same
Anthony@2014
How to bring in Encapsulation
• 1) Make the instance variables protected.
2) Create public accessor methods and use these
methods from within the calling code.
3) Use the JavaBeans naming convention
of getter and setter.
Eg: getPropertyName, setPropertyName.
• A setter is a method used to change the value of
an attribute and a getter is a method used to get
the value of an attribute. There is also a standard
naming convention for getters and setters, but
Java compiler won't complain even otherwise.
Anthony@2014
Example for encapsulation
Anthony@2014
Example for encapsulation
Anthony@2014
Example for encapsulation
Anthony@2014
Message passing
• A single object by itself may not be very useful.
An application contains many objects.
• One object interacts with another object by
invoking methods (or functions) on that object.
Through the interaction of objects, programmers
achieve a higher order of functionality which has
complex behavior.
• One object invoking methods on another object
is known as Message passing.
• It is also referred to as Method Invocation.
Anthony@2014
References
• Java Programming by Joyce Farrel
• Balagurusamy E., "Programming in Java",
TMH.
• 116. Naughton, Schildt, "The Complete
Reference Java2", TMH.
• www.oracle.com/java
Anthony@2014
END of OOPs Concepts
Anthony@2014
Multithreading
in
Java
Introduction to Thread and
Multithreading
• Multithreading in java is a process of
executing multiple threads simultaneously.
• A thread is a lightweight sub-process, the
smallest unit of processing.
• Multiprocessing and multithreading, both are
used to achieve multitasking.
Introduction to Thread and
Multithreading
• However, we use multithreading than
multiprocessing because threads use a shared
memory area.
• They don't allocate separate memory area so
saves memory, and context-switching
between the threads takes less time than
process.
• Java Multithreading is mostly used in games,
animation, etc.
Multitasking
• Multitasking is a process of executing multiple
tasks simultaneously.
• Multitasking is the ability of a computer's
operating system to run several programs
(or processes) concurrently on a single CPU.
• This is done by switching from one program to
another fast enough to create the appearance
that all programs are executing
simultaneously.
Thread-based Multitasking
(Multithreading)
• Multithreading extends the concept of multitasking by
allowing individual programs to perform several tasks
concurrently.
• Each task is referred to as a thread and it represents a
separate flow of control.
• Multithreading can be very useful in practical
applications.
• For example, if a web page is taking too long to load in
a web browser, the user should be able interrupt the
loading of the page by clicking on the stop button. The
user interface can be kept responsive to the user by
using a separate thread for the network activity
needed to load the page.
Thread-based Multitasking
(Multithreading)
• Threads share the same address space.
• A thread is lightweight.
• Cost of communication between the thread is
low.
Process vs Thread
Definition of Thread
• The threads are extensively used in Java-
enabled browsers.
• A thread is a part of a process.
• A process can contain multiple threads.
• These multiple threads in a process share the
same address space of the process.
• Each thread has its own stack and register to
operate on.
Process vs Thread
Definition of Thread
• Let us see how a thread is created. There are
two methods to create a thread.
• First, you have to define a class that must
extend the “Thread” class and further it must
override the run( ) method of the Thread
class.
What is Thread in java
• A thread is a lightweight subprocess, the
smallest unit of processing.
• It is a separate path of execution.
• Threads are independent.
• If there occurs exception in one thread, it
doesn't affect other threads.
• It uses a shared memory area.
Java Thread class
• Java provides Thread class to achieve thread
programming.
• Thread class provides constructors and
methods to create and perform operations on
a thread.
• Thread class extends Object class and
implements Runnable interface.
Life cycle of a Thread (Thread States)
• A thread can be in one of the five states.
1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated
• The life cycle of the thread in java is controlled
by JVM.
Life cycle of a Thread (Thread States)
Life cycle of a Thread (Thread States)
New
• A new thread begins its life cycle in the new
state.
• The thread is in new state if you create an
instance of Thread class but before the
invocation of start() method.
• It remains in this state until the program starts
the thread.
• It is also referred to as a born thread.
Runnable
• After a newly born thread is started, the
thread becomes runnable.
• A thread in this state is considered to be
executing its task.
• The thread is in runnable state after
invocation of start() method, but the thread
scheduler has not selected it to be the running
thread.
Running
• The thread is in running state if the thread
scheduler has selected it.
Non-Runnable (Blocked)/Waiting
• This is the state when the thread is still alive,
but is currently not eligible to run.
• Sometimes, a thread transitions to the waiting
state while the thread waits for another
thread to perform a task.
• A thread transitions back to the runnable state
only when another thread signals the waiting
thread to continue executing.
Timed Waiting
• A runnable thread can enter the timed waiting
state for a specified interval of time.
• A thread in this state transitions back to the
runnable state when that time interval expires
or when the event it is waiting for occurs.
Terminated
• A thread is in terminated or dead state when
its run() method exits.
• A runnable thread enters the terminated state
when it completes its task or otherwise
terminates.
How to create thread
• Let us see how a thread is created.
• There are two methods to create a thread. First,
you have to define a class that must extend the
“Thread” class and further it must override the
run( ) method of the Thread class.
• Second, you can also implement
the Runnable interface that contains only the
run( ) method, that is to be defined by the
implementing class.
How to create thread
• In summary thee two ways to create a thread
are as follow:
– By extending Thread class
– By implementing Runnable interface.
Create a Thread by Implementing a
Runnable Interface
• If your class is intended to be executed as a
thread then you can achieve this by
implementing a Runnable interface.
• You will need to follow three basic steps −
• Step 1
– Implement a run() method provided by
a Runnable interface.
– This method provides an entry point for the
thread and you will put your complete business
logic inside this method.
Create a Thread by Implementing a
Runnable Interface
• You will need to follow three basic steps −
• Step 2
– As a second step, you will instantiate
a Thread object using the following constructor −
Anthony@8014
Exceptions Handling
Outline
1. Introduction to Exceptions Handling
2. Exceptions Handling in C++
3. Exceptions Handling in Java
Introduction
• An exception is an abnormal condition that arises
in a code sequence at run time.
• An exception is a problem that arises during the
execution of a program.
• An exception can occur for many different
reasons, including the following:
– A user has entered invalid data.
– A file that needs to be opened cannot be found.
– A network connection has been lost in the middle of
communications or the JVM has run out of memory.
Errors
• Errors can be broadly categorized into two
types. We will discuss them one by one.
– Compile Time Errors
– Run Time Errors
Errors
• Compile Time Errors – Errors caught during
compiled time is called Compile time errors.
Compile time errors include library reference,
syntax error or incorrect class import.
• Run Time Errors - They are also known as
exceptions. An exception caught during run time
creates serious issues.
– Errors hinder normal execution of program.
• Exception handling is the process of handling
errors and exceptions in such a way that they do
not hinder normal execution of the system.
C++ Exception Handling
• Exception Handling in C++ is a process to
handle runtime errors.
• We perform exception handling so the normal
flow of the application can be maintained
even after runtime errors.
C++ Exception Handling
• In C++, exception is an event or object which is
thrown at runtime.
• All exceptions are derived from std::exception
class.
• It is a runtime error which can be handled.
• If we don't handle the exception, it prints
exception message and terminates the
program.
Advantage of Exception Handling
• It maintains the normal flow of the
application.
• In such case, rest of the code is executed even
after exception.
C++ Exception Handling Keywords
• In C++, we use 3 keywords to perform
exception handling:
– try
– catch, and
– throw
C++ try/catch
• In C++ programming, exception handling is
performed using try/catch statement.
• The C++ try block is used to place the code
that may occur exception.
• The catch block is used to handle the
exception.
C++ try/catch Syntax
try block
• The code which can throw any exception is
kept inside(or enclosed in) a try block.
• Then, when the code will lead to any error,
that error/exception will get caught inside
the catch block.
catch block
• catch block is intended to catch the error and
handle the exception condition.
• We can have multiple catch blocks to handle
different types of exception and perform
different actions when the exceptions occur.
• For example, we can display descriptive
messages to explain why any particular
exception occurred.
throw statement
• It is used to throw exceptions to exception
handler i.e. it is used to communicate
information about error.
• A throw expression accepts one parameter and
that parameter is passed to handler.
• throw statement is used when we explicitly want
an exception to occur, then we can use throw
statement to throw or generate that exception.
Understanding Need of Exception
Handling
• Let's take a simple example to understand the
usage of try, catch and throw.
• Below program compiles successfully but the
program fails at runtime, leading to an
exception.
Understanding Need of Exception
Handling
• The above program will not run, and will
show runtime error on screen, because we
are trying to divide a number with 0, which is
not possible.
• How to handle this situation?
• We can handle such situations using exception
handling and can inform the user that you
cannot divide a number by zero, by displaying
a message.
C++ example with try/catch
Other Examples on Exception
Handling in C++
C++ example without try/catch
C++ example without try/catch
C++ example without try/catch
C++ try/catch example
• Output
C++ try/catch example
• Output
C++ User-Defined Exceptions
• The new exception can be defined by overriding
and inheriting exception class functionality.
• Let's see the simple example of user-defined
exception in which std::exception class is used
to define the exception.
• Note: In the next slide example what() is a public
method provided by the exception class.
• It is used to return the cause of an exception
C++ User-Defined Exceptions
Exception Handling in Java
Exception Handling in Java
• The Exception Handling in Java is one of the
powerful mechanism to handle the runtime
errors so that normal flow of the application
can be maintained.
• What is Exception in Java
– In Java, an exception is an event that disrupts the
normal flow of the program.
– It is an object which is thrown at runtime.
Types of Java Exceptions
• There are mainly two types of exceptions:
checked and unchecked
– Checked Exception
– Unchecked Exception
Checked exceptions
• Some of these exceptions are caused by user
error, others by programmer error, and others
by physical resources that have failed in some
manner.
– Checked exceptions: A checked exception is an
exception that is typically a user error or a problem
that cannot be foreseen by the programmer.
– For example, if a file is to be opened, but the file
cannot be found, an exception occurs. These
exceptions cannot simply be ignored at the time of
compilation.
Checked exceptions
Anthony@8018
Operator Overloading
Outline
• In this part you will learn:
1. What is meant by operator overloading
2. Why we need to overload operators
3. Syntax
4. Program
5. Output
Introduction to Operator Overloading
Anthony@8014
Templates
Introduction to Templates
• Templates are the foundation of generic
programming, which involves writing code in a
way that is independent of any particular data
type.
• A template is a blueprint or formula for
creating a generic class or a function.
• Template is simple and yet very powerful tool
in C++.
• The simple idea is to pass data type as a
parameter so that we don’t need to write same
code for different data types.
Introduction to Templates
• Templates are used to prevent us from writing
the same function or class separately for
different data types.
• For example a software company may need
sort() for different data types.
• Rather than writing and maintaining the
multiple codes, we can write one sort() and
pass data type as a parameter.
To understand its need, let's first look
at the following program.
To understand its need, let's first look
at the following program.
• In the above example, we defined a separate
function to calculate the sum of three sets of
numbers, each with a different data type.
• Isn't it hectic to define the same function
again and again for different data types ?
How templates work?
• Templates are expanded at compiler time.
• This is like macros. The difference is, compiler
does type checking before template
expansion.
• The idea is simple, source code contains only
function/class, but compiled code may contain
multiple copies of same function/class.
How templates work?
Concept Of Templates
• The concept of templates can be used in two
different ways:
– Function Templates
– Class Templates
Function Templates
• A function template works in a similar to a
normal function, with one key difference.
• Function Templates prevent us from defining
separate functions performing the same task for
different data types.
• A single function template can work with
different data types at once but, a single normal
function can only work with one set of data
types.
• We write a generic function that can be used for
different data types.
Function Templates
• Normally, if you need to perform identical
operations on two or more types of data, you
use function overloading to create two
functions with the required function
declaration.
• However, a better approach would be to use
function templates because you can perform
the same task writing less and maintainable
code.
How to declare function Template
• A function template starts with the
keyword template followed by template
parameter/s inside < > which is followed by
function declaration.
How to declare function Template
• You can also use
keyword typename instead of
class in the this example.
Anthony@8014
Standard Template Libraries
(STL)
Introduction to STL
• The Standard Template Library (STL) is a set of
C++ template classes to provide common
programming data structures and functions such
as lists, stacks, arrays, etc.
OR
• The C++ STL provides general-purpose classes
and functions with templates that implement
many popular and commonly used algorithms
and data structures like vectors, lists, queues, and
stacks.
Introduction to STL
• A working knowledge of template classes is a
prerequisite for working with STL.
• STL has four components
– Algorithms
– Containers
– Iterators
Algorithms
• C++ provides a set of generic algorithm for
STD in header <algorithm>
• The header algorithm defines a collection of
functions especially designed to be used on
ranges of elements.
• They act on containers and provide means
for various operations for the contents of the
containers.
– Sorting: sort(), partial_sort(), merge().
– Searching: find(), count()
Sort in C (STL)
Anthony@8014
Dynamic and Static Binding
Understanding Binding
• Binding generally refers to a mapping of one
thing to another.
• In the context of compiled languages, binding
is the link between a function call and the
function definition.
• When a function is called in C++, the program
control binds to the memory address where
that function is defined.
Types of Binding
• There are two types of binding in C++:
– static (or early) binding and
– dynamic (or late) binding.
Early Binding
• In early binding, the compiler matches the
function call with the correct function
definition at compile time.
• It is also known as Static Binding or Compile-
time Binding.
• By default, the compiler goes to the function
definition which has been called during
compile time.
Early Binding
Example of Static Binding in C++
Late Binding
• Late binding, the compiler matches the
function call with the correct function
definition at runtime.
• It is also known as Dynamic
Binding or Runtime Binding.
• In late binding, the compiler identifies the
type of object at runtime and then matches
the function call with the correct function
definition.
Late Binding
• By default, early binding takes place.
• Late binding is achieved by declaring a virtual
function.
• Virtual Function is a member function of the
base class which is overridden in the derived
class.
• The compiler performs late binding on this
function.
• To make a function virtual, we write the
keyword virtual before the function definition.
Late Binding
Example of Dynamic Binding in C++
Difference between Static and Dynamic Binding in C++
END
of
Dynamic and Static Binding
Anthony@2018
Memory Management in C++
Introduction to memory Management
• Arrays can be used to store multiple
homogenous data but there are serious
drawbacks of using arrays.
• You should allocate the memory of an array
when you declare it but most of the time, the
exact memory needed cannot be determined
until runtime.
Introduction to memory Management
• The best thing to do in this situation is to
declare an array with maximum possible
memory required (declare array with
maximum possible size expected).
• The downside to this is unused memory is
wasted and cannot be used by any other
programs.
Dynamic Memory
• To avoid wastage of memory, you can dynamically
allocate memory required during runtime
using new and delete operator in C++.
• C++ provide Dynamic Memory mechanism
• A good understanding of how dynamic memory
really works in C++ is essential to becoming a
good C++ programmer.
• Memory in your C++ program is divided into two
parts −
– The stack
– The heap
The Stack vs The Heap
• The stack -
– All variables declared inside the function will take
up memory from the stack.
• The heap −
– This is unused memory of the program and can be
used to allocate the memory dynamically when
program runs.
Understanding dynamic memory
management
• Many times, you are not aware in advance how
much memory you will need to store particular
information in a defined variable and the size of
required memory can be determined at run time.
• You can allocate memory at run time within the
heap for the variable of a given type using a
special operator in C++ which returns the address
of the space allocated.
• This operator is called new operator.
Understanding dynamic memory
management
• If you are not in need of dynamically allocated
memory anymore, you can
use delete operator, which de-allocates
memory that was previously allocated by new
operator.
new and delete Operators
• There is following generic syntax to
use new operator to allocate memory
dynamically for any data-type.
new data-type;
• Here, data-type could be any built-in data type
including an array or any user defined data
types include class or structure.
new and delete Operators
• Let us put above concepts and form the
following example to show how ‘new’ and
‘delete’ work −
Dynamic Memory Allocation for
Arrays
Example 1: C++ Memory Management
The new Operator
• In the previous program, only the memory
required to store num (entered by user)
number of floating-point data is declared
dynamically.
Anthony@2018
Files; streams, file input stream
and file output streams
Introduction to File Handling using
File Streams
• When a program runs, the data is in the
memory but when it ends or the computer
shuts down, it gets lost.
• To keep data permanently, we need to write it
in a file.
Introduction to File Handling using
File Streams
• File represents storage medium for storing
data or information.
• Streams refer to sequence of bytes.
• In Files we store data i.e. text or binary data
permanently and use these data to read or
write in the form of input output operations
by transferring bytes of data
Why use File Handling
• For permanent storage.
• The transfer of input - data or output - data
from one computer to another can be easily
done by using files.
Reading and writing files in C++
• For read and write from a file you need
another standard C++ library called fstream,
which defines three new data types:
How to achieve File Handling
• For achieving file handling in C++ we need
follow following steps
1. Naming a file
2. Opening a file
3. Reading data from file
4. Writing data into file
5. Closing a file
Functions use in File Handling
Creating /Opening a File
• A file must be opened before you can read
from it or write to it.
• Either ofstream or fstream object may be
used to open a file for writing.
• And ifstream object is used to open a file for
reading purpose only.
Creating /Opening a File
Writing to a File
• While doing C++ programming, you write
information to a file from your program using
the stream insertion operator (<<) just as you
use that operator to output information to the
screen.
• The only difference is that you use
an ofstream or fstream object instead of
the cout object.
Writing to a File
Reading from a File
C++ Read and Write
Closing a File
• When a C++ program terminates it
automatically flushes all the streams, release
all the allocated memory and close all the
opened files.
• But it is always a good practice that a
programmer should close all the opened files
before program termination.
• Syntax void close();
END
of
the Course Advanced Object Oriented
Programming Sep-Dec 2021
Anthony@2018
Teaching Methodologies
• Lectures, practical and tutorial sessions in
Computer Laboratory, individual and group
assignments, exercises and project work
Instructional Materials
• LCD projector and computer, handouts, white
boards
• Course Assessment
• 40% Continuous Assessment (Tests 10%,
Assignment 10%, Practical 20%)
• 60% End of Semester Exams
Course Textbooks
• 1. Java in a Nutshell (5th Edition), David Flanagan,
O’Reilly Press, 2005.ISBN 342- 539003
• 2. The Object-Oriented Thought Process, 3rd
Edition, Matt Weisfeld, Addison-Wesley, 2009.
Excellent discussion of the O-O process. ISBN
342-652504
• 3. Flanagan, D. (2004). Java examples in a nutshell
: a tutorial companion to Java in a nutshell.
O'Reilly (3rd ed.) ISBN 342-4590232
References
• 1. An Introduction to Programming and Object
Oriented Design Using Java, Jaime(2007) ISBN
345-87590556
• 2. Nino and Frederick A. Hosch, John
Wiley(2009),Object Oriented Programing
Techniques, New York. ISBN 789-454832
• 3. Flanagan, D. (2005). Java in a nutshell : a
desktop quick reference. O'Reilly (5th
ed.).ISBN 342-542950
Course Journals
• 1. Acta Informatica ISSN 0001-5903
• 2. Advances in Computational Mathematics
ISSN 1019-7168
• 3. Advances in data Analysis and Classification
ISSN1 1862-5347
• 4. Annals Of software Engineering ISSN 1022-
7091
Reference Journals
• 1. Journal of computer science and
Technology ISSN 1000-9000
• 2. Journal of Science and Technology ISSN
1860-4749
• 3. Central European Journal Of Computer
Science ISSN 1896-1533
• 4. Cluster computing ISSN 1386-7857
•