Ra2311047010216 Oodp Assignment
Ra2311047010216 Oodp Assignment
Purvi Wasan
AI D SECTION
OODP ASSIGNMENT
int main() {
int arrInt[] = {7, 2, 9, 4, 3};
float arrFloat[] = {3.5, 1.2, 4.8, 0.9};
sortItems(arrInt, nInt);
cout << "After Sorting int
array: "; printArray(arrInt,
nInt);
sortItems(arrFloat, nFloat);
cout << "After Sorting float
array: ";
printArray(arrFloat, nFloat);
return 0;
}
OUTPUT -
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;
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 -
CODE -
#include <iostream>
class
ArithmeticOperations {
T a, b;
public:
T add() { return a + b; }
T subtract() { return a - b; }
T multiply() { return a
* b; } T divide() {
if (b != 0)
return a / b;
else
};
int main() {
ArithmeticOperations<int> intObj;
OUTPUT -
EXCEPTION HANDLING
CODE -
#include <iostream>
if (b == 0)
throw "Division by
zero!"; return a / b;
}
int main() {
int x, y;
try {
endl;
return 0;
OUTPUT -
CODE -
#include <iostream>
int main() {
try {
int a
= 10;
int b = 0;
if (b == 0)
<< endl;
} catch (...) {
return 0;
OUTPUT -
3. Write a C++ program and use the following in built functions
CODE - #include
<iostream>
#include
<stdexcept>
using namespace
std;
int main() {
try {
}
try {
<< endl;
return 0;
OUTPUT -
CODE -
#include <iostream>
#include <stdexcept>
int main() {
try {
try {
return 0;
OUTPUT -
7. Write a C++ program to demonstrate re throw exception within
exception handler.
CODE -
#include <iostream>
void throwError() {
try {
throw;
int main() {
try {
throwError();
return 0;
OUTPUT -