设为首页 加入收藏

TOP

类模版与函数模版
2019-04-06 18:08:07 】 浏览:93
Tags:模版 函数
//
//  main.cpp
//  类模版与函数模版
//  在类声明中使用类型参数来声明通用的类
//  Created by mac on 2019/4/6.
//  Copyright © 2019年 mac. All rights reserved.
//  一个类模版中是否支持多种类型的参数?--可以的
// 在algorithm下已经写过swap函数了,所以自己再用swap这个名字写的就通过不了

#include <iostream>
#include <string>
//#include <algorithm>
using namespace std;

template <class genType,int size=50>
class genClass {
public:
    genClass(){
        cout<<"类模版中执行构造函数"<<endl;
    }
    ~genClass(){
        cout<<"类模版中执行析构函数"<<endl;
    }
    genType storage[size];
};

template <class genType>
void Swap(genType &el1,genType &el2) {
    genType tmp=el1;el1=el2;el2=tmp;
}

int main(int argc, const char * argv[]) {
    int a=1,b=2;
    float c=3.3,d=4.4;
    //使用类模版定义对象
    genClass<int> intObject1;
    genClass<int,100> intObject2;
    genClass<float,123> floatObject;
    Swap(c,d);
    cout<<c<<"\t"<<d<<endl;
    return 0;
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇NLPIR(北理工张华平版中文分词系.. 下一篇指针常量与常量指针

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目