Name: Muhammad Hamza Amin Reg No: 191862 Course: Object Oriented Programming Submitted To: Miss Wajiha
Name: Muhammad Hamza Amin Reg No: 191862 Course: Object Oriented Programming Submitted To: Miss Wajiha
Lab #11
Objective:
In this lab we will discuss template functions and classes and their use
in C++.
Lab Tasks:
Task1:
Write a program to swap two data using template functions. Run the code
Source code:
#include <iostream>
T temp;
temp = n1;
n1 = n2;
n2 = temp;
int main()
int i1 = 6, i2 = 3;
cout << "i1 = " << i1 << "\ni2 = " << i2;
cout << "\nf1 = " << f1 << "\nf2 = " << f2;
cout << "\nc1 = " << c1 << "\nc2 = " << c2;
Swap(i1, i2);
Swap(f1, f2);
Swap(c1, c2);
cout << "i1 = " << i1 << "\ni2 = " << i2;
cout << "\nf1 = " << f1 << "\nf2 = " << f2;
cout << "\nc1 = " << c1 << "\nc2 = " << c2;
return 0;
Output:
Task 2:
Write a program to find the maximum among two data types using
Source code:
#include<iostream>
return(n1>n2)?n1:n2;
int main()
int i1,i2;
float f1,f2;
char c1,c2;
cin>>i1>>i2;
cout<<Large(i1,i2)<<"is Larger"<<endl;
cin>>f1,f2;
cout<<Large(f1,f2)<<"is Larger"<<endl;
return 0;
Output:
Task 3:
Create a template class Calculator that performs simple calculations like + ,
-, * , / .
Source code:
#include<iostream>
class Calculator
private:
T num1,num2;
public:
num1=n1;
num2=n2;
void displayResult()
cout<<"Numbers are:"<<num1<<"and"<<num2<<"."<<endl;
cout<<"Addition is:"<<add()<<endl;
cout<<"Subtraction is:"<<subtract()<<endl;
cout<<"Product is:"<<multiply()<<endl;
cout<<"Divison is:"<<divide()<<endl;
}
T add()
T subtract()
T multiply()
T divide()
};
int main()
Calculator<int>intCalc(2,1);
Calculator<float>floatCalc(2.4,1.2);
cout<<"Int results:"<<endl;
intCalc.displayResult();
cout<<endl<<"Float results:"<<endl;
floatCalc.displayResult();
return 0;
Output:
Conclusion: