Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

OOP Lab Assgn 9

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

Q1)

#include <iostream>
using namespace std;
#define N 5

template <class T>

void sort(T arr[], int SIZE)


{
for (int i = 0; i < SIZE; i++)
{
for (int j = i + 1; j < SIZE; j++)
{
if (arr[i] > arr[j])
{
T temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}

int main()
{
int int_array[N];
float float_array[N];
cout << "Enter integer array elements:" << endl;
for (int i = 0; i < N; i++)
{
cin >> int_array[i];
}
cout << "Enter floating array elements:" << endl;
for (int i = 0; i < N; i++)
{
cin >> float_array[i];
}
sort(int_array, N);
sort(float_array, N);
cout<<"Sorted integer array:"<<endl;
for (int i = 0; i < N; i++)
{
cout << int_array[i] << ", ";
}
cout << endl;

cout<<"Sorted floating number array:"<<endl;


for (int i = 0; i < N; i++)
{
cout << float_array[i] << ", ";
}
return 0;
}

Q2)
#include<iostream>
using namespace std;

template<class T1, class T2>


void display(T1 a, T2 b)
{
cout<<a<<endl<<b;
}

int main()
{
int a=20;
char b='B';

display(a,b);

return 0;
}

Q3)
#include <iostream>
using namespace std;
const int N = 5;

template <class Type>


class Array{
private:
Type arr[N];
public:
void read(){
for(int i = 0; i < N; i++){
cin>>arr[i];
}
}
void sortArr(){
Type temp;
for(int i = 0; i < N - 1; i++){
for(int j = i + 1; j < N; j++){
if(arr[i] < arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
void display(){
for(int i = 0; i < N; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
};

int main(){
Array <int> i_obj;
Array <float> f_obj;

cout<<"Enter integer array:";


i_obj.read();

cout<<"Enter floating number array:";


f_obj.read();

i_obj.sortArr();
f_obj.sortArr();

cout<<"Sorted integer array:"<<endl;


i_obj.display();

cout<<endl<<"Sorted floating number array:"<<endl;


f_obj.display();

return 0;
}
Q4)
#include<iostream>
using namespace std;

template<class T1, class T2>


class Test
{
T1 a;
T2 b;
public:
Test(T1 x, T2 y)
{
a = x;
b = y;
}
void show()
{
cout << a << " and " << b << endl;
}
};

int main()
{
Test <float, int> test1 (1.23, 123);
Test <int, char> test2 (100, 'W');

test1.show();
test2.show();

return 0;
}

Q5)
#include <iostream>
#include <stdexcept>

using namespace std;

float Division(float num, float den)


{
if (den == 0) {
throw runtime_error("Math error: Attempted to divide by Zero\n");
}
return (num / den);
}

int main()
{
float numerator, denominator, result;
numerator = 12.5;
denominator = 0;

try {
result = Division(numerator, denominator);

cout << "The quotient is "<< result << endl;


}

catch (runtime_error& e) {
cout << "Exception occurred" << endl << e.what();
}
return 0;
}

Q6)
#include <iostream>
using namespace std;

class Arr
{
int *a, size;

public:
Arr(int n)
{
size = n;
a = new int[size];
cout << "Array size = " << size << endl;
}
void input()
{
cout << "Input for Array " << endl;
int i = 1, j = 0;
while (1)
{
try
{
cout << "Enter value, -1 to end :";
cin >> i;
if (i == -1)
break;
else if (j >= size)
throw -1;
else
a[j++] = i;
}
catch (int x)
{
cout << "Exception Caught (ARRAY SIZE FULL) " << x << endl;
break;
}
}
}
};
int main()
{
Arr obj(6);
obj.input();
return 0;
}
Q7)
#include<iostream>
#include<conio.h>

using namespace std;

void test(int x) {
try {
if (x > 0)
throw x;
else
throw 'x';
} catch (int x) {
cout << "Catch a integer and that integer is:" << x<<endl;
}
catch (char x) {
cout << "Catch a character and that character is; " << x<<endl;
}
}

int main()
{
cout << "Testing multiple catches\n:";
test(10);
test(0);

return 0;
}

You might also like