当前位置:
文档之家› 计算机设计程序-模板与异常处理 (1)
计算机设计程序-模板与异常处理 (1)
模板中可以带有多个参数类型。例如:
template <class T1, class T2, class T3> void func1(T1 arg1,T2 arg2, T3 arg3) {
… }
函数可以带有模板参数表中未给出的数据类型的参数
template <class T> T func2(T arg1,int arg2) {
4
}
模板函数的说明语句
#include <iostream>//例12-1
#include <string> using namespace std;
template <class T> T Max(T a, T b)
template <class T>
{
T Max(T a, T b);
return a>b?a:b;
… }
8
求幂函数
#include <iostream.h> //例13-4 int Power(int a, int exp) //整数、实数的整数次幂 {
int ans = a; while(--exp>0) ans*=a; return ans; } int main() // 测试用主函数 { cout << "3^5= " <<Power(3, 5) << endl; cout << "1.1^2= " << Power(1.1, 2) << endl; return 0; }
cout << "Type string: " << Max(str1, str2) << endl;
return 0;
}
5
冒泡排序改成模板?
#include <iostream.h>
void bubble_sort( int a[],int N)
{ for(int i=0; i<N; i=i+1)
return 0;
}
6
#include <iostream.h>
template <class T>
void bubble_sort( T a[],int N)
{ for(int i=0; i<N; i=i+1)
for(int j=N-1; j>i; j=j-1)
if(a[j-1]>a[j])
{T tmp = a[j-1];a[j-1] = a[j];a[j] = tmp;}
14
}
圆柱体类
#include <iostream.h>
class Cylinder
{
int x,y,z;
int R,H;
public:
Cylinder(){ x=y=z=R=H=0;};
Cylinder(int x1,int y1,int z1,int R1,int H1)
{
x=x1;y=y1;z=z1;
275,653, 426, 154, 509, 612, 677, 765, 703
};
bubble_sort(list,16);
cout << "The result is :" << endl;
for(int k=0;k<16;k++)
cout << list[k] << " ";
cout<<endl;
13
定义通用数组类
#include <iostream.h> template <class X> class array { public:
X *ptr; array(int N){ ptr=new X[N];} ~array(){ delete []ptr; } }; int main() { array <int> a(16); a.ptr[0]=168; cout<<"array class: "<<a.ptr[0]<<endl; return 0;
}
int main()
{ const int COUNT=16;
int list1[COUNT]={503, 87, 512, 61, 908, 170, 897,
275,653, 426, 154, 509, 612, 677, 765, 703
};
double list2[COUNT]={50.3, 87, 51.2, 6.1, 90.8, 17.0, 89.7,
cout<<"体积:"<<hhh.volumn()<<endl;
cout<<"面积:"<<hhh.area()<<endl;
3
求两数最大值的函数模板
#include <iostream>//例12-1 #include <string> //含有重载>、>=、<、<=运算符 using namespace std; template <class T> T Max(T a, T b) {
return a>b?a:b; } int main() {
char *c1="xjtu";
cout <<sequentialsearch(i1, 15, 6)<< endl;
cout <<sequentialsearch(d1, 3.3, 5)<< endl;
cout <<sequentialsearch(c1, 'j', 4)<< endl;
return 0;
R=R1;H=H1;
}
int volumn(){ return 3.14*R*R*H;};
int area(){ return 6.28*(R*R+R*H);};
};
void main()
{
Cylinder hhh(0,0,0,2,3); //²âÊÔintÀàÐÍ
cout<<"体积:"<<hhh.volumn()<<endl;
} 12
类模板
类是对问题空间的抽象,而类模板则是对类的抽象。 与函数模板相似,程序中可以通过高度抽象首先定义
一个类模板,然后通过使用不同的实参生成不同的 类。 类模板的定义方法为:
template < class <类型参数> > class <类名> {
…… }; 使用 <类名> <int> 对象名1,对象名2;
template <class T> T Max(T a, T b) { return a>b?a:b; }
cout << "Type int: " << Max(i1, i2) << endl; cout << "Type double: " << Max(d1, d2) << endl;
2
函数模板
10
顺序查找
//在数组a中查找k,找到,返回下标
#include <iostream.h>
int sequentialsearch(int a[], const int & k, int n)
{
int i=0;
while(k!=a[i]&&i<=n-1) i++;
if(i>n-1)i=-1;
return i;
本章内容
模板
函数模板 类模板
异常处理
0术Biblioteka 简介模板通用的语言描述称为模板 函数模板
通用抽象的函数描述
类模板
通用抽象的类描述
异常处理机制
指用于控制程序期间错误的结构化方法
1
求两个数的最大值
int Max(int a, int b) { return a>b?a:b; }
double Max(double a, double b) { return a>b?a:b; }
{
int i=0;
while(k!=a[i]&&i<=n-1) i++;
if(i>n-1)i=-1;
return i;
}
int main() // 测试用主函数
{
int i1[] ={3, 2, 5, 0, -1, 7};
double d1[] ={3.3, 2.1, 0.3, 1.5, 10.6, 5.2};
}
int main() // 测试用主函数
{
int i1[] ={3, 2, 5, 0, -1, 7};
double d1[] ={3.3, 2.1, 0.3, 1.5, 10.6, 5.2};
char *c1="xjtu";
cout <<sequentialsearch(i1, 15, 6)<< endl;
27.5,65.3, 42.6, 15.4, 5.09, 6.12, 67.7, 7.65, 7.03};
int k;