0% found this document useful (0 votes)
77 views7 pages

C++ Assignment-7

The document contains 4 code examples demonstrating the use of templates in C++. The first example shows a template function to swap two variables. The second example shows a template function to calculate the sum of array elements. The third example shows a template class to perform basic calculations. The fourth example shows a template stack class with push and pop functions.

Uploaded by

Kavibharath R
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)
77 views7 pages

C++ Assignment-7

The document contains 4 code examples demonstrating the use of templates in C++. The first example shows a template function to swap two variables. The second example shows a template function to calculate the sum of array elements. The third example shows a template class to perform basic calculations. The fourth example shows a template stack class with push and pop functions.

Uploaded by

Kavibharath R
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/ 7

C++ ASSIGNMENT

1.
#include<iostream>
using namespace std;
template <class T>
void swap(T&a,T&b)      
{
    T temp=a;
    a=b;
    b=temp;
}
int main()
{
    int x1=4,y1=7;
    cout<<“Before Swap:”;
    cout<<“\nx1=”<<x1<<“ty1=”<<y1;
    swap(x1,y1);
    cout<<“\nAfter Swap:”;
    cout<<“\nx1=”<<x1<<“ty1=”<<y1;
    return 0;
}
Output:
Before swap:
x1=4 y1=7
After swap:
x1=7 y1=4
2.
#include<iostream>
using namespace std;
template<class T>
int sum(T arr[20],T start,T stop,T initial_val)
{
T s=0;
arr[start]=initial_val;
for(T i=start;i<=stop;i++)
s+=arr[i];
cout<<"The initial value:"<<arr[start];
return s;
};
int main()
{
int arr[20],start,stop,initial_val,s;
cout<<"\nEnter the starting index of array: ";
cin>>start;
cout<<"\nEnter the ending index of array: ";
cin>>stop;
cout<<"\nEnter the arrray elements: ";
for(int i=start;i<=stop;i++)
{
cin>>arr[i];
}
cout<<"\nEnter the optional initial value:";
cin>>arr[start];
initial_val=arr[start];
s=sum(arr,start,stop,initial_val);
cout<<"\nSum is:"<<s;
return 0;
}
Output:
Enter the starting index of array: 0
Enter the ending index of array: 5
Enter the arrray elements:
0
2
4
6
8
10
Enter the optional initial value:2
The initial value:2
Sum is:32
3.
#include <iostream>
using namespace std;
template <class T>
class Calculator
{
private:
T num1, num2;
public:
Calculator(T n1, T n2)
{
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 << "Division is: " << divide() << endl;
}
T add() { return num1 + num2; }
T subtract() { return num1 - num2; }
T multiply() { return num1 * num2; }
T divide() { return num1 / num2; }
};

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:
Int results:
Numbers are: 2 and 1
Addition is: 3
Subtraction is: 1
Product is: 2
Division is: 2
Float results:
Numbers are: 2.4 and 1.2
Addition is: 3.6
Subtraction is: 1.2
Product is: 2.88
Division is: 2

4.
#include <iostream>
using namespace std;
template<typename T>
class Stack
{
private:
T a;
public:
void push()
{
cout<<"Data is pushed\n";
}
void pop()
{
cout<<"Data is poped\n";
}
};
int main()
{
Stack<int> s1;
s1.push();
s1.pop();
return 0;
}

Output:
Data is pushed
Data is poped

You might also like