You are on page 1of 16

8

.
. .
C++


,
, :
int add(int a, int b)
{ return a+b; }
float add(float a, float b)
{ return a+b; }
long add(long a, long b)
{ return a+b; }
,,
,, , ,
,

template <class Type> >
Type add(Type X, Type Y)
{ return X+Y;
}
C++

C++

function
template


class
template



void swap(Type &a, Type &b)
{ Type t;
t=a; a=b; b=t;
}
Type

Typemax(Type a, Type b)
{ return(a>b)?(a):(b);
}
ab >


#include <iostream.h>
template <class T>
T max(T a, T b)
{ return a > b ? a : b;
}
void main()
{ cout << "max(20, 30) = " << max(20, 30) << endl;
cout << "max('t', 'v') = " << max('t', 'v') << endl;
cout << "max(10.1, 15.2) = " << max(10.1, 15.2) << endl;
}

int max(int a, int b){ return a > b ? a : b;}


char max(char a, char b){ return a > b ? a : b;}
float max(float a, float b){ return a > b ? a : b;}

. .
template

template function

.
1

C++




parameterized
type C++

int add(int x, int y)


{ x=x+y; //
return x;
}
float add(float x, float y)
{ x=x+y ; //

return x;
}
2

template

template <class Type> >


Type add(Type X, Type Y)
{ return X+Y;
}

template

Type

class typename



template <class Type, int size> >
Type max(const Type (&array)[size])
{ const int loc_size = size;
Type loc_array[loc_size];
//
}

Type size size
size
3


VC


<> <<>> (<>);

intdoublechar

1
#include<iostream.h>
template<class T>
T abs(T x)
{ return x<0?-x:x;
}
void main()
{ int n=-5;
double d=-5.5;
cout<<abs(n)<<endl;
cout<<abs(d)<<endl;
}

abs abs(n) n int


T int

int abs(int x)
{ return x<0?-x:x;
}

2
#include <iostream.h>
template <class T> //
void printArray(T *array,const int count)
{ for (int i=0; i< count; i++)
cout<< array[i]<< ;
cout<<endl;
}
void main( )
{ const int aCount=5, bCount=7, cCount=6;
int a[aCount]={1,2,3,4,5};
float b[bCount]={1.1,2.2,3.3,4.4,5.5,6.6,7.7};
char c[cCount]=Hello;
cout<<Array a contains:<<endl;
printArray(a, aCount); //
cout<<Array b contains:<<endl;
printArray(b, bCount); //
cout<<Array c contains:<<endl;
printArray(c, cCount); //
}

3
#include <iostream.h>
template <class Type> //
void sort(Type x[ ], int size)
{ for (int i=0; i<size-1; i++)
for (int j=i+1; j<size; j++)
if(x[i] >x[j] )
{ Type t=x[i]; x[i]= x[j]; x[j]=t; }
}
void main( )
{ int a[ ]={4, 3, 4, 5, 8, 9};
double d[9]={2.0, 4.3, 1.8, 9.5};
sort(a, 6); //
sort(d, 9); //

cout << " \n output sort for int a: ";


for(int i=0; i<6; i++) cout << a[i] << " , ";
cout << " \n output sort for double d: " ;
for(int j=0; j<9; j++) cout << d[j] << ", ";
}
1 sort,
2 ,
int double
3 , , ,
4 < > , [ ] ( )

4
#include "iostream.h"
template <class Type>
Type add(Type X, Type Y) //
{ return X+Y;
}
template <class Type> //
Type add(Type X, int Y, Type Z)
{ return X+Y+Z;
}
template <class Type> //
float add(Type X, double Y,Type Z)
{ float f=X+Y+Z;
return f;
}
void main(void)
{ cout<<add(1,2)<<endl<<add('A','B')<<endl;
cout<<add(1.2, 3.4)<<endl<<add(1,2,3) <<endl;
cout<<add(1.2, 2.3, 3.4) <<endl;
cout<<add(1, 2.3, 4) <<endl;
cout<<add(1.2, 2, 4.3) <<endl;
}

5
complex.h
class complex {
public:
double real,imag;
complex(double r=0,double i=0) //
{ real = r; imag = i; }
};
#include <iostream.h>
#include "complex.h"
class Mycomplex:public complex
{ public:
Mycomplex():complex(0,0){}
Mycomplex(double r, double i): complex(r,i){}
friend ostream& operator <<(ostream& s, complex c)
{ s<<"("<<c.real<<","<<c.imag<<")";
return s; }
friend int operator>( Mycomplex& c1, Mycomplex& c2);
};
int operator>( Mycomplex& c1, Mycomplex& c2)
{ return c1.real>c2.real;
}
template <class T> //
T& Bigger(T& a,T& b)
{ return(a>b)?a:b;
}
void main( )
{ int a1=10,a2=20,a3;
double d1=1.1,d2=2.2,d3=3.3;
Mycomplex c1(1,2),c2(3,4),c3;
a3= Bigger(a1,a2);
d3= Bigger(d1,d2);
c3= Bigger(c1,c2);
cout<<The result is: \n<<The Bigger of <<a1<< and <<a2<< is
<<a3<<endl;
cout<<The Bigger of <<d1<< and <<d2<< is << d3<<endl;
cout<<The Bigger of <<c1<< and <<c2<< is << c3<<endl;
}

6
#include <iostream.h>
template <class T> //
T max(T a, T b)
{ return (a>b? a:b);
}
void main( )
{ int a=9, b=99;
cout << "max(a,b): " << max(a,b) << endl;
float c=5.0, d=9.0;
cout << "max(c,d): " << max(c,d) <<endl;
}
4
6 cout << "max(a,c): " << max(a,c) <<endl;
Could not find a match for 'max(int,float)'
max(int,float) int float
float

float
float max(float, float);

int max(int, int);

int float

6
#include <iostream.h>
template <class T> //
T max(T a, T b)
{ return (a>b? a:b);
}
float max(float, float);
// int max(int, int); //
void main( )
{ int a=9, b=99;
cout << "max(a,b): " << max(a,b) << endl;
float c=5.0, d=9.0;
cout << "max(c,d): " << max(c,d) <<endl;
cout << "max(a,c): " << max(a,c) <<endl;
long l=15;
cout << "max(l,d): " << max(l,d) <<endl;
}
C++
1
2
3
4
5


#include <iostream.h>
template <class T>
T min(T x, T y) {
cout<<" min(T x, T y),";
return x > y ? y : x;
}
template <class T>
T min(T a[] , int n)
{ int i;
T minv = a[0];
for(i = 1 ; i < n ; i++)
{ if(minv > a[i])
minv = a[i];
}
cout<<" min(T a[], int n),";
return minv;
}
void main()
{ int x = 8, y = 23;
double a[5] ={ 2.342, 11.346, 8.93, 18.111 , 5.930};
cout << min(x,y) << endl;
cout << min(a , 5) << endl;
}
5


//maxmin.h
#ifndef MAXMIN_H
#define MAXMIN_H //#include
template <class T> //
T max(T a, T b)
{ return (a>b? a:b);
}
template <class T> //
T min(T a, T b)
{ return (a<b? a:b);
}
float max(float, float);
float min(float, float);
#endif MAXMIN_H

#include iostream.h
#include maxmin.h
void main( )
{ int a=9, b=99;
cout << "max(a,b): " << max(a,b) << endl;
float c=5.0, d=9.0;
cout << "max(c,d): " << max(c,d) <<endl;
cout << "min(a,c): " << min(a,c) << endl;
cout << "min(b,d): " << min(b,d) <<endl;
}
6

char *min(char *a,char *b)
{ return (a<b? a:b);
}


//maxmin.h
#ifndef MAXMIN_H
#define MAXMIN_H //#include
template <class T> //
T max(T a, T b)
{ return (a>b? a:b);
}
template <class T> //
T min(T a, T b)
{ return (a<b? a:b);
}
float max(float, float);
float min(float, float);
#endif MAXMIN_H
#include iostream.h
#include maxmin.h
#include string.h
char *min(char *a,char *b) //
{ if (strcmp(a,b)>0)
return (b);
else return (a);
}
char *max(char *a,char *b) //
{ if (strcmp(a,b)>0)
return (a);
else return (b);
}
void main( )
{ cout << " max(4,8): " << max(4,8) << endl;
cout << " max(10.1,1.2): " << max(10.1,1.2) <<endl;
cout << " min(2,1.0): " << min(2,1.0) << endl;
cout << " min(4,8.6): " << min(4,8.6) <<endl;
cout << " min(e,a): " << min(e,a) <<endl;
cout << " max(e,a): " << max(e,a) <<endl;
cout << " max(a,b): " << max(a,b) <<endl;
cout << " min(a,b): " << min(a,b) <<endl;
}

.3
.3
1




C++

2 template
template < >
class < >
{
//
}
template < >
<><>< < > <
1>
{
//
}

1< >
1class <>
2<><>
< >

template < class T1,int exp1,calss T2>
>
calss someclass
{
//
};
2< >

< > 1 2 n


4

1
template <class T> //
class complex
{ T real,imag;
public:
complex(T r=0,T i=0) //
{ real = r; imag = i;
}
void disp()
{ cout<< real <<+<< imag <<i<<endl;
}
};
#include <iostream.h>
void main()
{ complex<int> f1(2,3);
complex<double> f2(4.3,5.2);
f1.disp();
f2.disp();
}
f1 T int f2 T double

2#include <iostream.h>
#include <math.h>
template <class T> //
class TAdd
{ T x,y;
public:
TAdd(T a=0,T b=0); //
T add();
};
template <class T>
TAdd<T>::TAdd(T a=0,T b=0)
{ x = a; y = b;
}
template <class T>
T TAdd<T>::add()
{ return x+y;
}
void main()
{ TAdd <int> A(5,6);
TAdd <double> B(2.4,5.8);
cout<<sum1=<<A.add()<<endl;
cout<<sum2=<<B.add()<<endl;
}
A T int B T double

3
#include <iostream.h>
#include <string>
template <class T>
class Array {
public:
Array(T a, T b)
{ x = a; y = b;
}
T sum()
{ T tempsum = x + y;
return tempsum;
}
private:
T x; T y;
};
void main()
{ Array <int> A (7.0 , 6.0);
cout << "A "<< A.sum()<<endl;
Array <double> B (1.0 , 6.0 );
cout << "B "<< B.sum()<<endl;
}

4#include <iostream.h>
template <class T> //
class Sample
{ T n;
public:
Sample(T t){ n=t;}
void operator++();
void disp(){ cout<<n=<<n<<endl;}
};
template <class T>
void Sample<T>:: operator++()
{ n+=1;
}
void main()
{ Sample<char> s(a);
++s;
s.disp();
}

5
#include <iostream.h>
template <class T>
class Base
{ public:
void showb(T b){ cout<<b=<<b<<endl;}
};
template <class T1 ,class T2>
class Derived: public Base<T2>
{ public:
void showd(T1 obj1,T2 obj2)
{ cout<<obj1<< <<obj2<<endl;}
};
void main()
{ Derived<char *, double>obj1;
obj1.showb(55.58);
obj1.showd(The value is: ,53.25);
Derived<int,int>obj2;
obj2.showd(32,89);
Derived<float,char*>obj3;
obj3.showd(5.8,is ok.);
Derived<char,char*>obj4;
obj4.showd(I,love you.);
}

6
//store.h
#ifndef TEMPLATE_STORE_CLASS
#define TEMPLATE_STORE_CLASS //#include
#include <iostream.h>
#include <stdlib.h>
template <class T> //
class Store
{ private:
T item; // item
int haveValue; // haveValue item
public:
Store(void); //
T GetElement(void); //
void PutElement(T x); //
};
//
//
//
template <class T>
Store<T>::Store(void): haveValue(0)
{ }
//
template <class T>
T Store<T>::GetElement(void)
{ if (haveValue == 0) //
{ cout << "No item present!" << endl;
exit(1);
}
return item; // item
}
//
template <class T>
void Store<T>::PutElement(T x)
{ haveValue++; // haveValue TRUE item
item = x; // x item
}
#endif // TEMPLATE_STORE_CLASS
//student.h
#ifndef STUDENT
#define STUDENT //#include
// Student
struct Student
{ int studID; //
float gpa; //
};
// "=="
int operator==(Student a, Student b)
{ return a.studID == b.studID;
}
#endif // STUDENT
//.cpp
#include <iostream.h>
#include "store.h"
#include "student.h"
void main(void)
{ Student graduate = {1000, 23}; // Student
Store<int> A, B;// Store item int
Store<Student> S; // Store S item Student
Store<double> D; // Store D item double
A.PutElement(3); // A A
B.PutElement(-7); // B B
// A B
cout << A.GetElement() << " " << B.GetElement() << endl;
S.PutElement(graduate); // S S
// S
cout << "The student id is " << S.GetElement().studID << endl;
cout << "Retrieving object D " ;
cout << D.GetElement() << endl; // D
// D , D.GetElement()
}
. 4

1




2




3

You might also like