Oop-Unit Iv
Oop-Unit Iv
Oop-Unit Iv
int main()
{
sum(2.3, 5.4);
return 0;
}
Class Template
The class may also employ templates, just like function templates, to make it
compatible with other data types. You may have created a dynamic array using the
vector in C++ programming, and you can see that it functions flawlessly with any
data type you pass inside the >, such as vector < int >. The class template is solely
to blame for this.
template < class T >
class className {
// Class Definition.
// We can use T as a type inside this class.
};
#include<iostream> int main()
using namespace std; {
template <class T> demo <float> ob(2.5, 2.6);
class demo{ ob.check();
T n1, n2; return 0;
public: }
demo(T a, T b)
{
n1=a;
n2=b;
}
void check()
{
if(n1>n2)
{
cout<<n1<<" is largest.";
}
else{
cout<<n2<<" is largest.";
}
}
};
File Handling
• File handling is used to store data permanently in a computer. Using file handling
we can store our data in secondary memory (Hard disk).
• For achieving file handling we need to follow the following steps:-
STEP 1-Naming a file
STEP 2-Opening a file
STEP 3-Writing data into the file
STEP 4-Reading data from the file
STEP 5-Closing a file.
Streams in C++
We give input to the executing program and the execution program gives back the
output. The sequence of bytes given as input to the executing program and the
sequence of bytes that comes as output from the executing program are called
stream.
Classes for File stream operations
1. ios:-
• ios stands for input output stream.
• This class is the base class for other classes in this class hierarchy.
• This class contains the necessary facilities that are used by all the other derived classes for input and
output operations.
2. istream:-
• istream stands for input stream.
• This class is derived from the class ‘ios’.
• This class handle input stream.
• The extraction operator(>>) is overloaded in this class to handle input streams from files to the program
execution.
• This class declares input functions such as get(), getline() and read().
3. ostream:-
• ostream stands for output stream.
• This class is derived from the class ‘ios’.
• This class handle output stream.
• The insertion operator(<<) is overloaded in this class to handle output streams to files from the
program execution.
• This class declares output functions such as put() and write().
4. streambuf:-
• This class contains a pointer which points to the buffer which is used to manage the
input and output streams.
5. fstreambase:-
• This class provides operations common to the file streams. Serves as a base for fstream,
ifstream and ofstream class.
• This class contains open() and close() function.
6. ifstream:-
• This class provides input operations.
• It contains open() function with default input mode.
• Inherits the functions get(), getline(), read(), seekg() and tellg() functions from the
istream.
7. ofstream:-
• This class provides output operations.
• It contains open() function with default output mode.
• Inherits the functions put(), write(), seekp() and tellp() functions from the ostream.
8. fstream:-
• This class provides support for simultaneous input and output operations.
• Inherits all the functions from istream and ostream classes through iostream.
Create and Write To a File
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Create and open a text file
ofstream MyFile("filename.txt");