You are on page 1of 19

OODP WEEK 10

QUESTION 1
#include <iostream>
using namespace std;

template <typename T> T myMax(T x, T y)


{
return (x > y) ? x : y;
}

int main()
{
cout << myMax<int>(3, 7) << endl; // Call myMax
for int
cout << myMax<double>(3.0, 7.0)
<< endl; // call myMax for double
cout << myMax<char>('g', 'e')
<< endl; // call myMax for char
return 0;
}

QUESTION 2
#include <iostream>
using namespace std;

template <class T>


class Calculator {
private:
T num1, num2;

public:
Calculator(T n1, T n2) {
num1 = n1;
num2 = n2;
}
void displayResult() {
cout << "Numbers: " << num1 << " and " << num2
<< "." << endl;
cout << num1 << " + " << num2 << " = " << add() <<
endl;
cout << num1 << " - " << num2 << " = " <<
subtract() << endl;
cout << num1 << " * " << num2 << " = " <<
multiply() << endl;
cout << num1 << " / " << num2 << " = " << divide()
<< endl;
}

T add() { return num1 + num2; }


T subtract() { return num1 - num2; }
T multiply() { return num1 * num2; }
T divide() { return num1 / num2; }
};

int main() {
Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);

cout << "Int results:" << endl;


intCalc.displayResult();

cout << endl


<< "Float results:" << endl;
floatCalc.displayResult();

return 0;
}

QUESTION 3
#include <iostream>
using namespace std;

template <typename T>


void Swap(T &n1, T &n2)
{
T temp;
temp = n1;
n1 = n2;
n2 = temp;
}

int main()
{
int i1 = 6, i2 = 3;
float f1 = 7.2, f2 = 4.5;
char c1 = 'p', c2 = 'x';

cout << "Before passing data to function


template.\n";
cout << "i1 = " << i1 << "\ni2 = " << i2;
cout << "\nf1 = " << f1 << "\nf2 = " << f2;
cout << "\nc1 = " << c1 << "\nc2 = " << c2;

Swap(i1, i2);
Swap(f1, f2);
Swap(c1, c2);

cout << "\n\nAfter passing data to function


template.\n";
cout << "i1 = " << i1 << "\ni2 = " << i2;
cout << "\nf1 = " << f1 << "\nf2 = " << f2;
cout << "\nc1 = " << c1 << "\nc2 = " << c2;

return 0;
}
QUESTION 4
#include <iostream>
using namespace std;

template <class T>


inline T square(T x)
{
T result;
result = x * x;
return result;
};
int main()
{
int i, ii;
float x, xx;
double y, yy;

i = 2;
x = 2.2;
y = 2.2;

ii = square<int>(i);
cout << i << ": " << ii << endl;

xx = square<float>(x);
cout << x << ": " << xx << endl;
yy = square<double>(y);
cout << y << ": " << yy << endl;

yy = square(y);
cout << y << ": " << yy << endl;
return 0;
}
QUESTION 5
#include <iostream>
using namespace std;

template <class T>


inline T square(T x)
{
T result;
result = x * x;
return result;
};
int main()
{
int i, ii;
float x, xx;
double y, yy;

i = 2;
x = 2.2;
y = 2.2;

ii = square<int>(i);
cout << i << ": " << ii << endl;

xx = square<float>(x);
cout << x << ": " << xx << endl;

yy = square<double>(y);
cout << y << ": " << yy << endl;

yy = square(y);
cout << y << ": " << yy << endl;
return 0;
}

QUESTION 6
#include <iostream>
using namespace std;

template<class t1,class t2>


void sum(t1 a,t2 b)
{
cout<<"\nSum="<<a+b<<endl;
}
int main()
{
int a,b;
float x,y;
cout<<"\nEnter two integer data: ";
cin>>a>>b;
cout<<"\nEnter two float data: ";
cin>>x>>y;
sum(a,b);
sum(x,y);
}

QUESTION 8
#include <iostream>
using namespace std;
template <class T>
T Large(T n1, T n2)
{
return (n1 > n2) ? n1 : n2;
}

int main()
{
int i1, i2;
float f1, f2;
char c1, c2;

cout << "Enter two integers:\n";


cin >> i1 >> i2;
cout << Large(i1, i2) <<" is larger." << endl;
return 0;
}
QUESTION 9
#include<iostream>
using namespace std;

template<class Test> Test FindLarge(Test num1,Test


num2, Test num3)
{
if(num1>=num2)
{
if(num1>=num3)
return num1;
else
return num3;
}
else
{
if(num2 >= num3)
return num2;
else
return num3;
}
}
int main()
{
int num1, num2, num3, large;

cout << "\n Enter Three Numbers";


cout<<"\n --------------------------";
cout<<"\n First Number : ";
cin>>num1;

cout<<"\n Second Number : ";


cin>>num2;

cout<<"\n Third Number : ";


cin>>num3;

large=FindLarge(num1,num2,num3);
cout<<"\n Largest Number is : "<<large;

return 0;
}

QUESTION 10
#include <bits/stdc++.h>
using namespace std;

// Function to calculate square


void square(int a)
{
cout << "Square of " << a
<< " is " << a * a
<< endl;
}

// Function to calculate square


void square(double a)
{
cout << "Square of " << a
<< " is " << a * a
<< endl;
}

// Driver Code
int main()
{
// Function Call for side as
// 9 i.e., integer
square(9);

// Function Call for side as


// 2.25 i.e., double
square(2.25);
return 0;
}

QUESTION 11
#include<iostream>
using namespace std;

int search(int ar[], int size)


{
// Extreme cases
if (ar[0] != 1)
return 1;
if (ar[size - 1] != (size + 1))
return size + 1;

int a = 0, b = size - 1;
int mid;
while ((b - a) > 1) {
mid = (a + b) / 2;
if ((ar[a] - a) != (ar[mid] - mid))
b = mid;
else if ((ar[b] - b) != (ar[mid] - mid))
a = mid;
}
return (ar[a] + 1);
}

int main()
{
int ar[] = { 1, 2, 3, 4, 5, 6, 8 };
int size = sizeof(ar) / sizeof(ar[0]);
cout << "Missing number:" << search(ar, size);
}

You might also like