返回类型 函数名(形式参数表)
{
... //函数体
}template <class type>
type Sum(type xvar, type yvar) {
return xvar + yvar;
}#include <iostream>
using namespace std;
template <class type,int len> //定义一个模板类型
type Max(type array[len]) { //定义函数模板
type ret = array[0];
for(int i=1;i<len;i++) {
ret = ret > array[i] ? ret : array[i];
}
return ret;
}
int main(int argc,char* argv[]) {
int array[5] = {1,2,3,4,5}; //定义一个整型数组
int iret = Max<int,5>(array); //调用函数模板Max
double dset[3] = {1.2, 3.45, 6.789}; //定义实数数组
double dret = Max<double,3>(dset); //调用函数模板Max
cout << dret << endl;
return 0;
}#include <iostream>
#include <cstring>
using namespace std;
template<class type>
type Min(type a, type b) { //定义函数模板
return a < b ? a : b;
}
char* Min(char* a, char* b) { //重载函数模板
if(strcmp(a, b) < 0) return a;
return b;
}
int main(int argc, char* argv[]) {
cout << Min(10, 3) << endl;
cout << Min('a','b') << endl;
cout << Min("lasolmi","hello world") << endl;
return 0;
}template<template<class A> class B>
class CBase {
private:
B<int> m_n;
};emplate<class T>
class CDerived : public T
{
public:
CDrived();
};
template<class T>
CDerived<T>::CDerived(): T() {
cout << :: <<endl;
}
int main(int argc,char* argv[]) {
CDerived<CBase1> D1;
return 0;
}#include <iostream>
using namespace std;
template<class T1, class T2>
class MyTemplate {
T1 t1;
T2 t2;
public:
MyTemplate(T1 tt1, T2 tt2) {t1 = tt1; t2 = tt2;}
void display() {cout << t1 << " " << t2 <<endl;}
};
int main(int argc, char* argv[]) {
int a = 123;
double b = 3.14159;
MyTemplate<int,double> mt(a, b);
mt.display();
return 0;
}#icnlude <cassert>
template<class T, int b>
class Array {
T& operator[] (int sub) {
return sub >=0 && sub < b;
}
};#include <cstdio>
class CBase { //基类
public:
virtual char * GetName() = 0; //虚方法
};
class CBint : public CBase {
public:
char * GetName() {return "CBint";}
int GetInt() {return 1;}
};
class CBString : public CBase {
public:
char * GetName() {return "CBString";}
char * GetString() {return "Hello";}
};
int main(int argc, char* argv[]) {
CBase* B1 = (CBase*)new CBint();
printf(B1->GetName());
CBint* B2 = static_cast<CBint*>(B1); //静态替换
if(B2) printf("%d" , B2->GetInt());
CBase* C1 = (CBase*)new CBString();
printf(C1->GetName());
CBString* C2 = static_cast<CBString*>(C1);
if(C2) printf(C2->GetString());
return 0;
}#include <iostream>
#include <cstring>
using namespace std;
class CCustomError {
private:
int m_ErrorID;
char m_Error[255];
public:
CCustomError(int ErrorID, char* Error) {
m_ErrorID = ErrorID;
strcpy(m_Error, Error);
}
int GetErrorID() {return m_ErrorID;}
char* GetError() {return m_Error;}
};
int main(int argc, char* argv[]) {
try {
throw (new CCustomError(1,"出现异常!"));
} catch(CCustomError* error) {
//输出异常信息
cout << "异常ID:" << error->GetErrorID() <<endl;
cout << "异常信息:" << error->GetError() << endl;
}
return 0;
}int main(int argc, char* argv[]) {
try {
throw "字符串异常";
} catch (CCustomError* error) {
//输出异常信息
cout << "异常ID:" << error->GetErrorID() <<endl;
cout << "异常信息:" << error->GetError() << endl;
} catch (char* error) {
cout << "异常信息:" << error << endl;
} catch (...) {
cout << "未知异常信息" << endl;
}
return 0;
}namespace std
{
//exception 派生
class logic_error;
//logic_error 派生
class domain_error;
class invalid_argument;
class length_error;
class out_of_range;
class bad_cast;
class bad_typeid;
//exception 派生
class runtime_error;
//runtime_errot 派生
class range_error;
class overflow_error;
class bad_alloc;
}原文:http://blog.csdn.net/lasolmi/article/details/40556489