[go: up one dir, main page]

0% found this document useful (0 votes)
40 views4 pages

Oop Lab-8

The document contains three C++ programs that illustrate different aspects of templates in C++. The first program demonstrates a basic template class. The second program shows a class template with multiple parameters. The third program exhibits member function templates.

Uploaded by

Harshini Mannava
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)
40 views4 pages

Oop Lab-8

The document contains three C++ programs that illustrate different aspects of templates in C++. The first program demonstrates a basic template class. The second program shows a class template with multiple parameters. The third program exhibits member function templates.

Uploaded by

Harshini Mannava
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/ 4

Exercise -8 (Templates)

a) Write a C++ Program to illustrate template class

#include <iostream.h>

#include <conio.h>

template<class T>

class Data

public: Data(T c)

cout<<"\n Value = "<<c;

cout<<"\tSize = "<<sizeof(c)<<" byte(s)";

};

int main()

clrscr();

Data <char> c('A');

Data <int> i(100);

Data <double> f(23.45);

getch();

return 0;

b) Write a Program to illustrate class templates with multiple parameters

#include <iostream.h>

#include<conio.h>

template <class T>

class Calculator

{
private:

T num1, num2;

public:

Calculator(T n1, T n2)

num1 = n1;

num2 = n2;

void show()

cout << "\nNumbers are: " << num1 << " and " << num2;

cout << "\nAddition is: " << add();

cout << "\nSubtraction is: " << subtract();

cout << "\nProduct is: " << multiply();

cout << "\nDivision is: " << divide();

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(8, 2);

Calculator<float> floatCalc(2.4, 1.2);

cout << "Integer results\n-----------------------";

intCalc.show();

cout << endl << "\nFloat results\n---------------------";

floatCalc.show();

return 0;

8(c) Write a Program to illustrate member function templates

# include <iostream.h>

# include <conio.h>

template <class T>

T Max (T a, T b)

return (a > b) ? a : b;

int main ()

int i = 39;

int j = 20;

clrscr();

cout << "Max("<<i<<","<<j<<"): " << Max(i, j) << endl;

double f1 = 13.5;
double f2 = 20.7;

cout << "Max("<<f1<<","<<f2<<"): " << Max(f1, f2) << endl;

char c1 = 'Z';

char c2 = 'A';

cout << "Max("<<c1<<","<<c2<<"): " << Max(c1, c2) << endl;

getch();

return 0;

You might also like