c++day-16

12

Upload: eshamu

Post on 04-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++Day-16

7/31/2019 C++Day-16

http://slidepdf.com/reader/full/cday-16 1/12

Page 2: C++Day-16

7/31/2019 C++Day-16

http://slidepdf.com/reader/full/cday-16 2/12

Define Templates

Types of Templates

Explain Function Templates

Explain Class Templates

Page 3: C++Day-16

7/31/2019 C++Day-16

http://slidepdf.com/reader/full/cday-16 3/12

 

• A generic function defines a general set of operations that will

be applied to various types of data that a function will operate

upon.

• A generic class defines the algorithm that specifies the actual

type of data when objects of that class are created

•A generic class and a generic function is created using the

keyword “Template” 

ADVANTAGES :

  It helps to define classes that are general in nature.

It makes the process simpler and safer

Page 4: C++Day-16

7/31/2019 C++Day-16

http://slidepdf.com/reader/full/cday-16 4/12

 // TEMPLATES DEFINITION

#include<iostream.h>

#include<conio.h>

template <class T>T max(T x,T y)

{

return(x>y)?x:y;

};void main()

{

clrscr();

cout<<max(17,19)<<endl;

cout<<max(1.5,6.7)<<endl;

cout<<max('A','B')<<endl;

getch();}

Output :

19

6.7

Page 5: C++Day-16

7/31/2019 C++Day-16

http://slidepdf.com/reader/full/cday-16 5/12

function template Example=====================

#include<iostream.h>#include<conio.h>template<class T1,class T2>void good(T1 x,T2 y){clrscr();cout<<x<<" "<<y<<endl;}void main()

{good(456,"hello");getch();}

Output :

456 hello

Page 6: C++Day-16

7/31/2019 C++Day-16

http://slidepdf.com/reader/full/cday-16 6/12

For Example#include<iostream.h> :void function1(int i)

{cout<<"The value of i is"<<i<<endl;}void function2(double d){

cout<<"The integer part is"<<int(d);d - = int(d);cout<<"The factorial part is"<<d;}

void main(){function1 (2);function2 (23.5);}

Output :

The value of i is 2

The integer part is 23

The factorial part is 0.5

Page 7: C++Day-16

7/31/2019 C++Day-16

http://slidepdf.com/reader/full/cday-16 7/12

A program to define the function

template for swapping two items of

the various data type as integer and

floating point number

#include<iostream.h>

#include<conio.h>

template<class T>T swap( T &first, T

&second){

T temp;

temp = first;

first = second;

second = temp;

return(0);

}

int swap(int &a,int &b);

void main()

{

int ix,iy;

float fx,fy;

cout<<"Enter any twoNo \n";

cin>>ix>>iy;

cout<<"Enter any 2 Real No";

cin>>fx>>fy;

swap(ix,iy);cout<<"after swapping;

cout<<ix <<iy;

swap(fx,fy);

cout<<"After swapping : cout<<fx <<

fy<<endl;

getch();

}

Page 8: C++Day-16

7/31/2019 C++Day-16

http://slidepdf.com/reader/full/cday-16 8/12

Default Constructor#include<iostream.h>#include<conio.h>template<class T>

class sample{

private:T value;

public:sample(T=0)

{

}void display(){cout<<"default constructor is

called"<<endl;cout<<"Content of the value"<<value<<endl;}};

void main(){clrscr();sample<int> obj1;obj1.display();sample<int> obj2;obj2.display();getch();}

//it prints the ascii value of the intand float.

Overloading -> We have to pass avalue for the variable and have to

implement

Overridding -> To overwrite themethod or function.

Page 9: C++Day-16

7/31/2019 C++Day-16

http://slidepdf.com/reader/full/cday-16 9/12

CLASS TEMPLATE#include<iostream.h>template<class q>class sample{private:q value,value1,value2;public:

get();sum();template <class q>{

void sample<q>::get(){cout<<"Enter a value for the value1"<<endl;cin>>value1;cout<<"Enter a value for the value2"<<endl;

cin>>value2;

Page 10: C++Day-16

7/31/2019 C++Day-16

http://slidepdf.com/reader/full/cday-16 10/12

}}template<class q>

void sample<q>::sum(){q value;value=value1+value2;

cout<<"The added valueis"<<value<<endl;}

};void main(){clrscr();sample<int>obj1;

sample<float>obj2;

cout<<"Enter the integervalue"<<endl;obj1.get();obj1.sum();cout<<"Enter the floatingvalue"<<endl;obj2.get();obj2.sum();

getch();}

Page 11: C++Day-16

7/31/2019 C++Day-16

http://slidepdf.com/reader/full/cday-16 11/12

  A generic function defines a general set of operationsthat will be applied to various types of data that a function

will operate upon.

A generic class defines the algorithm that specifies the

actual type of data when objects of that class are created

A generic class and a generic function is created using the

keyword “template” 

 Rules to be followed for all programs written in C

Page 12: C++Day-16

7/31/2019 C++Day-16

http://slidepdf.com/reader/full/cday-16 12/12

1. Explain generic functions with an example?2. Explain generic class with an example?

EXERCISES