6.19C++:函数模板、类模板、模板传参数

1 函数模板

使用函数模板,可以避免因数据类型不同写多个函数体,同时修改时可以统一修改。

#include <iostream>
using namespace std;
template <class T>
//函数根据传入的数据类型决定T替换成int、double、Point
T abs(T x, T y){
    return x + y; //替换Point时注意要重载Point对象的+运算符
}
class Point {
public:
    Point(int i, int j) : a(i), b(j) {};
    void display(){
        cout << this->a << "   " << this->b << endl;//this表示本对象指针
    }
    friend Point operator + (const Point& x, const Point& y);
private:
    int a, b;
};
Point operator + (const Point& x, const Point& y) {
    return Point(x.a + y.b, x.b + y.a); 
}
int main()
{
    Point x(22, 33), y(44, 55);
    int a = -5, b = 10;
    double c = 6.2, d = 8.2;
    abs(x, y).display();
    cout << abs(a+b) << endl;
    cout << abs(c+d) << endl;
    return 0;
}

2 类模板

使用类模板使用户可以为类声明一种模式,使得类中的某些数据成员、某些成员函数的参数、某些成员函数的返回值,能取任意类型(包括基本类型的和用户自定义类型)。 同样需注意自定义类型的运算符使用问题。

#include <iostream>
using namespace std;
struct Student //定义一个Student结构体
{
    int id;
    float gpa;
};
template <class T>   //模板类定义前要写一次
class Store {
public:
    Store() : flag(false) {};
    void save(const T &a);
    T &get_item();
private:
    T item;
    bool flag;
};
template <class T>   //类外每个函数前都要写该语句 
void Store<T> :: save(const T &a) { //返回值类型 类名<模板参数标识符列表> :: 函数名(参数表)
    item = a;
    flag = true;
}
template <class T >  //类外每个函数前都要写该语句 
T &Store<T> :: get_item() {  //注意要写<T>
    if (flag)
        return item;
    else {
        cout << "暂未存储数据" << endl;
        exit(1); //exit(0)表示程序正常退出,非0表示非正常退出
    }
}
int main()
{
    Store<int> i1, i2;
    i1.save(50);
    i2.save(188);
    cout << i1.get_item() << "  " << i2.get_item() << endl;
    Store<Student> s;  //结构体名字Student是一种数据类型
    Student g = { 1000, 3.3 };  //可以通过g.id、g.gpa 来调用结构体中的数据
    s.save(g);  // 对象s的数据成员item现在是Student类型,赋值为g
    cout << s.get_item().id << endl;  
    Store<double> d;
    cout << d.get_item() << endl;
    return 0;
}

3 模板传参数

函数模板,传入的都是同类型的话,会自动转换获取类型T; 当传入的类型不同,一定要使用声明<类型>放在函数名和()之间,告诉函数模板应该转换什么类型; 但传入的类型不同,但又没有类型声明,那不好意思,编译器不知道该怎么转了,也就出错了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值