new (4)
new (4)
Exception handling
#include <iostream>
if (b == 0) {
return a / b;
int main()
{ int a,b;
cout<<"Enter numerator:
"; cin>>a;
cin>>b;
try {
return 0;
}
FILE:
OUTPUT:
Reading from a file
#include <iostream>
#include <fstream>
#include <string>
using namespace
std;
int main() {
ifstream file("reading.txt");
string s;
while (getline(file,
endl;
file.close();
return 0;
}
OUTPUT:
FILE:
Writing to a file
#include <iostream>
#include <fstream>
#include <string>
using namespace
std;
int main() {
ofstream File("writing.txt");
File.close();
cout << "Data has been written to the file." << endl;
return 0;
}
Output:
Function template for two numbers
#include <iostream>
using namespace
std;
T result =
a+b; return
result;
int main()
int i =2;
int j =3;
float m =
2.3; float n =
1.2;
:"<<add(m,n); return 0;
}
Output:
Class template for two numbers
#include <iostream>
using namespace
std; template<class
T> class A
public:
T num1 =
5; T num2
= 6; void
add()
};
int main()
A<int> d;
d.add();
return 0;
}
Output:
Using Vectors
#include <iostream>
#include <vector>
using namespace
std;
int main() {
numbers.push_back(60);
return 0;
}
INDEX
11 • Exception handling
• Reading from a file
• Writing to a file 12/11/2024