LAB (Static +template Functions)
LAB (Static +template Functions)
#include <iostream>
class Box {
public:
static int objectCount;
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void) {
// Print total number of objects before creating object.
cout << "Inital Stage Count: " << Box::getCount() << endl;
return 0;
}
1. Declare the integer static data member ’oldestWeightDate’. Create the static member
function ‘changeOldestWeightDate’ with ‘void’ return type and a single integer parameter.
Set ‘oldestWeightDate’ to that parameter. Do not forget a function prototype for
‘changeOldestWeightDate’. Also, be sure to define the static member at global scope.
class ZooAnimal
{
private:
string name;
int cageNumber;
int weightDate;
int weight;
public:
ZooAnimal (string, int, int, int); // constructor function
void changeWeight (int pounds);
string reptName ();
int reptWeight ();
int daysSinceLastWeighed (int today);
};
2. Write a c++ program to find maximum of two data items using function template
Pseudo code:
T max(T a,T b)
begin
if (a>b)
return a
else
return b
end
Output:
3. Write a class template to represent a generic vector. Include member functions to perform
the following task
a. To create a vector
b. Sort the elements in ascending order
c. Display the vector
Pseudo code:
array
T *a;
int n;
void getdata()
void putdata()
void sort( )
getdata()
begin
int i
print ”enter how many no:”
input n
set a=new T[n]
for i=0 to n by 1
do
print”enter a number:”
input a[i]
end for
end
putdata()
begin
for i=0 to n by 1
do
Print a[i]
end for
end
sort( )
begin
T k;
int i,j;
for i=0 to n by 1
do
for j=0 to n by 1
do
if (a[i]>a[j])
do
set k=a[i]
set a[i]=a[j]
set a[j]=k
end if
end for
end for
end
Output:
enter how many no:5
enter a number:
5 8 2 4 1
After Sorting
1 2 4 5 8
enter a number:
After Sorting
4. Write a c++ program to find the data type of 5 data items inserted by the user (using a
single function template)?
Output:
1 1.1 a A
Int, Float, Char, Char
VIVA QUESTIONS
1. Define Template.
2. What is meant by Function Template.
3. Write the syntax for Function template?
4. What is use of Template?