0% found this document useful (0 votes)
44 views6 pages

Fa20-Bee-040 Oop Lab 13

This document is a lab report for CSC241 Object Oriented Programming. It summarizes the tasks completed in Lab 13, including writing code for template classes and functions to swap variables, print arrays, and implement a stack data structure. The lab helped the student learn about templates and their uses for creating generic functions and classes that can work with different data types.

Uploaded by

Abdullah king
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
0% found this document useful (0 votes)
44 views6 pages

Fa20-Bee-040 Oop Lab 13

This document is a lab report for CSC241 Object Oriented Programming. It summarizes the tasks completed in Lab 13, including writing code for template classes and functions to swap variables, print arrays, and implement a stack data structure. The lab helped the student learn about templates and their uses for creating generic functions and classes that can work with different data types.

Uploaded by

Abdullah king
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/ 6

Course: CSC241 Object Oriented

Programming

Lab 13

Bilal Saleem Ahmed

Names

FA20-BEE-040
Registration
Numbers

Class 3B (BEE)

Instructor’s Name Ma’am Asma Bibi

Lab Assessment

Post Lab Total


Pre-Lab In-Lab
Data Presentation Data Analysis Writing Style
In Lab Tasks:

Task 5.1
#include<iostream>
#include<conio.h>

using namespace std;

template <class T1>

class Test
{
public:

void swap(T1 var1, T1 var2)


{
cout << "Before swapping." << endl;
cout << "var1 = " <<var1<< ", var2= " << var2 << endl;

T1 temp = var1;
var1 = var2;
var2 = temp;

cout << "\nAfter swapping." << endl;


cout << "var1 = " <<var1<< ", var2= " << var2 << endl;

}
};

int main()
{
Test <int> t1;
Test <float> t2;
Test <char> t3;

t1.swap(9, 15);
t2.swap(18.2,25.3);
t3.swap('c','a');
return 0;
}

Output
Task 5.2
#include<iostream>

using namespace std;

template< typename T >


T printArray(const T *array, int count)
{
for (int i = 0; i < count; i++)

cout<<array[i]<<" ";
cout<<endl;

return (array[0] + array[1]) / 2;


cout<<endl;
}

int main()
{
const int size = 2;
int a[size] = { 1, 2 };
double b[size] = { 1.11, 2.23 };
char c[size] = { 'A','B'};

cout << "average of type int" << endl;


cout<< printArray<int> (a, size);

cout << endl;


cout << "average of type double" << endl;
cout<< printArray <double> (b, size);
cout << endl;
cout << "average of type char" << endl;
cout << printArray <char>(c, size);
cout << endl;
return 0;
}

Output

Post Lab Tasks:

Task 6.1

#include<iostream>
using namespace std;
template<class T>
class stack{
private:
T st[17];
int top;
public:
stack()
{
top = -1;
}
bool isempty()
{
return (top < 0);
}
void push(T var)
{
st[++top] = var;
}
T pop()
{
return st[top--];
}

};
int main()
{
stack<float> s1;
s1.push(111.1);
cout<<"1: "<<s1.pop()<<endl;
s1.isempty();
stack<int> s2;
s2.push(-1);
cout<<"2: "<<s2.pop()<<endl;
s2.isempty();
return 0;
}

Output
Critical Analysis
This lab taught us about templates and their use in programming, templates can
be used in two different ways with functions and with classes. Using a template
we can create a single function that can process any type of data. The portability
provided by a template mechanism can be extended to classes.

You might also like