Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

UNIT- IV

Templates and Generic


Programming
Generic Programming in C++
The generic programming pattern generalizes the algorithm with the help of
templates in C++ so that it can be used along with different data types. In templates,
we specify a placeholder instead of the actual data type, and that placeholder gets
replaced with the data type used during the compilation. So, if the template function
were being called for integer, character, and float, the compiler would produce 3
copies of the function. This is possible because of the static-type nature of C++.
Use of Templates
• In the C++ programming language, all data must be stored in a container.
Technically, these are termed as data types like int, float, user-defined, etc.
• As we know, the algorithm could be the same for different types of data, i.e., the
procedure to find the distance between two coordinates will remain the same
irrespective of whether the coordinates are given as integer or floating-point
numbers. But in C++ Programming, we must write different functions for both
data types because the int data type is incapable of storing float values, and using
the float data type for int values is a waste of memory. So, Templates in C++ solve
this problem by providing a generalized algorithm.
Function Template
The function templates are used to write functions with generic types that can adapt
functionality according to the data type used during the function call. This makes us
easier to perform the same operation on different data types without code
replication.
#include<iostream>
using namespace std;

template <class T>


T sum(T a, T b)
{
cout<<"Sum="<<a+b;
}

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");

// Write to the file


MyFile << "Files can be tricky, but it is fun enough!";

// Close the file


MyFile.close();
}
Read a File
#include <iostream> // Create a text string, which is used to output the
#include <fstream> text file
#include <string> string myText;
using namespace std;
// Read from the text file
int main () { ifstream MyReadFile("filename.txt");
// Create a text file
ofstream MyWriteFile("filename.txt"); // Use a while loop together with the getline()
function to read the file line by line
// Write to the file while (getline (MyReadFile, myText)) {
MyWriteFile << "Files can be tricky, but it is fun // Output the text from the file
enough!"; cout << myText;
}
// Close the file
MyWriteFile.close(); // Close the file
MyReadFile.close();
}

You might also like