Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

LAB 13

1. Wap to demonstrate the use of function template.


Code:
#include <iostream>
using namespace std;
template <typename T>
T add(T num1, T num2)
{
return (num1 + num2);
}
int main()
{
int result1;


double result2;


result1 = add<int>(5, 6);


cout << "2 + 3 = " << result1 << endl;
result2 = add<double>(5.2, 6.3);
cout << "2.2 + 3.3 = " << result2 << endl;
return 0;
}
2. WAP to demonstrate the use of template class
Code:
#include <iostream>
using namespace std;
template <class T>
class Number

{


private:
T num;
public:
Number(T n) : num(n) {}
T getNum()
{
return num;
}

};


int main()
{
Number<int> numberInt(12);
Number<double> numberDouble(3.7);
cout << "int Number = " << numberInt.getNum() << endl;
cout << "double Number = " << numberDouble.getNum() << endl;
return 0;
}
Lab-14
1. Wap to write data in a file.
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()

{


ofstream file("myfile.txt");


string text = "Hey everyone this is my program in file handling";


file << text << endl;


file.close();


return 0;
}
2. WAP to read data from a file.
Code:
#include<iostream>
#include<fstream>
#include<stdio.h>
using namespace std;
int main()

{


char fileName[30], ch;


fstream fp;


cout<<"Enter the Name of File: ";


gets(fileName);
fp.open(fileName, fstream::in);
if(!fp)
{

cout<<"\nError Occurred!";
return 0;
}
cout<<"\nContent of "<<fileName<<":-\n";
while(fp>>noskipws>>ch)
cout<<ch;
fp.close();
cout<<endl;
return 0;
}
Lab 15
1. Wap to demonstrate the use of simple exception handling.
Code:
#include<iostream>
using namespace std;

int main()


{
int nu, de, res;


cout<<"Enter numerator and denominator";

cin>>nu>>de;


try

{
if(de == 0)
{
throw(de);
}
else
{
cout<<"Result = "<<res;
}

}


catch(int i)
{
cout<<"Divide by zero exception occured zero";
}
}

2. WAP to read data from a file.


Code:
#include<iostream>
#include<fstream>
#include<stdio.h>
using namespace std;
int main()

{


char fileName[30], ch;


fstream fp;
cout<<"Enter the Name of File: ";
gets(fileName);
fp.open(fileName, fstream::in);
if(!fp)


{

cout<<"\nError Occurred!";
return 0;
}
cout<<"\nContent of "<<fileName<<":-\n";
while(fp>>noskipws>>ch)
cout<<ch;
fp.close();
cout<<endl;
return 0; }

You might also like