Object Oriented Programming: More C++ Programs on Templates
More
Programs on Templates
Example:
Write a
function template for finding the minimum value contained in array.
Solution :
#include<iostream>
using namespace std;
template <class T>
T min(T a[10])
{
T min;
min=a[0];
for(int
i=0;i<5;i++)
{
if(a[i]
<<min)
{
min=a[i];
}
}
return min;
}
int main()
{
int a[10];
int i;
for(i=0;i<5;i++)
{
cout<<"Enter
the integer values "<<endl;
cin>>a[i];
}
cout<<"\n
The minimum element of an array is: "<<min(a)<<endl;
float b[10];
for(i=0;i<5;i++)
{
cout<<"Enter
the real values "<<endl;
cin>>b[i];
}
cout<<"\n
The minimum element of an array is: "<<min(b)<<endl;
return 0;
}
Output
Enter the integer
values
10
Enter the integer
values
4
Enter the integer
values
11
Enter the integer
values
5
Enter the integer
values
12
The minimum element of
an array is: 4
Enter the real values
10.10
Enter the real values
5.5
Enter the real values
11.11
Enter the real values
6.6
Enter the real values
7.7
The minimum element of
an array is: 5.5.
Example:2
Write a
C++ program using class template for finding the scalar product for int type
vector and float type vector.
Solution :
#include<iostream>
using namespace std;
template <class T>
class Scalar
{
Ta1, a2,a3,b1,b2,b3;
T ans;
public:
void GetData()
{
cout<<"\n
Enter the value of a bar: "<<endl;
cout<<"\n
Enter a1_ i:
cin>>a1;
cout<<"\n
Enter a2_j: ";
cin>>a2;
cout<<"\n
Enter a3_k: ";
thobby
cin>>a3;
cout<<"\n
Enter the value of b bar: "<<endl;
cout<<"\n
Enter b1_i: ";
cin>>b1;
cout<<"\n
Enter b2_j: ";
cin>>b2;
cout<<"\n Enter b3_k: ";
cin>>b3;
}
T Mul()
{
T A,B,C;
cout<<"\n
The scalar product is: "<<endl;
A=a1*b1;
B=a2*b2;
C=a3*b3;
ans=A+B+C;
return ans;
}
};
int main()
{
Scalar <int> obj1;
Scalar <float> obj2;
cout<<"\n Enter int type values"<<endl;
obj1.GetData();
cout<<obj1.Mul()<<endl;
cout<<"\n Enter float type values"<<endl;
obj2.GetData();
cout<<obj2.Mul()<<endl;
return 0;
}
Output
Enter int type values
Enter the value of a
bar:
Enter a1_i: 1
Enter a2_j: 2
Enter a3_k: 3
Enter the value of b
bar:
Enter b1_i: 4
Enter b2_j: 5
Enter b3_k: 6
The scalar product is:
32
Enter float type
values
Enter the value of a
bar:
Enter a1 i: 1.1
Enter a2_j: 2.2
Enter a3_k: 3.3
Enter the value of b
bar:
Enter b1_i: 4.4
Enter b2 j: 5.5
Enter b3 k: 6.6
The scalar product is:
38.72
Example:3
Develop a
program in C++ to implement linear search of an array (Array can be of any
type).
Solution:
/**********************************************************************
Program to implement Linear search using the class template
**********************************************************************/
#include<iostream>
#include<cstring>
using namespace std;
template<class T>
class LinSearch
{
T Array[10];
T key;
public:
LinSearch(T k){ key=k;}
void Get();
T Find();
};
template<class T>
void LinSearch<T>::Get()
{
int i;
for(i=0;i<=5;i++)
{
cout<<"\n
Enter the element: ";
cin>>Array[i];
}
}
template<class T>
T LinSearch<T>::Find()
{
Get();
for(int
i=0;i<=5;i++)
if(Array[i]==key)
return key;
return ‒1;
}
void main()
{
LinSearch <int> obj1(10);
LinSearch <double> obj2(10.10);
cout<<"\n\t Handling the integer
data..."<<endl;
if(obj1.Find()>=0)
cout<<"The
element is present in the list"<<endl;
else
cout<<"The
element is not present"<<endl;
cout<<"\n\t Handling the double data...";
if(obj2.Find()>=0)
cout<<"The
element is present in the list"<<endl;
else
cout<<"The
element is not present"<<endl;
}
Output
Handling the integer
data...
Enter the element: 12
Enter the element: 11
Enter the element: 10
Enter the element: 3
Enter the element: 33
Enter the element: 5
The element is present
in the list
Handling the double
data...
Enter the element: 1.1
Enter the element: 2.2
Enter the element:
10.10
Enter the element: 4.5
Enter the element: 32.23
Enter the element: 7
The element is present
in the list.
Object Oriented Programming: Chapter 5: Templates and Exception Handling : Tag: Oops, Computer Programming : - More C++ Programs on Templates
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs) - Laboratory
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation