RA2311047010216
Purvi Wasan
AI D SECTION
OODP ASSIGNMENT
C++ PROGRAMS USING CLASS AND FUNCTION TEMPLATES
1. Write a Function Template in C++ to perform sorting of n items. The
items can be of type int, float, etc.
CODE -
#include <iostream>
using namespace std;
template <typename T>
void sortItems(T arr[], int
n) { for (int i = 0; i <
n-1; i++) { for (int j
= i+1; j < n; j++) {
if (arr[i] > arr[j]) {
// Swap the elements if they are in wrong order
T temp =
arr[i]; arr[i]
= arr[j];
arr[j] = temp;
}
}
}
}
template <typename T>
void printArray(T arr[],
int n) { for (int i = 0;
i < n; i++) cout
<< arr[i] << " "; cout
<< endl;
}
int main() {
int arrInt[] = {7, 2, 9, 4, 3};
float arrFloat[] = {3.5, 1.2, 4.8, 0.9};
int nInt = sizeof(arrInt) / sizeof(arrInt[0]);
int nFloat = sizeof(arrFloat) / sizeof(arrFloat[0]);
cout << "Before Sorting int array: ";
printArray(arrInt, nInt);
sortItems(arrInt, nInt);
cout << "After Sorting int
array: "; printArray(arrInt,
nInt);
cout << "Before Sorting float array: ";
printArray(arrFloat, nFloat);
sortItems(arrFloat, nFloat);
cout << "After Sorting float
array: ";
printArray(arrFloat, nFloat);
return 0;
}
OUTPUT -
2. Write a program to define the function template for swapping two
items of the various. data types such as integer, float and characters.
CODE -
#include <iostream>
using namespace std;
template <typename
T> void swapItems(T
&a, T &b) {
T temp =
a; a = b;
b = temp;
}
int main() { int x
= 5, y = 10; float
p = 3.14, q = 6.28;
char c1 = 'A', c2 = 'B';
cout << "Before swapping (int): x = " << x << ", y = " << y
<< endl; swapItems(x, y);
cout << "After swapping (int): x = " << x << ", y = " << y << endl;
cout << "Before swapping (float): p = " << p << ", q = " << q
<< endl; swapItems(p, q);
cout << "After swapping (float): p = " << p << ", q = " << q <<
endl;
cout << "Before swapping (char): c1 = " << c1 << ", c2 = " << c2
<< endl; swapItems(c1, c2);
cout << "After swapping (char): c1 = " << c1 << ", c2 = " << c2
<< endl;
return 0;
}
OUTPUT -
4. Write a program to illustrate how to define and declare a class
template for reading two data items from the keyboard and to find
their sum.
CODE -
#include <iostream>
using namespace std;
template <typename T>
class AddTwoItems {
T a, b;
public:
void read() {
cout << "Enter two values: ";
cin >> a >> b;
}
T sum() {
return a + b;
}
};
int main() {
AddTwoItems<int> intObj;
intObj.read();
cout << "Sum of integers: " << intObj.sum() << endl;
AddTwoItems<float> floatObj;
floatObj.read();
cout << "Sum of floats: " << floatObj.sum() << endl;
return 0;
}
OUTPUT -
6. Ram and shiva are working as accountants in bank. They need to
know all the arithmetic operations to verify the accounts. Since they
are weak in mathematics, they found difficulty in doing such arithmetic
operations. Help them to check accounts by applying arithmetic
operations including add, subtract, multiply and divide using class
template.
CODE -
#include <iostream>
using namespace std;
template <typename T>
class
ArithmeticOperations {
T a, b;
public:
void read() { cout
<< "Enter two values: ";
cin >> a >> b;
T add() { return a + b; }
T subtract() { return a - b; }
T multiply() { return a
* b; } T divide() {
if (b != 0)
return a / b;
else
return 0; // Avoid divide by zero error
};
int main() {
ArithmeticOperations<int> intObj;
intObj.read(); cout << "Sum: " <<
intObj.add() << endl; cout <<
"Difference: " << intObj.subtract() << endl;
cout << "Product: " << intObj.multiply()
<< endl; cout << "Quotient: " <<
intObj.divide() << endl; return 0;
OUTPUT -
EXCEPTION HANDLING
1. Write a C++ program to demonstrate throw an exception for division
by zero condition.
CODE -
#include <iostream>
using namespace std;
int divide(int a, int b) {
if (b == 0)
throw "Division by
zero!"; return a / b;
}
int main() {
int x, y;
cout << "Enter two numbers: ";
cin >> x >> y;
try {
cout << "Result: " << divide(x, y) << endl;
} catch (const char* msg) {
cout << "Error: " << msg <<
endl;
return 0;
OUTPUT -
2. Write a C++ program to catch default exception for any given
condition.
CODE -
#include <iostream>
using namespace std;
int main() {
try {
int a
= 10;
int b = 0;
if (b == 0)
throw 0; // Default exception for
demonstration cout << "Result: " << a / b
<< endl;
} catch (...) {
cout << "Default exception caught!" << endl;
return 0;
OUTPUT -
3. Write a C++ program and use the following in built functions
std::logic_error and std::domain_error.
CODE - #include
<iostream>
#include
<stdexcept>
using namespace
std;
int main() {
try {
throw logic_error("Logic error occurred");
} catch (logic_error &e)
{ cout << "Caught: " <<
e.what() << endl;
}
try {
throw domain_error("Domain error occurred");
} catch (domain_error &e) {
cout << "Caught: " << e.what()
<< endl;
return 0;
OUTPUT -
4. Write a C++ program and use the following in built functions
std::out_of_range and std::length_error.
CODE -
#include <iostream>
#include <stdexcept>
using namespace std;
int main() {
try {
throw out_of_range("Out of range error");
} catch (out_of_range &e)
{ cout << "Caught: " <<
e.what() << endl;
try {
throw length_error("Length error occurred");
} catch (length_error &e)
{ cout << "Caught: " <<
e.what() << endl;
return 0;
OUTPUT -
7. Write a C++ program to demonstrate re throw exception within
exception handler.
CODE -
#include <iostream>
using namespace std;
void throwError() {
try {
throw "Initial exception"; } catch (const
char* msg) { cout << "Caught: " << msg
<< ". Rethrowing..." << endl;
throw;
int main() {
try {
throwError();
} catch (const char* msg)
{ cout << "Caught in main: " <<
msg << endl;
return 0;
OUTPUT -