0% found this document useful (0 votes)
45 views

LAB (Static +template Functions)

The document describes a C++ class named Box that defines a static member variable and static member function. It demonstrates how to declare a static member variable, initialize it, and access it via a static member function. It also shows how to declare a constructor that increments the static member variable each time an object is created. The main function creates two Box objects and prints the object count before and after to show it is incremented.

Uploaded by

Radi Sado
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

LAB (Static +template Functions)

The document describes a C++ class named Box that defines a static member variable and static member function. It demonstrates how to declare a static member variable, initialize it, and access it via a static member function. It also shows how to declare a constructor that increments the static member variable each time an object is created. The main function creates two Box objects and prints the object count before and after to show it is incremented.

Uploaded by

Radi Sado
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB (Static and Template Functions/Classes)

#include <iostream>

using namespace std;

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;

// Increase every time object is created


objectCount++;
}
double Volume() {
return length * breadth * height;
}
static int getCount() {
return objectCount;
}

private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

// Initialize static member of class Box


int Box::objectCount = 0;

int main(void) {
// Print total number of objects before creating object.
cout << "Inital Stage Count: " << Box::getCount() << endl;

Box Box1(3.3, 1.2, 1.5); // Declare box1


Box Box2(8.5, 6.0, 2.0); // Declare box2

// Print total number of objects after creating object.


cout << "Final 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:

enter two characters: a c


c
enter a,b:5 7
7
enter p,q:5.2 7.5
7.5

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:

5.2 8.7 2.3 4.5 1.1

After Sorting

1.1 2.3 4.5 5.2 8.7

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?

You might also like