12 CS 2019-20 Lucknow Public School SM1
12 CS 2019-20 Lucknow Public School SM1
SESSION-2019-20
STUDY MATERIAL
FRAGMENT 1
CHAPTERS INCLUDED:
TEACHERS’ CONTRIBUTORS:
Object Oriented Programming follows bottom up approach in program design and emphasizes on
safety and security of data..
Inheritance:
Inheritance is the process of forming a new class from an existing class or base class. The base
class is also known as parent class or super class.
Derived class is also known as a child class or sub class. Inheritance helps in
reusability of code , thus reducing the overall size of the program
Data Abstraction:
It refers to the act of representing essential features without including the background details
.Example : For driving , only accelerator, clutch and brake controls need to be learnt rather
than working of engine and other details.
Data Encapsulation:
It means wrapping up data and associated functions into one single unit called class..
A class groups its members into three sections :public, private and protected, where private
and protected members remain hidden from outside world and thereby helps in implementing
data hiding.
Modularity :
The act of partitioning a complex program into simpler fragments called modules is
called as modularity.
It reduces the complexity to some degree and
It creates a number of well defined boundaries within the program .
Polymorphism:
Poly means many and morphs mean form, so polymorphism means one name multiple forms.
It is the ability for a message or data to be processed in more than one form.
C++ implements Polymorhism through Function Overloading , Operator overloading and
Virtual functions .
. SHAPE
area()
SOLVED QUESTIONS
Q2. What are programming paradigms? Give names of some popular programming paradigms.
Ans. Programming Paradigm: A Programming Paradigm defines the methodology of designing
and implementing programs using the key features and building blocks of a programming
language.
Following are the different programming paradigms:
(i) Procedural Programming
(ii) Object Based Programming
(iii) Object Oriented Programming
Q3. What are the shortcomings of procedural and modular programming approaches?
Ans. Following are the various shortcomings of procedural and modular programming
approaches:
Procedural Programming is susceptible to design changes.
Procedural Programming leads to increased time and cost overheads during design changes.
Procedural and Modular programming both are unable to represent real world relationship
that exists among objects.
Study Material - CS-XII Chap - 3 : Object Oriented Programming 2
Study Material - CS-XII Chap - 3 : Object Oriented Programming 3
In modular programming, the arrangement of the data can’t be changed without modifying
all the functions that access it.
Q5. What is the significance of private, protected and public specifiers in a class?
Ans. A class groups its members into three sections: private, protected, and public. The private
and protected members remain hidden from outside world. Thus through private and
protected members, a class enforces data-hiding. The public members are accessible
everywhere in a program.
ASSIGNMENT
Problem 1: What do you understand by Polymorphism? Give an example in C++ to show its
implementation in C++.
Problem 3: What is Inheritance? Give an example in C++ to show its implementation in C++.
Problem 5: Encapsulation is one of the major properties of OOP. How is it implemented in C++?
Problem 6: Reusability of classes is one of the major properties of OOP. How is it implemented in
C++?
Problem 7: Define the term Data Hiding in the context of Object Oriented Programming. Give a
suitable example using a C++ code to illustrate the same.
return s*s;
return l*b;
float s, ar;
s=(a+b+c)/2;
ar=sqrt((s*(s-a)*(s-b)*(s-c));
return ar;
In the above example , we can see there are three definitions given for the user-
defined function AREA. The first definition has only one parameter, and is
returning the area of a square. The second definition of the function AREA,
carrying two parameters and is returning the area of a rectangle, while the third
definition of the function AREA, is having three parameters and is returning the
area of a triangle.
ADVANTAGE :
As we can understand from the above example, that to get the area of
square, rectangle and triangle, we donot need to declare three different functions
with different names, and more importantly , we are not supposed to write three
programs. We can get the area of our desired shape, according to our need, that
too, within the same program
SOME QUESTIONS :
Q.1 Which function is called during each function call in the program
given below:
void main ( )
int n;
char ch,ch1;
float n1;
sum(n); // call 1 //
choice(ch); // call 2 //
Answer :
Q.2 Write the prototype of the overloaded function called sum( ) that add rwo
integers and return an integer, that add two double precision quantity and return
double precision quantity, that add one float and one integer and return float
quantity.
return ( s * s );
return (.5 * b* h );
void main ( )
cout<<area(4,3)<< endl;
cout<<area(6,area(3))<<endl;
25
27
class x
cout<<”double”<<endl;
cout<<”int”<<endl;
};
void main ()
X obj;
char ch=’A’;
int num=66;
float pi=3.14;
obj.print(ch);
obj.print(pi);
obj.print(st);
obj.print(num);
1. long p , int T
3. long P, float r
4. int t
NOTE :
The major components of Object Oriented Programming are . Classes & Objects
A Class is a group of similar objects . Objects share two characteristics: They all have state and
behavior. For example : Dogs have state (name, color, breed, hungry) and behavior (barking,
fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current
speed) and behavior (changing gear, applying brakes). Identifying the state and behavior for real-
world objects is a great way to begin thinking in terms of object-oriented programming. These
real-world observations all translate into the world of object-oriented programming.
Software objects are conceptually similar to real-world objects: they too consist of state and
related behavior. An object stores its state in fields (variables in some programming languages)
and exposes its behavior through functions
Classes in Programming :
Class Declaration/Definition :
A class definition starts with the keyword class followed by the class name; and the class body,
enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a
list of declarations.
class <class_name> {
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
Where class_name is a valid identifier for the class, object_names is an optional list of names for
objects of this class. The body of the declaration can contain members that can either be data or
function declarations, and optionally access specifiers.
[Note: the default access specifier is private.]
Example : class Box
{ int a;
public:
double length; // Length of a box
Member-Access Control
Type of Access Meaning
Access control helps prevent you from using objects in ways they were not intended to be
used. Thus it helps in implementing data hiding and data abstraction.
OBJECTS in C++:
Objects represent instances of a class. Objects are basic run time entities in an object
oriented system.
Both of the objects Box1 and Box2 will have their own copy of data members.
Object_name.Data_member=value;
The dot ('. ') used above is called the dot operator or class member access operator. The dot
operator is used to connect the object and the member function. The private data of a class can
be accessed only through the member function of that class.
The member functions of a class can be defined outside the class definitions. It is only
declared inside the class but defined outside the class. The general form of member function
definition outside the class definition is:
The scope resolution operator (::) specifies the class to which the member being declared
belongs, granting exactly the same scope properties as if this function definition was directly
included within the class definition
class sum
{
int A, B, Total;
public:
void getdata ();
void display ();
};
void sum:: getdata () // Function definition outside class definition Use of :: operator
{
cout<<” \n enter the value of A and B”;
cin>>A>>B;
}
Study Material - CS-XII Chap - 4 : Classes and Objects 3
Study Material - CS-XII Chap - 4 : Classes and Objects 4
void sum:: display () // Function definition outside class definition Use of :: operator
{
Total =A+B;
cout<<”\n the sum of A and B=”<<Total;
}
class sum
{
int A, B, Total;
public:
void getdata ()
{
cout< ”\n enter the value of A and B”;
cin>>A>>B;
}
void display ()
{
total = A+B;
cout<<”\n the sum of A and B=”<<total;
}
};
Difference between structure and class in C++
In C++, a structure is a class defined with the struct keyword.Its members and base classes
are public by default. A class defined with the class keyword has private members and base
classes by default. This is the only difference between structs and classes in C++.
INLINE FUNCTIONS
#include< iostream.h>
#include< conio.h>
class height
{
int feet,inches;
public:
void getht(int f,int i)
{
feet=f;
inches=i;
}
void putheight()
{
cout< < "\nHeight is:"< < feet< < "feet\t"< < inches< < "inches"< < endl;
}
void sum(height a,height b)
{
height n;
n.feet = a.feet + b.feet;
n.inches = a.inches + b.inches;
if(n.inches ==12)
{
n.feet++;
n.inches = n.inches -12;
}
cout< < endl< < "Height is "< < n.feet< < " feet and "< < n.inches< <
endl; }};
void main()
{height h,d,a;
clrscr();
h.getht(6,5);
a.getht(2,7);
h.putheight();
a.putheight();
d.sum(h,a);
getch();
}
/**********OUTPUT***********
Height is:6feet 5inches
Height is:2feet 7inches
Height is 9 feet and 0
Solved Problems :
Ans.
class TAXPAYER
{
char Name[30],PanNo[30];
float Taxabincm;
double TotTax;
void CompTax()
{ if(Taxabincm
>500000) TotTax=
Taxabincm*0.15; else
if(Taxabincm>300000)
TotTax=
Taxabincm*0.1; Else
if(Taxabincm>160000)
TotTax=
Taxabincm*0.05; else
TotTax=0.0; }
public:
TAXPAYER(char nm[], char pan[], float tax, double tax) //parameterized constructor
{
strcpy(Name,n
m);
strcpy(PanNo,p
an);
Taxabincm=tax
; TotTax=ttax;
} void
INTAX()
{ gets(Name);
cin>>PanNo>>Taxabi
ncm; CompTax();
}
void OUTAX()
{ cout<<Name<<‟\n‟<<PanNo<<‟\n‟<<Taxabincm<<‟\n‟<<TotTax<<endl; }
};
Solution :
#include<iostream.h>
class HOTEL
{unsigned int Rno;
char
Name[25];
unsigned int
Tariff;
unsigned int
NOD; int
CALC()
{int x; x=NOD*Tariff;
if( x>10000)
return(1.05*NOD*Ta
riff);
else
return(NOD*Tariff);
}
public:
void Checkin()
{cin>>Rno>>Name>>Tariff>>NOD;}
void Checkout()
{cout<<Rno<<Name<<Tariff<<NOD<<CALC();}
};
Public Members
A function Enter( ) to allow user to enter values for ANo, Name, Agg & call
function GradeMe( ) to find the Grade
A function Result ( ) to allow user to view the content of all the data members.
Solution:-
class ITEM
{int Icode,Qty;
char item[20];
float price,discount;
void finddisc();
public:
void buy();
void showall();
};
void stock::finddisc()
{If (qty<=50)
Discount=0;
Else if (qty> 50 && qty <=100)
Discount=0.05*price;
Else if (qty>100)
Discount=0.10*price;
}
void stock::buy()
{cout<<"Item Code :";cin>>Icode;
cout<<"Name :";gets(Item);
cout<<"Price :";cin>>Price;
cout<<"Quantity :";cin>>Qty;
finddisc();
}
void TEST::DISPTEST()
{cout<<"Item Code :";cout<<Icode;
cout<<"Name :";cout<<Item;
cout<<"Price :";cout<<Price;
cout<<"Quantity :";cout<<Qty;
cout<<"Discount :";cout<<discount;
}
PRACTICE QUESTIONS
Q 1. Define a class employee with the following specifications :
public members :
Readmarks() a function that reads marks and invoke the Calculate function.
Displaymarks() a function that prints the marks.
ASSIGNMENT
7. Rewrite the following C++ code after removing the syntax error(s) (if any).
Underline each correction.
include<iostream.h>
class FLIGHT{
long FlightCode;
char Description[25];
public:
void AddInfo()
{
cin>>FlightCode; gets(Description);
}
void ShowInfo()
{
cout<<FlightCode<<":" <<Description<<endl;
}
};
void main()
{
FLIGHT F;
AddInfo.F();
ShowInfo.F();
}
8. Rewrite the following program after removing the syntactical error(s) (if any).
Underline each correction.
#include[iostream.h]
#include[stdio.h]
class Employee
{
int EmpId=901;
char EName[20];
public:
Employee() {}
void Joining()
{
cin>>EmpId; gets(EName);
}
void List()
{
cout<<EmpId<<":"
<<EName<<endl;
}
}
void main(){
Employee E;
Joining.E();
E.List();
}
};
void X::init(int i,int j,int k){
a=i;
b=j;
x=k;
}
void X::print(void){
count();
cout<<"a="<<a;<<"b="
<<b<<"x="<<x<<"\";
}
void func(void);
X Ob1;
int main(){
X Ob2;
Ob1.init(0,1,2);
Ob2.init(2,3,4);
Ob1.print();
Ob2.print();
Ob1.count();
Ob2.count();
}
void func(void)
{
X Ob3;
Ob1.init(4,5,6);
Ob2.init(7,8,9);
Ob3.init(9,10,11);
Ob3.a=Ob3.b=Ob3.x;
}
10. Define a class to represent batsmen in a cricket team. Include the following
members:
Data Members:
First name, Last name, Runs made, Number of fours, Number of sixes
Member Functions:
(i) To assign the initial values
(ii) To update runs made (It should simultaneously update fours and sixes, if
required).
(iii) To display the batsman’s information
Make appropriate assumptions about access labels.
12. Declare a class to represent bank account of 10 customers with the following
data members.
Name of the depositor, Account number, Type of account (S for Savings and
C for Current), Balance amount.
The class also contains member functions to do the following:
(i) To initialize data members
(ii) To deposit money
(iii) To withdraw money after checking the balance (minimum balance in Rs. 1000)
(iv) To display the data members
public members:
Readmarks() a function that reads marks and invokes the calculate
function.
Displaymarks() a function that prints the marks.
16. Write a program that invokes a function newdate() to return a object of date
type. The function newdate() takes two parameters: an object olddate of date
type and number of days (int) to calculate the newdate as olddate + number
of days and returns the newdate.
Constructors are member functions of a class which are used to initialize the
data members of the class objects. These functions are automatically called
when an object of its class is created. There is no need to call these functions.
Characteristics of Constructors:
The name of a constructor is same as that of class in which it is declared.
Constructors do not have any return type, not even void.
Constructors are always defined in the public section of the class.
They cannot be inherited, though a derived class can call the base class
constructor.
Like other C++ functions, constructors can also be overloaded.
Types of Constructors:
There are three types of constructors.
1. Default Constructors
2. Parameterized Constructors
3. Copy Constructors
Default Constructors
A constructor that accepts no parameter is called the default constructors.
Example
class stud
{ int sno;
char sname[40];
public :
stud( ) // Default Constructor
{
sno=0;
strcpy(sname,“new” );
}
void getinfo( )
{cin>> sno;
gets(sna
me);
}
void showinfo( )
{cout<< sno;
puts(sname);
}
};
The above class stud has sno and sname as data members and three member functions
i.e. stud (), getinfo(), showinfo( )
Here stud ( ) is a default constructor since having the same name as that of class and does not
accepts any argument, also declared in the pubic section.
As we declare the object of this class it will immediately call to the constructor of the class.It
automatically assigns the value 0 to variable sno and a “new” to sname.
Here consider the following main function
void main()
{
stud obj; // Default constructor called
Obj.showinfo(); // displays the value of sno as 0 and sname as “new”
Obj.getinfo(); // reads the user given value from the user Obj.showinfo();
// displays the user given values
}
The default constructors are very useful when we want to create objects without having to
type the initial values.
With a default constructor objects are created just the same way as variables of other data
types are created.
Parameterized Constructors
A constructor that accepts parameters for its invocation is known as parameterized
constructors.
Example
class stud
{
Int sno;
char sname[40];
public :
stud(int s, char n[ ] ) // Paramerized Constructor
{
sno=s;
strcpy(sname,n);
}
void getinfo( )
{ cin>> sno;
gets(sname);
}
void showinfo( )
{ cout<< sno;
puts(sname);
}
};
This means we always specify the arguments whenever we declare an instance (object)
of the class.
void main()
{
stud obj (1, “Ashu”); // Parameterized constructor invoked Obj.showinfo();
// displays the value of sno as 1 and sname as “Ashu”
Obj.getinfo(); // reads the user given value from the user
Obj.showinfo(); // displays the user given values
}
Just like any other function a parameterized constructor can also have default
arguments
e.g.
stud(int s=0, char n[ ]=“new\0” )
A constructor with default arguments is equivalent to a default constructor.
A class must not have a default arguments constructor and default constructor
together as it generates ambiguity.
Copy Constructors:
A copy constructor is a constructor of the form classname( & classname). It is used
to initialize an object with the values of another object.
The compiler will use the copy constructor whenever -
1. We initialize an instance using values of another instance of same type.
2. A function returns an object
3. A function receives an object as parameter.
Example:
class stud
{
int sno;
char sname[40];
public :
// Copy Constructor
stud( stud &s)
{
sno=s.sno;
strcpy(sname,s.name);
}
stud( )// Default Constructor
{
sno=0;
strcpy(sname,“new” );
}
void getinfo( )
{ cin>> sno;
gets(sname);
}
void showinfo( )
{ cout<< sno;
puts(sname);
}
};
void main()
{
stud obj; // Default constructor called
Obj.showinfo(); // displays the value of sno as 0 and sname as “new”
stud objnew(obj); // Copy constructor invoked and initialize the members
of object objnew with the values of object obj i.e. 0 and
“new”.
Objnew.showinfo(); //displays the value of sno as 0 and sname as “new”
Obj.getinfo(); //reads the user given value for object obj
Points to remember:
Declaring a constructor with arguments hides the default constructor.
A Constructor with default arguments is equivalent to a default constructor.
A constructor declared under private access specifier, makes the class private and
object of a private class cannot be created.
A class must not have a default arguments constructor and default constructor
together as it generates ambiguity.
Constructors also show the polymorphism as a single class can have multiple
constructors of different forms. (Also known as constructor / function
overloading.)
Destructors:
A destructor is a class member function that has the same name as the constructor
(and the class ) but with a ~ (tilde) in front.
e.g. ~stud() ;
Destructor is used to deinitialize or destroy the class objects. When an object goes
out of scope, its destructor is automatically invoked to destroy the object.
Example :
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
Study Material - CS-XII Chap - 5 : Constructors & Destructors 4
Study Material - CS-XII Chap - 5 : Constructors & Destructors 5
class stud
{
int sno;
char sname[40];
public : // Default Constructor
stud( )
{
sno=0;
strcpy(sname,"new");
cout<<"\nConstructing the object............";
}
~stud( ) // Destructor
{
cout<< "\nDestructing the object..............";
}
void getinfo( )
{
cout<<"\nEnter Student No. :";
cin>> sno;
cout<<"\nEnter Student Name :";
gets(sname);
}
void showinfo( )
{
cout<< "\n Student No.: "<<sno;
cout<<"\n Student Name :";
puts(sname);
}
};
void main()
{
clrscr();
stud obj; // Default constructor called
obj.showinfo(); // displays the value of sno as 0 and sname as “new”
obj.getinfo(); // reads the user given value for object obj
obj.showinfo(); // displays the user given values for object obj
getch();
}
Output:
ASSIGNMENT
Problem 1: Answer the questions (i) and (ii) after going through the following program:
class Match
{int Time;
public:
Match() //Function 1
{Time=0;
cout<<”Match commences”<<end1;
}
void Details() //Function 2
{
cout<<”Inter Section Basketball Match”<<end1;
}
Match(int Duration) //Function 3
{
Time=Duration;
cout<<”Another Match begins now”<<end1;
}
Match(Match &M) //Function 4
{
Time=M.Duration;
cout<<”Like Previous Match ”<<end1;
}};
i. Which category of constructor - Function 4 belongs to and what
is the purpose of using it?
ii. Write statements that would call the member Functions 1 and 3.
class Bazar
{
char Type[20];
char Product[20];
int Qty;
float Price;
Bazar( ) //Function 1
{strcpy (Type, “Electronic”);
strcpy(Product, “Calculator”);
Qty = 10;
Price = 225;
}
public :
void Disp( ) //Function 2
{cout << Type << “-“ << Product << “:” << Qty
<< ”@” << Price << endl;
}
};
void main( )
{ Bazar B ; //Statement 1
B. Disp( ); //Statement 2
}
i. Will Statement 1 initialize all the data members for object B with the values
given in the Function 1 ? (Yes OR No). Justify your answer suggesting the
correction(s) to be made in the above code.(Hint: Based on the characteristics of
Constructor declaration)
ii. What shall be the possible output when the program gets executed?
(Assuming, if required the suggested correction(s) are made in the program).
Problem 7: Answer the questions (a) and (b) after going through the following class:
class Interview
{
int month:
public:
Interview(int y) { Month=y;}
Interview(Interview & t);
};
1. Create an object, such that it invokes Constructor 1.
2. Write complete definition for Constructor 2.
Problem 8: Answer the questions (i) and (ii) after going through the following
program:
class Match
{ int Time;
public:
Match() //Function 1
{Time=0;
cout<<”Match commences”<<endl;
}
----------------------------//Statement-1
M1.show();
M2.show();
}
Problem 10: What is default constructor? How does it differ from destructor?
Problem 11: Why is a destructor function required in classes? Illustrate with the help of
an example.
Problem 12: Answer the questions (i) and (ii) after going through the following class:
class Science
{char Topic[20];
int Weightage;
Study Material - CS-XII Chap - 5 : Constructors & Destructors 9
Study Material - CS-XII Chap - 5 : Constructors & Destructors 10
public:
Science ( ) //Function 1
{strcpy (Topic, “Optics” );
Weightage = 30;
cout<<“Topic Activated”;}
~Science( ) //Function 2
{
cout‟<<”Topic Deactivated”;
}
};
(i) Name the specific features of class shown by Function 1 and Function 2 in the above
example.
(ii) How would Function 1 and Function 2 get executed?
-----------------------
Inherits properties
from the base class
................
................
};
XII-CS-NOTES CHAPTER 6:INHERITANCE Page 1 of 7
Points to remember :-
Here transiting properties means ... transiting data members and member
functions.
Since the properties get transited into child class , hence phenomenon of
Inheritence is also known as Extending the class.
The derived class or child class is not a replica of the base class or parent
class.
The child class or derived class can have all the properties of the parent
class or base class, but it contains at least one property of its own, to
retain its identity.
All the properties of the parent class get inherited but all are not
accessible into its child class.
The accessibility of the properties of parent class into child class depends
on the Visibility mode.
Inherited variables and methods can be used in the derived class as if they
had been declared locally.
Inherited variables and methods keep their original visibility
characteristics in the subclass.
...................
.................
};
If the data members and member functions are publicly derived, then,
remember the following :
..................
....................
SUMMARY
CLASS B
Intermediate Base Class
CLASS C
3. Hierarchical Inheritance
In this type of inheritance, more than one derived classes can
be derived from one base class.
PERMANANT CONTRACTUAL
DERIVED CLASS
Containership : This means that one class is being contained in other class
i.e, the object of one class is being declared as one of the member of another
class. Such classes are known as nested classes and this relationship is known as
containership.
Output stream
Input stream:
The stream that receives or read data from a file on device and supply it to the c++ program. It is
one way data flow.
Input stream
Input/ Output streams:
The stream that sends/receives data from c++ program to/from file on device. it is two way data
flow.
of Line (EOL).Stores information in ASCII characters. In text file some translations takes place
when this EOL character is read or written.
Binary Files: Data is stored in binary form or in machine language. This data is in non-readable
form.it contains the information in the same format as it is held in the memory. In binary file there
is no delimiter for a line. Also no translation occur in binary file as a result binary files are faster
and easier for program to read and write.
1. ifstream – It provides functions to Open the already existed data file for Input and read its contents
into c++ program. Data file is opened for reading purpose only.
An object of ifstream class is to be created in the program to carry out reading operations
from the data file.
Example.
ifstream p; // statement to create object of ifstream class
Here p is the object of ifstream that will carry out respective reading operations through available
function of ifstream class.
2. ofstream – It provides functions to Open the data file for writing purpose and write or add contents
to that file from c++ program. Data file is opened for writing purpose only.
An object of ofstream class is to be created in the program to carry out writing operations
from the data file
Example.
ofstream p; // statement to create object of ofstream class
Here p is the object of ofstream that will carry out respective writing operations through available
function of ofstream class.
3. fstream – it provides functions to Open the data file (Open the file for Input /Output /both) and both
read & write operations can be done.
Data file is opened for reading /writing or both purpose.
An object of fstream class is to be created in the program to carry out reading/ writing operations
Example.
fstream p;// statement to create object of fstream class
Here p is the object of fstream that will carry out respective reading /writing operations through
available function of fstream class.
Closing a File:
A file is closed by disconnecting it with the stream it is associated with. The close() function is used to
close an opened file.
Syntax:
stream_object.close( );
out_stream.close(); //out_stream is stream object
ios::trunc If the file is opened for output operations and is already existed, its ofstream
previous content is deleted and replaced by the new one.
ios::nocreate If file does not exist this file mode ensures that no file is created and ofstream
open fails.
ios::noreplace If file does not exist, a new file gets created but if the file already Ofstream
exists, the open fails.
ios::binary Opens a file in binary mode. ofstream,
ifstream
Steps involved in creating a text file and adding text to a text file( insertion operator<<).
1. Create object of ofstream class.
ofstream file1;
2. Open a file with this object.
file1.open (“names.txt”,ios::out );
3. Write in the file (add two line of text)
file1<<”work is worship”<<endl;// use of insertion operator to send data to text file
File1<<”life is to work”<<endl;
4. Close the opened file using close() function.
file1.close();
Steps involved to open an existing text file and adding text at the end of file. (appending data)
1. Create object of ofstream class.
ofstream file1;
2. Open a file with this object.
file1.open (“names.txt”,ios::app );//open file in append mode
3. Write in the file (add two line of text)
file1<<”revise daily”<<endl;// use of insertion operator to send data to text file
File1<<”it is important”<<endl;
4. Close the opened file using close() function.
file1.close();
// Simple example to append or add data at end of a text file
#include <fstream.h>
void main()
{
ofstream File1;
File1.open("names.txt", ios::app);
File1<<”revise daily”<<endl;// use of insertion operator to send data to text file
File1<<”it is important”<<endl;
File1.close();
}
Different methods for writing in a text file.
1. getline()- It reads characters from input stream and puts them in the array pointed to by the buff until
either num charcters have been read, or character specified by delim is encountered. If not mentioned,
the default value of delim is newline character.
Syntax:
istream & getline(char * buf, int num, char delim=‟\n‟)
char arr[20];
fileInput.getline(arr, 20); //it breaks as newline character is encountered
or
fileInput.getline(arr, 20, „$‟); //it breaks as „$‟ is encountered
CHAPTER 7:DATA FILE HANDLING Page 7 of 38
XII/CS(283)/NOTES GAJENDRA S DHAMI,PGT(CS) ,LUCKNOW PUBLIC SCHOOL,SOUTH-CITY
2. Extraction operator (>>)- This operator is also used to read from a file.It is suitable for reading
word by word as >> cannot read whitespaces.
char arr[20];
fileInput >>arr; //where fileinput is an ifstream object
3. get( ) – This function is used for reading a single character or a string in a file.
fileInput.get(„c‟);//where fileinput is an ifstream object
or
char arr[10];
fileInput.get(arr, 10 );
Steps involved in reading line by line from a text file using getline( ).
1. Create object of ifstream class.
ifstream fileInput;
2. Open a file with this object.
fileInput.open (“names.txt” ,ios::in);
3. Read from the file (line by line)
char arr[20]; // declare an array to store the line of text
while(!fileInput.eof()) //loop to read all the data line by line till the end of file
{
fileInput.getline(arr,20); //read a line of text and storing in arr array.
cout<<arr; // display it on screen
}
4. Close the opened file using close() function.
fileInput.close( );
Example program:
// Simple example to read line by line from a text file from a file
#include <fstream.h>
void main()
{
ifstream fileInput;
fileInput.open(“names.txt,ios::in);
char arr[20];
while(!fileInput.eof()) //loop to read all the data line by line til the end of file
{
fileInput.getline(arr,20); //read a line of text and storing in arr array.
cout<<arr; // display it on screen
}
fileInput.close();
}
CHAPTER 7:DATA FILE HANDLING Page 8 of 38
XII/CS(283)/NOTES GAJENDRA S DHAMI,PGT(CS) ,LUCKNOW PUBLIC SCHOOL,SOUTH-CITY
Steps involved in reading word by word from a text file using >> operator.
1. Create object of ifstream class.
ifstream fileInput;
2. Open a file with this object.
fileInput.open (“names.txt” ,ios::in);
3. Read from the file (line by line)
char arr[20]; //declare an array to store a word
while(!fileInput.eof()) //loop to read all the data line by line til the end of file
{
fileInput>>arr; //read a word and storing in arr array.
cout<<arr; // display it on screen
}
4. Close the opened file using close() function.
fileInput.close();
Example program:
// Simple example to read word by word from a text file from a file
#include <fstream.h>
void main()
{
ifstream fileInput;
fileInput.open(“names.txt”,ios::in);
char arr[20];
while(!fileInput.eof( )) //loop to read all the data line by line till the end of file
{
fileInput>>arr; //read a word and storing in arr array.
cout<<arr; // display it on screen
}
fileInput.close();
}
Steps involved in reading character by character from a text file using get() function.
1. Create object of ifstream class.
ifstream fileInput;
2. Open a file with this object.
fileInput.open (“names.txt” ,ios::in);
3. Read from the file (line by line)
char ch;
while(!fileInput.eof()) //loop to read all the data till the end of file
{
fileInput.get(ch); //read a character and storing in ch variable.
cout<<ch; // display it on screen
CHAPTER 7:DATA FILE HANDLING Page 9 of 38
XII/CS(283)/NOTES GAJENDRA S DHAMI,PGT(CS) ,LUCKNOW PUBLIC SCHOOL,SOUTH-CITY
}
4. Close the opened file using close( ) function.
fileInput.close();
Example program:
// Simple example to read character by character from a text file
#include <fstream.h>
void main()
{
ifstream fileInput;
Fileinput.open(“names.txt”,ios::in);
char ch; // variable to hold a character
while(!fileInput.eof()) //loop to read all the data line by line till the end of file
{
fileInput.get(ch); //read a character and storing in ch variable.
cout<<ch; // display it on screen
}
fileInput.close();
}
SOLVED EXERCISES:
Q1. To create a File to store a string in it.
#include<fstream.h>
#include<stdio.h>
int main( )
{
char str[100];
ofstream out_obj("FileInput.txt");
cout<<"Enter Text: ";
gets(str);
cout_obj<<str;
out_obj.close();
}
Q2. Write a function in C++ to count the number of lowercase alphabets present in a text file
“BOOK.txt”.
#include<fstream.h>
#include<ctype.h>
int main()
{
char ch; int c=0;
ifstream input(“BOOK.txt”);
CHAPTER 7:DATA FILE HANDLING Page 10 of 38
XII/CS(283)/NOTES GAJENDRA S DHAMI,PGT(CS) ,LUCKNOW PUBLIC SCHOOL,SOUTH-CITY
while(!eof())
{
input.get(ch)
if(islower(ch))
c++;
}
input.close();
cout<<”No. of lowercase alphabets: ”<<c<<endl;
}
Q3. Write a function in C++ to read the content of a text file “DELHI.TXT” and display all those
lines on screen, which are either starting with „D‟ or starting with „M‟.
#include<fstream.h>
void fun()
{
char str[100];
ifstream in("ABC.txt");
while(!in.eof())
{
in.getline(str,100))
if(str[0]=='M'||str[0]=='D')
cout<<str<<endl;
}
in.close();
}
Q4.To copy the contents of a file “names.txt” into another file “new.txt”
#include <fstream.h>
void main()
{
ifstream fileInput;
ofstream fileout;
fileinput.open(“names.txt”,ios::in); // open source file in input mode or reading mode
fileout.open(“new.txt”,ios::out); // open destination file in output mode or writing mode
char ch[80];
while(!fileInput.eof( )) //loop to read all the data line by line till the end of file
{
fileInput.getline(ch,80); //read a charcter from names.txt and storing in ch variable.
fileout <<ch; // writing ch data to new.txt
}
fileInput.close();
fileout.close();}
void main( ){
Person P1 = {"SAM", 18, "male"};
Person P2 = {"Soma", 17, "female"};
ofstream outfile;
outfile.open("PERSON.dat", ios::binary | ios::out);
outfile.write((char *)&P1, sizeof(P1)); //it will write 62 bytes of person P1 data from
//beginning of file “PESON.dat”
outfile.write((char *)&P2, sizeof(P2)); // it will write 62 bytes of person P2 data after P1 data
outfile.close();
}
// The person.dat will store the following information in the form as record is represented in memory
but we can visualize in following form i.e record by record
SAM 18 male 62 bytes every record will store 62 bytes as size of object of Person is 62 bytes
Soma 17 female 124 bytes so files stores 124 bytes of data
struct Student
{
int roll;
char name[80];
};
int main()
{
Student s2;
ifstream ip_file;
ip_file.open("File1.dat", ios::binary|ios::in);
int no;
cout<<"\nEnter roll no for Searching: ";
cin>>no;
int flag=0;
while(ip_file.read((char *)&s2, sizeof(s2)))
{
if(no==s2.roll)
{
flag=1;
break;
}
}
if(flag==1)
cout<<"\nRoll found.";
else
cout<<"\nRoll Not found!";
ip_file.close();
}
Insert a record at a certain position in a binary file:
The process requires reading data from source file and copying data to a temporary data file along
with new data to be inserted.
Temporary file contains the updated data
Delete the source file and rename the temporary as source file.
step1 : create the input stream object and two record objects
student obj1,obj2={11,”anand”};
ifstream fin;
step 2:open the file by using stream object
fin.open(“file.dat”,ios::binary|ios::in);
step 3: create an output stream object and open the file “temp.dat”
ofstream fout;
CHAPTER 7:DATA FILE HANDLING Page 16 of 38
XII/CS(283)/NOTES GAJENDRA S DHAMI,PGT(CS) ,LUCKNOW PUBLIC SCHOOL,SOUTH-CITY
{
fout.write((char*)&obj2,sizeof(obj2));//insert before roll no 3 by copying it before 3 to temp.dat
}
fout.write((char*)&obj1,sizeof(obj1)); //copying current record obj1 to temp.dat
}
fin.close( );
fout.close( );
remove(“file.dat”); //statement to remove a file
rename(“temp.dat”,”file.dat”); //statement to rename a file
}
Delete a record from a binary file
The process requires reading data from source file and copying all the record to a temporary data
except the record to be deleted.
Temporary file contains the updated data
Delete the source file and rename the temporary as source file.
step1 : create the input stream object and a record object
student obj1;
ifstream fin;
step 2:open the file by using object
fin.open(“file.dat”,ios::binary|ios::in);
step 3: create an output stream object and open the “temp.dat” file
ofstream fout;
fout.open(“temp.dat”,ios::binary|ios::ios::out);
step 4:read the record one by one from “file.dat”
while(fin.read((char*)&obj1,sizeof(obj1)))
{
if(obj.rn!=11) // delete roll no 11 by not copying to temp.dat
fout.write((char*)&obj1,sizeof(obj1));//copying current record except 11 roll no
}
Step 5: Close both the files
fin.close( );
fout.close( );
Step 6: Delete the original file “file.dat”
remove(“file.dat”);//statement to remove a file
Step 7: Rename the “temp.dat” to “file.dat” as temp.dat stores is the updated file
rename(“temp.dat”,”file.dat”);//statement to rename a file
CHAPTER 7:DATA FILE HANDLING Page 18 of 38
XII/CS(283)/NOTES GAJENDRA S DHAMI,PGT(CS) ,LUCKNOW PUBLIC SCHOOL,SOUTH-CITY
void main()
{
ofstream File;
File.seekp(0);// it will move the position to beginning of file
File.seekp(sizeof(student)*5,ios::beg);//it will reposition get pointer to the end of 5th record or
//beginning of 6th record
File.seekp(-5*sizeof(student),ios::end);//it will reposition get pointer to 5 record back from end of file
File.seekp(0,ios::end); //it takes get pointer to the end of file
File.seekp(-1L*sizeof(student),ios::cur); //takes to beginning of current record(82 bytes back)
}
Exercises:
Q1.A binary file stores records of 100 students where each student have following structure:
struct student
{
int rn;
char name[80];
};
i) Now give the output of following statements:
Student T;
ifstream fin;
Fin.open(“student.dat”,ios::binary | ios::in);
CHAPTER 7:DATA FILE HANDLING Page 21 of 38
XII/CS(283)/NOTES GAJENDRA S DHAMI,PGT(CS) ,LUCKNOW PUBLIC SCHOOL,SOUTH-CITY
Fin.read((char*)&T,sizeof(T));
Fin.read((char*)&T,sizeof(T));
cout<<fin.tellg( )<<endl;
Fin.seekg(12*sizeof(T));
cout<<Fin.tellg()<<endl;
Fin.seekg(-5L*sizeof(T),ios::cur);
cout<<Fin.tellg( );
Fin.seekg(0,ios::end);
cout<<fin.tellg()/(sizeof(T)*2);
ii) Now give the output of following statements:
Student T={2”aman”};
ofstream fin;
Fin.open(“student.dat”,ios::binary | ios::out);
Fin.write((char*)&T,sizeof(T));
Fin.write((char*)&T,sizeof(T));
cout<<fin.tellp( )<<endl;
Fin.seekp(6*sizeof(T),ios::beg);
cout<<Fin.tellp()<<endl;
Fin.seekp(-1L*sizeof(T),ios::cur);
cout<<Fin.tellp( );
Fin.seekp(0,ios::end);
cout<<Fin.tellp()/sizeof(T);
Fin.seekp(0);
cout<<Fin.tellp();
strcpy(obj1.name,”mohan”);
//rewriting record to binary file
fout.write((char*)&obj1,sizeof(obj1));//it will modify the record
}
fin.close( );
fout.close( );
}
Exercise:
Q1.Write a program to create a binary file “scholar.dat” and store 5 records of scholar
And carry out following file operation using a menu bases options
Define a function to append a scholar at the end of file
Define a function to accept any name and search it if found display its information
Define a function to accept a scholar and a name , search the name if found insert the scholar after
that record
Define a function to accept a name and delete the scholar having that name
Define a function to increase the scholar per% by 5 where per% is between 28 to 32.
Define a function to display all the scholar information on screen
{
Doctor obj;
Ofstream T;
T.open(“doctor.dat”,ios::binary|ios::out);
for(int i=1;i<=4;i++)//
{
obj.enter( ); //calling public member function to initialize private data members
T.write((char*)&obj,sizeof(obj)); //write record containing through obj object
}
T.close();
}
void APP( ) // to add record at end of file
{
Doctor obj;
Ofstream T;
T.open(“doctor.dat”,ios::binary|ios::app);
obj.enter();
T.write((char*)&obj,sizeof(obj));
T.close( );
}
void READ( )
{ Doctor obj;
ifstream T;
T.open(“doctor.dat”,ios::binary|ios::in);
while(T.read((char*)&obj,sizeof(obj)))//loop breaks if no valid data left i.e T becomes null
{
obj.disp();//calling public member function to display private data members
}
T.close();
}
void main( )
{WRITE( ); //calling function to write data
READ( ); // calling function to read and display data on screen
APP( ); //calling function to add a record at end of file
}
CHAPTER 7:DATA FILE HANDLING Page 25 of 38
XII/CS(283)/NOTES GAJENDRA S DHAMI,PGT(CS) ,LUCKNOW PUBLIC SCHOOL,SOUTH-CITY
ASSIGNMENTS
Q1 Name the header file required to carry out data file operations.
Q2 Explain the difference between binary file and text file.
Q3 Give difference between :
I. ios::in and ios::out
II. ios::app and ios::ate
Q4 Give difference between get( ) and getline( ).
Q5 Give difference between seekg( ) and tellg( ).
Q6 Write statements at the given places for pre-existing binary file ”abc.dat” that stores
records of items as per following information in the .
struct item
{
int code;
char name[80];
};
fstream FIT;
__________________________//to open the file in I/O mode
__________________________//to move to beginning of 2nd last record
_____________________________// to move to beginning of file
_______________________________//to display the byte number
Q7 Define a function to create a text file “os.txt” and store name of 6 operating systems in
it.
Q8 Define a function to add two more operating system names in “os.txt”.
Q9 Define a function read “os.txt” file and return the number of operating systems that
starts with “win”.
Q10 Define a function read “os.txt” file and return the number of upper case vowels.
Q11 Define a function read “os.txt” file and return the number of words having odd number
of characters
Q12 Define a function read “os.txt” file and return the number of lines that starts with letter
‘A’.
Q13 Define a function read “os.txt” file and print the average length of line.
Q14 Define a function read “os.txt” file and print the word with maximum number of
characters.
Q15 Define a function read “os.txt” file and copy all upper case alphabets to “upper.txt” and
all lower case letters to “lower.txt”
Q16 Define a function create a binary file “doc.dat”and store 5 records where each record is
having following structure:
class doctor
{
int doc_id;
char dname[80];
float salary;
public:
void enter()
{
cin>>doc_id;
gets(dname);
cin>>salary;
}
void disp()
{
cout<<doc_id<<”\t”<<dname<<”\t”<<salary<<endl;
}
};
Q17 Define a function open binary file “doc.dat” and append 2 records where each record is
having following structure:
class doctor
{
int doc_id;
char dname[80];
long salary;
public:
void enter()
{
cin>>doc_id;
gets(dname);
cin>>salary;
}
void disp()
{
cout<<doc_id<<”\t”<<dname<<”\t”<<salary<<endl;
}
};
Q18 Define a function read the binary file “doc.dat” and count the doctors where salary is in
the range 120000-300000 and where each doctor is defined by the following class.
class doctor
{
int doc_id;
char dname[80];
long salary;
public:
void enter()
{
cin>>doc_id;
gets(dname);
cin>>salary;
}
long rt_sal(){return salary;}
void disp()
{
cout<<doc_id<<”\t”<<dname<<”\t”<<salary<<endl;
}
};
Q19 Define a function to read the binary file “doc.dat” copy all the doctors to file “elite.dat”
where specialisation is in “radiologist”.
class doctor
{
int doc_id;
char dname[80];
char spl[80]; //type of specialisation
float salary;
public:
void enter()
{
cin>>doc_id;
gets(dname);
cin>>salary;
}
int check(char a[])
{
return strcmpi(a,spl);
}
void disp()
{
cout<<doc_id<<”\t”<<dname<<”\t”<<salary<<endl;
}
};
Q20 Define a function open the binary file “doc.dat” and delete information of those doctors
where specialisation is either “neuro” or “gastro”.
class doctor
{
int doc_id;
char dname[80];
char spl[80]; //type of specialisation
float salary;
public:
void enter()
{
cin>>doc_id;
gets(dname);
cin>>salary;
}
char *rt_name()
{
return name;
}
void disp()
{
cout<<doc_id<<”\t”<<dname<<”\t”<<salary<<endl;
}
};
Q21 Define a function open the binary file “doc.dat” and insert a new doctor information at
3rd place from beginning.
class doctor
{
int doc_id;
char dname[80];
char spl[80]; //type of specialisation
float salary;
public:
void enter()
{
cin>>doc_id;
gets(dname);
cin>>salary;
}
void disp()
{
cout<<doc_id<<”\t”<<dname<<”\t”<<salary<<endl;
}
};
Q22 Define a function open the binary file “doc.dat” increase the salary of those doctors by
20% where specialisation is “neuro”.
class doctor
{
int doc_id;
char dname[80];
char spl[80]; //type of specialisation
float salary;
public:
void enter()
{
cin>>doc_id;
gets(dname);
cin>>salary;
}
char *rt_name()
{
return name;
}
void setsal(long p)
{
salary+=p;
}
void disp()
{
cout<<doc_id<<”\t”<<dname<<”\t”<<salary<<endl;
}
};
Q23 Observe the program segment carefully and answer the question that follows:
class item
{
int item_no;
char item_name[20];
public:
void enterDetail( );
void showDetail( );
int getItem_no( ){ return item_no;}
};
void modify(item x, int y )
{
fstream File;
File.open( “item.dat”, ios::binary | ios::in | ios::out) ;
item i;
int recordsRead = 0, found = 0;
while(!found && File.read((char*) &i , sizeof (i)))
{
recordsRead++;
if(i . getItem_no( ) = = y )
{
_________________________//Missing statement
File.write((char*) &x , sizeof (x));
found = 1;
}
}
if(! found)
cout<<”Record for modification does not exist” ;
File.close() ;
}
If the function modify( ) is supposed to modify a record in the file “ item.dat “, which
item_no is y, with the values of item x passed as argument, write the appropriate
statement for the missing statement using seekp( ) or seekg( ), whichever is needed, in
the above code that would write the modified record at its proper place.
Q24 Observe the program segment carefully and answer the question that follows:
class item
{
int item_no;
char item_name[20];
public:
void enterDetails( );
void showDetail( );
int getItem_no( ){ return item_no;}
};
void modify(item x )
{fstream File;
File.open( “item.dat”, _______________ ) ; //parameter missing
item i;
while(File .read((char*) & i , sizeof (i)))
{if(x . getItem_no( ) = = i . getItem_no( ))
{File.seekp(File.tellg( ) – sizeof(i));
File.write((char*) &x , sizeof (x));
}
else
File.write((char*) &i , sizeof (i));
}
File.close() ;
}
If the function modify( ) modifies a record in the file “ item.dat “ with the values
of item x passed as argument, write the appropriate parameter for the missing
parameter in the above code, so as to modify record at its proper place.
Q25 Observe the program segment given below carefully and fill the blanks marked as
Line 1 and Line 2 using fstream functions for performing the required task.
#include<fstream.h>
class Stock
{
long Ino; // Item Number
char Item[20]; // Item Name
int Qty; // Quantity
public:
void Get(int);
Get(int);// Function to enter the content
void Show( ); // Function to display the content
void Purchase(int Tqty)
{
Qty+ = Tqty; // Function to increment in Qty
}
long KnowIno( )
{
return Ino;
}
};
# include <fstream.h>
class PRODUCT
{
int Pno;
char pname [20];
int qty;
public :
void ModifyQty();// The function is to modify quantity of a PRODUCT
};
void PRODUCT :: ModifyQty ( )
{
fstream Fil;
Fil.open(“PRODUCT.DAT”,ios::binary | ios::in | ios::out);
int MPno;
cout<<“Product No to modify quantity :”;
cin>>MPno;
while( Fil.read ((char*) this, sizeof (PRODUCT)))
{
if (MPno ==Pno)
{
class EMPLOYEE
{
int ENO;
char ENAME[10];
public :
void GETIT()
{
cin >> ENO;
gets (ENAME);
}
void SHOWIT()
{
cout <<ENO << ENAME ;
cout<<endl;
}
};
Q30 Consider the class declaration:
class BUS
{
int bus_no;
char destination[20];
float distance;
public :
void Read(); // To read an object from the keyboard
void Write (); // To write an object into a file
void Show (); // To display the file contents on The monitor
};
Complete the member functions definitions.
Q31 Write a function in C++ to search for a laptop from a binary file
“LAPTOP.DAT” containing the objects of class LAPTOP (as defined below).
The user should enter the Model No and the function should search and
display the details of the laptop.
class LAPTOP
{
long ModelNo;
float RAM, HDD;
char Details[120];
public:
void StockEnter ( )
{
cin>>Model>>No>>RAM>>HDD;
gets(Details);
}
void StockDisplay( )
{cout<<ModelNo<<RAM<<HDD<<Details<<endl;
}
long ReturnModelNo ( )
{
return ModelNo ;
}
};
Q32 Write a function in C++ to search and display the details of all flights, whose
destination is “Mumbai” from “FLIGHT.DAT”. Assuming the binary file is
containing objects of class FLIGHT.
class FLIGHT
{
int Fno; //Flight Number
char From[20] ; //Flight Starting point
char To[20] ; //Flight Destination
public :
char* GetFrom( )
{
return From ;
}
char* GetTo( )
{
return To ;
}
void Enter( )
{
cin >> Fno ;
gets (From) ;
gets(To) ;
}
void Display( )
{
cout << Fno<< “:” << From << “:” << To << endl ;
}
};
Q33 A binary file “Students.dat” contains data of 10 students where each student’s data is
an object of the following class:
class Student
{
int Rno;char Name[20];
public:
void EnterData() {cin>>Rno; cin.getline(Name,20);
void ShowData() {cout<<Rno<<” - ”<<Name<<endl;}
};
With reference to this information, write output of the following program segment:
ifstream File; Student S;
File.open(“STUDENTS.DAT”,ios::binary|ios::in);
File.seekg(0, ios::end);
cout<<File.tellg();
Q34 Assuming the class Account given below and a binary file BANK.DAT contains objects of
this class, write functions in C++ to perform the followings:
(i) Deposit ( ), which deposits amount x to account number y.
(ii) Withdraw ( ), which withdraws amount x from account number y.
class Account{
int acc_no;
char name[20];
float balance;
public:
float getBalance( )
{
return balance;
}
void setBalance(float f )
{
balance = f;
}
int get_acc_no( )
{
return acc_no;
}
};
Q1 What enables a program to store the data permanently on secondary storage devices?
(a) monitor (b) printer
(c) file (d ) None of these
Q2 A collection of related data stored on some storage devices such as a hard disk,
magnetic tape or floppy disk.
(a) program (b) process
(c) file (d ) None of these
Q3 Data files can be classified as
(a) Text File (b) Binary File
(c) Both (a) & (b) (d ) None of these
Q4 The files that store data in the form of machine form are known as
(a) Text File (b) Binary File
(c) Both (a) & (b) (d ) None of these
Q5 The files that store data as strings of characters are known as
(a) Text File (b) Binary File
(c) Both (a) & (b) (d ) None of these
Q6 A flow of data in the form of a sequence of bytes is known as____
(a) word (b) string
(c) stream (d ) None of these
Q7 The stream that reads the data from the device and supplies it to the program is known
as
(a) input stream (b) output stream
(c) Both (a) & (b) (d ) None of these
Q8 The stream that receives data from the program and writes it to the device is known as
CHAPTER 7:DATA FILE HANDLING Page 36 of 38
XII/CS(283)/NOTES GAJENDRA S DHAMI,PGT(CS) ,LUCKNOW PUBLIC SCHOOL,SOUTH-CITY