随笔 - 56, 文章 - 0, 评论 - 0, 引用 - 0
数据加载中……

模板

◆类模板的使用
读程序:
#include<iostream>
#include
<process.h>
using namespace std;

template
<class T>class valueAcess
{
    T value;
    
bool haveValue;
    
public:
    valueAcess(
void);
    T GetValue(
void);
    
void PutValue(T x);
}
;
template
<class T>
valueAcess
<T>::valueAcess(void)
{
    haveValue
=false;
}

template
<class T>
T valueAcess
<T>::GetValue(void)
{
    
if(haveValue) return value;
    
else
    
{
        cout
<<"No value present!"<<endl;
        exit(
1);
    }

}

template
<class T>
void valueAcess<T>::PutValue(T x)
{
    haveValue
=true;
    value
=x;
}

struct Student
{
    
char name[20];
    
long id;
    
float score;
}
;
int main()
{
    valueAcess
<int> iObj1,iObj2;
    iObj1.PutValue(
321);
    iObj2.PutValue(
-876);
    cout
<<"iObj1.GetValue()=>"<<iObj1.GetValue()<<endl;
    cout
<<"iObj2.GetValue()=>"<<iObj2.GetValue()<<endl;
    Student maji
={"maji",1022,89};
    valueAcess
<Student>sObj;
    sObj.PutValue(maji);
    cout
<<"sObj.GetValue().id=>"<<sObj.GetValue().id<<endl;
    valueAcess
<double>dObj;
    cout
<<"dObj.GetValue()=>";
    cout
<<dObj.GetValue()<<endl;
    
return 0;
}

运行结果为:
iObj1.GetValue()=>321
iObj2.GetValue()
=>876
sObj.GetValue().id
=>1022
No value present
!

◆既使用类型参数又使用普通参数的类模板
读程序:
#include<iostream>
#include
<cstring>
using namespace std;

template
<class T,int i>class tmplt
{
    
public:
    T dat[i];
    T getDat(
int j);
}
;
template
<class T,int i>
T tmplt
<T,i>::getDat(int j)
{
    
return (*(dat+j));
}

int main()
{
    tmplt
<char ,5> ClassInstA;
    strcpy(ClassInstA.dat,
"rsxyz");
    
for(int i=0;i<5;i++)
    
{
        
char res=ClassInstA.getDat(i);
        cout
<<res<<" ";
    }

    cout
<<endl;
    tmplt
<double ,6>ClassInstF;
    
for(int i=0;i<6;i++)
    ClassInstF.dat[i]
=2*i+0.12;
    
for(int i=0;i<6;i++)
    
{
        
double res=ClassInstF.getDat(i);
        cout
<<res<<" ";
    }

    cout
<<endl;
    
return 0;
}

显示结果为:
 r    s    x   y   z
0.12  2.12  4.12  6.12  8.12  10.12

◆类模板作基类派生出类模板
读程序:
#include<iostream>
using namespace std;

template
<class T2>class baseCla
{
    
int d;
    
char ch;
    T2 x2;
    
public:
    baseCla()
    
{
        d
=25;
        ch
='a';
        x2
=100;
    }

    
void putx2(T2 tt)
    
{
        x2
=tt;
    }

    T2 getx2()
    
{
        
return x2;
    }

    
void print()
    
{
        cout
<<"baseCla:d="<<d<<" ch="<<ch<<" x2="<<x2<<endl;
    }

}
;
template
<class T1,class T2>class derivedCla:public baseCla<T2>
{
    T1 x1;
    
public:
    derivedCla()
    
{
        x1
=0;
    }

    
void putx1(T1 tt)
    
{
        x1
=tt;
    }

    T1 getx1()
    
{
        
return x1;
    }

    
void print()
    
{
        baseCla
<T2>::print();
        cout
<<"derivedCla: x1="<<x1<<endl;
    }

}
;
int main()
{
    derivedCla
<double,char>der1;
    der1.print();
    cout
<<"------------------"<<endl;
    der1.putx1(
321.5);
    der1.putx2(
'H');
    cout
<<"der1.getx1()="<<der1.getx1()<<endl;
    cout
<<"der1.getx2()="<<der1.getx2()<<endl;
    der1.print();
    
return 0;
}

结果显示:
baseCla:d=d=25 ch=a x2=d
derivedCla: x1
=0
-------------------------
der1.getx1()
=321.5
der1.getx2()
=H
baseCla:d
=d=25 ch=a x2=H
derivedCla: x1
=321.5
分析:
可以使用多种不同的方式来派生出类模板。如可使用一般类作基类而派生出类模板,也可使用类模板作基类而派生出新的
类模板。本范例中的derivedCla是被派生出的类模板,而其基类baseCla也为一个类模板。derivedCla类模板中具有两个类型参数
T1与T2,其中的类型参数T2将被传递给基类baseCla,而在派生类derivedCla中将只使用另一个类型参数T1 (而不使用T2)。

posted on 2009-08-23 10:23 八路 阅读(120) 评论(0)  编辑 收藏 引用 所属分类: 学习笔记


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理