100% found this document useful (1 vote)
41 views4 pages

EXP-12 Class Template

The document explains the concept of templates in C++, which allow for the creation of generic functions and classes, promoting code reusability. It includes examples of function templates and class templates, demonstrating how to define and use them with different data types. Additionally, it provides a sample program that sorts arrays of integers and floats using class templates.

Uploaded by

shiv.prasad2049
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
41 views4 pages

EXP-12 Class Template

The document explains the concept of templates in C++, which allow for the creation of generic functions and classes, promoting code reusability. It includes examples of function templates and class templates, demonstrating how to define and use them with different data types. Additionally, it provides a sample program that sorts arrays of integers and floats using class templates.

Uploaded by

shiv.prasad2049
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

12.

Understanding of Template in C++


 The templates are one of the most powerful and widely used methods added to C++, allowing us
to write generic programs. It allow us to define generic functions and classes. It promote generic
programming, meaning the programmer does not need to write the same function or method
for different parameters.
 We can define a template as a blueprint for creating generic classes and functions. The idea
behind the templates in C++ is straightforward. We pass the data type as a parameter, so we
don’t need to write the same code for different data types.
 Types of Templates in C++
 As we know, we can use templates in C++ to define generic functions and classes. We can
represent templates in C++ in two different ways, namely - function templates and class
templates.

Example : Function Template


#include <iostream>
using namespace std;

// Template function that will be adding two data.


template <typename T>
T add(T a, T b)
{
return (a + b);
}

// Main function
int main()
{
// Variables to store results of different data types.
int ans1;
double ans2;

// Calling template function with int parameters.


ans1 = add<int>(2, 2);
cout << "Sum of 2 + 2 is: " << ans1 << endl;

// Calling template function with double parameters.


ans2 = add<double>(2.5, 3.5);
cout << "Sum of 2.5 + 3.5 is: " << ans2 << endl;

return 0;
}
Output:
Sum of 2 + 2 is: 4
Sum of 2.5 + 3.5 is: 6
Example 2: Class template
#include <iostream>
using namespace std;

// Declaring a template class named Test.


template <class T>
class Test
{
private:
// A variable (answer) of type T so that it can store results of various types.
T answer;

public:
// Constructor of Test class.
Test(T n) : answer(n)
{
cout << "Inside constructor" << endl;
}

T getNumber()
{
return answer;
}
};

// Main function
int main()
{
// Creating an object with an integer type.
Test<int> numberInt(60);

// Creating an object with double type.


Test<double> numberDouble(17.27);

// Calling the class method getNumber with different data types:


cout << "Integer Number is: " << numberInt.getNumber() << endl;
cout << "Double Number = " << numberDouble.getNumber() << endl;

return 0;
}
Output:
Inside constructor
Inside constructor
Integer Number is: 60
Double Number = 17.27
Example 3: Write a program using class template to arrange N numbers of type int and float
in descending order.

#include <iostream>
using namespace std;
const int N = 7;

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;
int SIZE = sizeof(arr)/sizeof(Type);
for(int i = 0; i < SIZE - 1; i++){
for(int j = i + 1; j < SIZE; j++){
if(arr[i] < arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
void display(){
int SIZE = sizeof(arr)/sizeof(Type);
for(int i = 0; i < SIZE; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
};

int main(){
Array <int> i_obj;
Array <float> f_obj;
//reading integer array
cout<<"Enter integer array:";
i_obj.read();
//reading floating number array
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;
}

Sample run:
Enter integer array:3
66
5
88
2
1
77
Enter floating number array:4.4
55.55
99.77
66.66
33.33
22.2
80.0
Sorted integer array:
88 77 66 5 3 2 1

Sorted floating number array:


99.77 80 66.66 55.55 33.33 22.2 4.4

You might also like