EXP-12 Class Template
EXP-12 Class Template
// Main function
int main()
{
// Variables to store results of different data types.
int ans1;
double ans2;
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;
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);
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;
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