American International University Bangladesh (AIUB)
Faculty of science & Technology
Department of Computer Science
LAB MANUAL 09
CSC 2207 Programming Language 2 [EEE]
TITLE
Templates class and File Handling in C++
PREREQUISITE
• To know about user define class
• To know about library class
• To know about pointers
OBJECTIVE
• To get a basic idea input/output in files and template class in C++.
THEORY
Templates are used to prevent us from writing the same function or class separately for different
data types. We normally use templates in large programs where we have to define the same
function or class for different data types.
In this example, we defined a separate function to calculate the sum of three sets of numbers,
each with a different data type.
#include <iostream>
using namespace std;
int sum( int x, int y)
{
return x + y;
}
float sum( float x, float y)
{
return x + y;
}
double sum( double x, double y)
{
return x + y;
}
int main()
{
cout << "Sum : " << sum(3, 5) << endl;
cout << "Sum : " << sum(3.0f, 5.2f) << endl;
cout << "Sum : " << sum(3.24234, 5.24144) << endl;
return 0;
}
Function using template
#include <iostream>
using namespace std;
template <typename T>
T sum( T x, T y)
{
return x + y;
}
int main()
{
cout << "Sum : " << sum(3, 5) << endl;
cout << "Sum : " << sum(3.0f, 5.2f) << endl;
cout << "Sum : " << sum(3.24234, 5.24144) << endl;
return 0;
}
Class Templates
#include <iostream>
using namespace std;
template <class T>
class Student
{
T marks1;
T marks2;
public:
Student( T m1, T m2 )
{
marks1 = m1;
marks2 = m2;
}
T totalMarks()
{
return marks1 + marks2;
}
};
int main()
{
Student<int> s1 ( 45, 50 );
Student<float> s2 ( 47.5, 56.4 );
cout << "Total marks of student1 : " << s1.totalMarks() << endl;
cout << "Total marks of student2 : " << s2.totalMarks() << endl;
return 0;
}
File i/o in C++
• File is used to store data.
• fstream is another c++ standard library that is used to read and write on files.
• These are the data types used for file handling from the fstream library:
• Ofstream: It is used to create files and write on files.
• Ifstream: It is used to read from files.
Write to file
#include <iostream>
#include <fstream>
using namespace std;
int main(){
//ofstream file;
//file.open("example.txt");
//another way
ofstream file("example.txt");
file<<"Hello World"<<endl;
file<<"Line two"<<endl;
return 0;
}
Read from file
#include <iostream>
#include <fstream>
using namespace std;
int main(){
string s1;
ifstream file("example.txt");
while (getline (file,s1) )
{
cout << s1 << '\n';
}
file.close();
return 0;
}
Reading and writing on a file
• We use << and >> to write and read from a file respectively. Let's see an example.
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char text[200];
fstream file;
file.open ("example1.txt", ios::out | ios::in );
cout << "Write text to be written on file." << endl;
cin.getline(text, sizeof(text));
// Writing on file
file << text << endl;
// Reding from file
file >> text;
cout << text << endl;
//closing the file
file.close();
return 0;
}
Write Object to file using operator overloading
#include<iostream>
#include<fstream>
using namespace std;
class Student{
public:
int id;
string name;
friend ofstream & operator <<(ofstream &ofs, Student &s);
friend ifstream & operator >>(ifstream &ifs, Student &s);
};
ofstream & operator <<(ofstream &ofs, Student &s){
ofs<<s.id<<endl;
ofs<<s.name<<endl;
return ofs;
ifstream & operator >>(ifstream &ifs, Student &s){
ifs>>s.id;
ifs>>s.name;
return ifs;
int main(){
Student s1;
s1.id = 1001;
s1.name="Richard";
ofstream ofs("Student.txt");
ofs<<s1;
ofs.close();
ifstream ifs("Student.txt");
Student s;
ifs>>s;
cout<<s.id<<" "<<s.name<<endl;
}
LAB WORK/ ASSIGNMENT
1. C++ program to print unique words in a file.
2. Write a C++ program that read student information from text file. Hints: Use student class
with member’s variables and member functions.