C++ Notes (Unit-4,5)
C++ Notes (Unit-4,5)
(C++)
Virendra Sir
(Scholars Education)
UNIT-4
Inheritance
Inheritance is one of the key features of Object-oriented programming in C++. It
allows user to create a new class (derived class, sub class, child class) from an
existing class (base class, super class, Parent class).
The derived class inherits all the features from the base class and can have
additional features of its own.Syntax
class derived-class: access-specifier base-class
The process of obtaining the data members and methods from one class to
another class is known as inheritance. It is one of the fundamental features of
object-oriented programming.
Important points
In the inheritance the class which is give data members and methods is known
as base or super or parent class.
The class which is taking the data members and methods is known as sub or
derived or child class.
Syntax
class subclass_name : superclass_name
{
// data members
// methods
}
The real life example of inheritance is child and parents, all the properties of father are
inherited by his son.
Diagram
In the above diagram data members and methods are represented in broken line are
inherited from faculty class and they are visible in student class logically.
Advantage of inheritance
In order to inherit the feature of base class into derived class we use the following
syntax
Syntax
Explanation
classname-1 and classname-2 represents name of the base and derived classes
respectively.
: is operator which is used for inheriting the features of base class into derived
class it improves the functionality of derived class.
class employee
{
public:
int salary;
};
class developer : public employee
{
employee e;
public:
void salary()
{
cout<<"Enter employee salary: ";
cin>>e.salary; // access base class data member
cout<<"Employee salary: "<<e.salary;
}
};
void main()
{
clrscr();
developer obj;
obj.salary();
getch();
}
Output
Based on number of ways inheriting the feature of base class into derived class it
have five types they are:
1) Single inheritance
2) Mult level inheritance
3) Multiple inheritance
4) Hierarchical inheritance
5) Hybrid inheritance
1) Single inheritance
In single inheritance there exists single base class and single derived class.
In which one class access the property another class.
Program)
//inheritance ( single inheritance Example )
#include<iostream.h>
#include<conio.h>
class derived : public base // base class is derived publicly in derived class
{ // single derived class
public:
void show2()
{
cout<<"Show2"<<endl;
}
};
int main()
{
derived d; // Derived class object
getch();
return 0;
}
Output
class B:A
{
Int p,q,r;
public:
void mul()
{
cout<<"enter two number=";
Output
Note
Private member cannot be accessed outside the class
Protected member can be accessed child class but not outside the class and not
in main() function.
When a base class is privately inherit derived class than public member of base
class become private member of derive of class.
Program) A public member of class base will be inaccessible to the object of derived
class.
#include<iostream.h>
class B:A
{
public:
void display()
{
sum();
}
};
void main()
{
clrscr();
B o;
o.display();
getch();
}
Output
class B:public A
{
public:
void display()
{
sum();
}
TC Business School | BEST BCA College In JAIPUR 10
};
void main()
{
clrscr();
B o;
o.display();
o.sum();
getch();
}
Output
2) Multilevel inheritances
It is the inheritance hierarchy wherein subclass acts as a base class for other
classes.
In this when sub classes (B) inherit a base class (A) in this situation A become
parent class of B. And again class B inherited by class C in this situation class B
become parent class for class c.
The transitive nature of inheritance is reflected by multilevel inheritance.
Syntex
TC Business School | BEST BCA College In JAIPUR 11
class A
{
……..
…….
};
Class B:A
{
……….
………
};
Class C:B
{
..........
…….
};
Program)
#include<iostream.h>
#include<conio.h>
class A
{
inta,b,c;
protected:
void sum()
{
cout<<"Enter two number=";
cin>>a>>b;
c=a+b;
cout<<"Sum="<<c<<endl;
}
};
class B:A
{
intp,q,r;
protected:
void mul()
{
sum();
cout<<"Enter no=";
cin>>p>>q;
r=p*q;
}
};
class C:B
{
intg,h,i;
public:
void divide()
{
mul();
cout<<endl;
cout<<"Enter two number";
cin>>g>>h;
i=g/h;
cout<<"divide="<<i;
}
};
void main()
{
clrscr();
C o;
o.divide();
getch();
}
Output
Program)
#include<iostream.h>
#include<conio.h>
class derived1 : public base // base class is derived publicly in derived class
{ // first derived class
public:
void show2()
{
cout<<"Show2"<<endl;
}
};
class derived2 :public derived1 // second derived class from first derived class
{
public:
void show3()
{
cout<<"Show3"<<endl;
}
};
int main()
{
derived2 d; // Derived2 class object
getch();
return 0;
}
3) Multiple inheritance
It is the inheritance one derived class inherits from multiple base class.
When a sub class inherits more than one base class this situation is known as
multiple inheritance.
#include<iostream.h>
#include<conio.h>
int main()
{
derived d; // Derived class object (derived class derived from two base
classes)
getch();
return 0;
}
Output
Prog)
#include<iostream.h>
#include<conio.h>
class derived1 : public base // first derived class from base class
{
public:
void show2()
{
cout<<"Show2"<<endl;
}
};
class derived2 : public base // second derived class from base classs
{
public:
int main()
{
clrscr();
derived1 d1; // Derived1 class object
derived2 d2; // Derived2 class object
getch();
return 0;
}
Output
int main()
{
TC Business School | BEST BCA College In JAIPUR 19
clrscr();
Rectangle rect;
Triangle tri;
rect.set_data(5,3);
tri.set_data(2,5);
cout<<rect.area()<<endl;
cout<<tri.area()<<endl;
getch();
return 0;
}
Output
5) Hybrid inheritance
The inheritance hierarchy that reflects any legal combination of other four types
of inheritance.
A structure which is created by using two or more inheritance combination of two
inheritances is known as hybrid inheritance.
class base2
{
public:
void show4()
{
cout<<"Show4"<<endl;
}
};
class derived1 : public base1,public base2 // first derived1 class from base1 and
base2 class
{ // multiple inheritance
public:
void show2()
{
cout<<"Show2"<<endl;
}
};
int main()
{
clrscr();
derived2 d;
d.show1();
d.show2();
d.show3();
d.show4();
getch();
return 0;
}
Output
Program-1)
class derived : private base // base class is derived privately in derived class
{
// protected and public members of base become private here
public:
int c;
derived()
{
c=30;
};
int main()
{
//derived d;
//cout<<d.a; // a and b of base are not accessible here directly
//cout<<d.b;
//cout<<d.c; // public member c of derived class can be accessed
directly here
sub aa;
getch();
return 0;
}
Output
#include<iostream.h>
#include<conio.h>
class base
{
int a;
protected:
int b;
public:
base() //Base class constructor
{
a=10;
b=20;
cout<<a<<" "<<b;
}
};
class derived : protected base // base class is derived protectedly in derived class
{
// protected and public members of base become protected here
public:
int c;
derived() // Derived class constructor
{
//a=50; // private member a can not be accessed in derived class
c=30;
cout<<" "<<c<<" ";
b=40; // protected member b can be accessed in derived class
cout<<" "<<b<<" ";
}
};
getch();
return 0;
}
Prog-3)
//inheritance ( public derivation of base class in derived class )
#include<iostream.h>
#include<conio.h>
class base
{
int a;
protected:
int b;
public:
base() //Base class constructor
{
a=10;
b=20;
cout<<a<<" "<<b;
}
};
class derived : public base // base class is derived publicly in derived class
{
// protected will remain protected here ,
public: // public will remain public here
int c;
derived() // Derived class constructor
{
//a=50; // private member a can not be accessed in derived class
c=30;
cout<<" "<<c<<" ";
b=40; // protected member b can be accessed in derived class
cout<<" "<<b<<" ";
}
};
getch();
return 0;
}
Output
Prog-4)
//inheritance ( Difference between private and protected data member of the class )
#include<iostream.h>
#include<conio.h>
class base
{
int a;
protected:
int b;
public:
void fun()
{
a=10;
b=20;
cout<<a<<" "<<b<<" ";
}
};
TC Business School | BEST BCA College In JAIPUR 27
class derived : public base // base class is derived publicly in derived class
{
public:
void show()
{
a=30; // private member a is not accessed in derived function ,
b=40; // but protected member b can be accessed in derived class
function
//cout<<a<<" ";
cout<<b<<" ";
}
};
int main()
{
derived d; // Derived class object
d.fun(); // base class function fun() is called with derived class object
d.show(); // derived class function show() is called with derived object
getch();
return 0;
}
Prog-5)
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
derived d;
d.show();
Output
Prog-6)
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
derived2 d;
d.show();
Output
#include<iostream.h>
#include<conio.h>
class base
{
public:
base() // Base class constructor
{
cout<<"Base class constructor"<<endl;
}
~base() // Base class destructor
{
cout<<"Base class Destructor"<<endl;
}
};
int main()
{
{
derived d;
}
getch();
return 0;
}
Output
1) Function overloading
More than one function with same name but parameter is different.
In case of function overloading we can have two or more than two function by the
same name in the same class but the list of argument should be different.
Example:
#include<iostream.h>
#include<conio.h>
class A
{
void main()
{
clrscr();
A ob1;
ob1.fun(10);
obj1. fun(10,20);
getch();
}
Result:
2) Operating overloading
TC Business School | BEST BCA College In JAIPUR 33
To define additional task to an operator, we must specify the relation of operator with
class this is done by a special function called operatorfunction which describe the
task. The general syntax of operator function
return_typeclass_name:: operator op(argument_list)
{
function _body;
}
Another way
return_type operator op(argument_list)
{
//body of operator function
}
Note
void main()
{
clrscr();
number N;
N.setdata(5);
N.show();
-N; //invoke operator-() function
Result:
Program) WAP to increase and decrease an object’s value using unary++ and –
operator.
#include<iostream.h>
#include<conio.h>
class number
{
int x;
public:
number(int a) //constructor
{
x=a;
}
void show()
{
cout<<"value of x="<<x<<endl;
}
void operator++() //overloading unary ++operator
{
x=x+1;
Result:
Program)
//Operator overloading Example to overload - operator as a unary operator
#include<iostream.h>
#include<conio.h>
class oprminus
TC Business School | BEST BCA College In JAIPUR 37
{
inta,b,c;
public:
oprminus(ints,intt,int u)
{
a=s;
b=t;
c=u;
}
void showabc()
{
cout<<a<<" "<<b<<" "<<c<<endl;
}
};
int main()
{
oprminus o1(2,3,4);
cout<<"Value of a,b and c before function call :";
o1.showabc();
-o1; // operator - function will be called
cout<<"Value of a,b and c after function call :";
o1.showabc();
getch();
return 0;
}
Output
#include<iostream.h>
#include<conio.h>
class unary
{
int a;
public:
unary()
{
a=5;
}
void show()
{
cout<<a<<endl;
}
};
int main()
{
unary o;
cout<<"Value of a before function call :";
o.show();
Output
Operator Keyword
The keyword operator facilitates overloading of the c++ operators. The general format of
operator overloading
ReturnType operator operatorSymbol(arg)
The keyword operator indicates that the operator symbol following it, the c++ operator to
be overloading to operate on members of its class. The operator overloaded in a class
is known as overloaded operator function.
Program)
#include<iostream.h>
#include<conio.h>
class oprplus
{
int a;
public:
oprplus()
{
a=5;
}
oprplus(int s)
{
a=s;
}
int main()
{
getch();
return 0;
}
Output
Program)
//Operator overloading Example to overload - operator
#include<iostream.h>
#include<conio.h>
class oprminus
{
int a;
public:
oprminus()
{
a=5;
}
oprminus(int s)
{
a=s;
}
int main()
{
oprminus o1,o2(7),o3;
cout<<"o1.a :";
o1.show();
cout<<"o2.a :";
o2.show();
o3=o1-o2; // calls the operator function defined for + operator
cout<<"o3.a :";
o3.show();
getch();
return 0;
}
Output
Program)
#include<iostream.h>
#include<conio.h>
class opr
{
int a;
public:
opr()
{
a=5;
};
int main()
{
opr o1,o2(4);
o1=o2;
getch();
return 0;
}
Output
Program)
#include<iostream.h>
#include<conio.h>
class opr
{
public:
void operator<<(opr& o)
void operator<<(int c)
{
cout<<"overloading of << on passing a value to the operator function"<<endl;
}
void operator>>(int d)
{
cout<<"Overload >> operator";
}
};
int main()
{
opr o1,o2;
o1<<o2;
o1<<4;
o1>>5;
getch();
return 0;
}
Output
Program)
// overload + using object+int and int+object
#include<iostream.h>
#include<conio.h>
class opr
{
public:
void operator+(int c)
{
};
void operator+(ints,opr& o)
{
cout<<"integer + object"<<endl;
}
int main()
{
clrscr();
opr o;
o+10; //object + integer ang integer is passed
10+o; // this will not work without making operator function as a friend of
the class
getch();
return 0;
}
Output
3) Virtual function
If there is member function with same name in base class and derived class then
virtual functions give programming capability to call member function of different
class by a same function calling. This feature is known as ‘runtime
polymorphism” or “data binding”.
Program)
#include<iostream.h>
#include<conio.h>
class super
{
public:
void display()
{
cout<<"display super";
}
virtual void show()
Result:
void main()
{
A *p;
B ob;
clrscr();
p=&ob;
p->show();
TC Business School | BEST BCA College In JAIPUR 50
getch();
}
Output
Program)
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void show() // show() function of base
{
cout<<"Show base"<<endl;
}
};
getch();
return 0;
}
Output
Program)
#include<iostream.h>
#include<conio.h>
class x
{
public:
virtual void fun()
{
cout<<"Base fun()"<<endl;
}
};
class y:public x
{
public:
class z:public x
{
public:
};
int main()
{
x *bp,ob;
y ob1;
z ob2;
bp=&ob;
bp->fun();
bp=&ob1;
bp->fun();
bp=&ob2;
bp->fun();
getch();
return 0;
}
Output
Program)
#include<iostream.h>
#include<conio.h>
class B:public A
{
int y;
public:
B()
{
y=2;
}
void show()
{
cout<<"value of y="<<y;
TC Business School | BEST BCA College In JAIPUR 54
}
};
int main()
{
clrscr();
A *p;
B ob;
p=&ob;
p->show();
getch();
return 0;
}
Output
Program)
#include<iostream.h>
#include<conio.h>
class y:public x
{
public:
void show()
{
TC Business School | BEST BCA College In JAIPUR 55
cout<<val<<endl;
}
};
class z:public x
{
public:
void show()
{
cout<<val<<endl;
}
};
class w:public x
{
public:
void show()
{
cout<<val<<endl;
}
};
int main()
{
clrscr();
y o1;
z o2;
w o3;
//x o4;
o1.setval(10);
o1.show();
o2.setval(20);
o2.show();
o3.setval(30);
o3.show();
getch();
return 0;
}
Output
File Management: Handling data files (sequential and random), Opening and closing of
files, stream state member functions, operations on files, Templates, Exception
Handling.
Exception Handling
Introduction
An exception is a problem that arises during the execution of a program. In journal a
program must have bugs like
1) Logical error
2) Syntactic error
The logical error occur to poor understanding of the problem and the syntactic error
arise due to poor understanding of the language but when there is a problem in a
program other than logical or syntactic syntax error. There are known as exception.
Exception is run time or unusual conditions that a problem may execute. Some
condition such as division by zero, error index out of bounds exception or running out of
memory or disk space.
An exception is run-time error. When doing exception handling it consist of three
things
1) The detecting of a run-time error.
2) Raising an exception in response to the error, and
3) Taking corrective action.
In C++ for exception handling there is block syntax. For example
try
{
//In try block the code which is monitor for the exception is kept.
throw exception;
Exceptions provide a way to transfer control from one part of a program to another. C++
exception handling is built upon three keywords: try, catch, and throw.
1) try: A try block identifies a block of code for which particular exceptions will be
activated. It is followed by one or more catch blocks.
You can list down multiple catch statements to catch different type of exceptions in
case your try block raises more than one exception in different situations.
try
{
// protected code
}catch( ExceptionName e1 )
{
// catch block
}catch( ExceptionName e2 )
{
// catch block
}catch( ExceptionName eN )
{
// catch block
}
Program-1)
#include<iostream>
class Excep
{
double a,b,c;
void div()
{
try
{
if(b!=0)
{
c=a/b;
cout<<"Division a/b : "<<c;
}
else
throw b;
}
catch(double x)
{
cout<<"value of b should not be zero....";
}
}
};
int main()
{
Excep e;
e.getab();
e.div();
return 0;
}
Result:
class test
{
int num;
public:
void checkno(int n)
{
num=n;
try
{
if(n>10)
throw 1;
else
cout<<"Number is less than 10 ";
}
catch(int e)
{
cout<<"You should enter a number less than 10...";
}
}
};
int main()
{
int no;
test o;
cout<<"Enter any number :";
cin>>no;
o.checkno(no);
return 0;
}
Result:
#include<iostream>
using namespace std;
class EvenOdd
{
int no,num[10];
double avg,sum;
public:
void checkno(int n)
{
no=n;
try
{
if(no%2==0)
{
getarr();
getavg();
}
else
{
throw 1;
}
}
catch(int i)
{
cout<<"Enter any even number to calculate average......";
}
}
void getarr()
{
cout<<"Enter "<<no<<" elements of array :"<<endl;
for(int i=0;i<no;i++)
{
cin>>num[i];
}
}
void getavg()
{
sum=0.0;
for(int i=0;i<no;i++)
{
int main()
{
EvenOdd o;
int n;
cout<<"Enter any number :";
cin>>n;
o.checkno(n);
return 0;
}
Result:
Program-4)
// Exception Handling ( Multiple catch Block )
#include<iostream>
using namespace std;
class Excep
{
int size,arr[];
public:
void getarr()
{
cout<<"Enter size of array :";
showarr();
}
if(size>5)
throw 1;
if(size<1)
throw 'a';
}
catch(int x)
{
cout<<"Size should not be greater than 5";
}
catch(char y)
{
cout<<"Size should not be less than 1";
}
}
void showarr()
{
cout<<"Array elements are :";
for(int i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
}
};
int main()
{
Excep e;
e.getarr();
return 0;
}
Program-5)
// Exception Handling ( Default catch block )
#include<iostream>
using namespace std;
class Excep
{
int eid;
int age;
public:
void getdata()
{
cout<<"Enter Employee ID :";
cin>>eid;
cout<<"Enter Age :";
cin>>age;
try
{
if(eid==5)
throw 1;
else
cout<<"Valid Employee id ,";
if(age==20)
throw 'c';
else
cout<<"Valid age ";
TC Business School | BEST BCA College In JAIPUR 65
}
catch(int i)
{
cout<<"Please Enter Eid other than 5";
}
catch(...) // Default catch block with elipses (...)
{
cout<<"Please Enter age other than 20";
}
}
};
int main()
{
Excep e;
e.getdata();
return 0;
}
Result:
Program-6)
// Exception Handling ( Base - Derived concept in Exception Handling )
#include<iostream>
using namespace std;
class base
{
public:
};
};
int main()
{
Derived d; // Derived class object
try
{
throw d; // throw the derived class object
}
catch(Derived d)
{
cout<<"Derived type exception ";
}
catch(base b)
{
cout<<"Base type Exception ";
}
return 0;
}
Result:
Function Template
There are several functions which have to be used with different data type. The
limitation of such function is that they operate only on a particular data type it can be
overcomes by defining that function as a function template. A function template
specifies how an individual function can be constructed.
The general form of a template function definition is shown here:
template<class T>
returntype functionname(arguments of type T)
{
//body of function
// with type T
//…
}
We must use the template parameter T as and when necessary in the function body
and in its arguments list.
Result:
Program)
#include<iostream.h>
#include<conio.h>
template<class T1,class T2>
void display(T1 a, T2 b)
{
cout<<a<<" "<<b<<endl;
}
void main()
{
clrscr();
display(2016,"Virendra education society");
getch();
TC Business School | BEST BCA College In JAIPUR 70
}
Result:
class test
{
T1 a;
T2 b;
public:
test(T1 x,T2 y)
{
a=x;
b=y;
}
void show()
{
cout<<a<<" and "<<b<<"\n";
}
};
int main()
{
test <float,int> test1(1.23,123);
test <int,char> test2(100,'w');
Result:
Program-2)
//Function Template Example
#include<iostream.h>
#include<conio.h>
template<class T>
T fun(T x)
{
return x*x;
}
int main()
{
int a=fun(2);
double b=fun(3.4);
cout<<a<<endl;
Program-3)
//Overloading template function
#include<iostream.h>
#include<conio.h>
This chapter we will see how C++ programmers can create, open, close test or binary
files for their data storage.
(0, 1) Bit
Directory
When ever there is a need to handle large volume of the data, we store the data into the
file on the disk and we read when ever necessary a file is a place on the disk where a
group of related data is store.
Definition
Type of File
1) Text File
It is a file that store information in ASCII character. In which every line is
terminating with special character know as EOF.
2) Binary File
It is a file that contains information in same formation as it is held in
memory. There is no need to terminate binary file.
Function Description
1) ifstream It is used to read a data file. Where i means input (for read) and f
means file.
Ifstream means Input file stream class.
Open() is a member function of class ifstream.
Inherited function of ifstream class from the class istream
are
get()
getline()
read()
3) fstream read write both from a file. fstream class to apply both read
write.
It supports files for simultaneous input and output.
fstream is derived from iostream class.
Member functions of class fstream are:
open()
close()
Assignment
Program-1)
//File Handling ( writing - reading to the file ) use of open() to open a file
#include<iostream>
#include<fstream> // header file for file input - output operations
using namespace std;
class FileEx
{
char ch[30]; // character array to hold the data from file
fstream f; // file stream object
public:
void writeToFile() // Function to write to the file
{
f.open("D:\\File1",ios::out); // open a file in writing mode in E drive
f<<"Hello how are you ?"; // write line of text to the file through stream
f.close(); // close the stream
}
void showFromFile() // Function to read characters from file
{
f.open("D:\\File1",ios::in); // open file in reading mode
TC Business School | BEST BCA College In JAIPUR 79
while(f) // while file pointer goes to end of file
{
f.getline(ch,30); // getline function will get the maximum 30
characters and store it in ch array variable
cout<<ch; // display text on the screen
}
f.close(); // close() will close the association of file to stream
}
};
int main()
{
FileEx f; // class FileEx object
f.writeToFile(); // calls writeToFile() function
f.showFromFile(); // calls showFromFile() function
return 0;
}
Result:
Program-2)
//File Handling ( writing - reading to the file ) use of constructor to open file
//File Handling ( writing - reading to the file ) use of open() to open a file
#include<iostream>
#include<fstream> // header file for file input - output operations
using namespace std;
class FileEx
{
char ch[30]; // character array to hold the data from file
public:
void writeToFile() // Function to write to the file
{
fstream f("E:\\File2",ios::out); // file is opened in writing mode using
constructor
f<<"Hello how are you ?"; // write line of text to the file through stream
f.close(); // close the stream
}
void showFromFile() // Function to read characters from file
{
fstream f("E:\\File2",ios::in); // open file in reading mode
TC Business School | BEST BCA College In JAIPUR 80
while(f) // while file pointer goes to end of file
{
f.getline(ch,30); // getline function will get the maximum 30
characters and store it in ch array variable
cout<<ch; // display text on the screen
}
f.close(); // close() will close the association of file to stream
}
};
int main()
{
FileEx f; // class FileEx object
f.writeToFile(); // calls writeToFile() function
f.showFromFile(); // calls showFromFile() function
return 0;
}
Result:
Program-3)
//File Handling ( writing - reading to the file ) use of open() to open a file
#include<iostream>
#include<fstream> // header file for file input - output operations
using namespace std;
//File Handling ( writing - reading to the file ) use of ifstream and ofstream class
class FileEx
{
char ch; // character to get and put the data from file
fstream f;
public:
void writeToFile() // Function to write to the file
{
f.open("D:\\File4",ios::out); // ofstream class object for output operations
//f<<"Hello how are you ?"; // write line of text to the file through stream
cout<<"Enter text :";
Result:
Program-4)
//File Handling ( writing - reading to the file ) use of open() to open a file
#include<iostream>
#include<fstream> // header file for file input - output operations
using namespace std;
//File Handling ( writing - reading to the file ) use of ifstream and ofstream class
class FileEx
{
char ch; // character to get and put the data from file
fstream f;
public:
Result:
Program-7)
//File Handling ( writing - reading to the file ) use of open() to open a file
#include<iostream>
TC Business School | BEST BCA College In JAIPUR 83
#include<fstream> // header file for file input - output operations
using namespace std;
//A program to check if the file exist if not then the file is created
int main()
{
char ch[30];
fstream f;
f.open("E:\\f.txt",ios::in);
f.close();
if(!f)
{
f.open("E:\\f.txt",ios::out);
cerr<<"New File created..";
f.close();
}
else
{
f.close();
}
return 0;
}
Result:
Program-7)
//A program which copies a file from one location to another
//File Handling ( writing - reading to the file ) use of open() to open a file
int main()
{
fstream f1,f2;
char ch[30];
f1.open("d:\\file3.txt",ios::out);
f1<<"hello how are you?";
f1.close();
f2.open("e:\\file4.txt",ios::out);
f1.open("d:\\file3.txt",ios::in);
while(f1)
{
f1.getline(ch,50);
f2<<ch;
}
if(f1.eof())
cout<<"File copied";
f1.close();
f2.close();
return 0;
}
Result: