类模板学习

#include <iostream>
using namespace std;


//1.类模板的概念
/*
template <class T>
class person
{
public:
    person(T id,T mage)
    {
        this->id=id;
        this->mage=mage;

    }

    void show()
    {
        cout<<"id: "<<id<<"mage: "<<mage<<endl;

    }

    T id;
    T mage;

};


void test1()
{
    //函数模板在调用的时候,可以自动类型推导。
    //而类模板必须显示的指定类型
    person<int> p(10,29);
    p.show();

}


int main() {
    test1();
    return 0;
}
*/


//2.函数模板的案例
//对char类型和int类型数组进行排序

/*
//打印函数
template <class T>
void printarray(T *arr,int len)
{
    for (int i=0;i<len;i++)
    {
        cout<<arr[i]<<" ";

    }
    cout<<endl;

}

//排序
template <class T>
void mysort(T *arr,int len)//冒泡排序
{
    for (int i=0;i<len;i++)
    {
        for (int j=i+i;j<len;j++)
        {
            if (arr[i]<arr[j])
            {
                int temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
    }
}

int main()
{
    //数组
    int arr[]={2,3,4,6,7,9,11,34,56,22};
    //数组长度
    int len= sizeof(arr)/ sizeof(int);

    //排序前
    cout<<"排序前...................."<<endl;
    printarray(arr,len);

    //排序
    mysort(arr,len);

    //排序后
    cout<<"排序后......................"<<endl;
    printarray(arr,len);


    //字符数组
    char charr[]={'a','b','e','t','v','h'};
    int len1= sizeof(charr)/ sizeof(char);
    cout<<"char类型数组排序前................"<<endl;
    printarray(charr,len1);//普通函数对于数组参数,不具有类型转换,加函数模板可解决问题。
    mysort(charr,len1);
    cout<<"char类型数组排序后..................."<<endl;

    printarray(charr,len1);

}
 */


//3.类模板派生普通类和类模板派生类模板
/*
//类模板派生普通类
template <class T>
class parent
{
public:
    parent(T a)
    {
        this->a=a;

    }
    void vocie()
    {

        cout<<"a= "<<a<<endl;
    }
    T a;

};

class child1:public parent<int >//子类继承模板类需指定父类类型,因为编译器创建类对象,需要分配内存
{
public:
    child1(int a,int b):parent(a)
    {
        this->b=b;
    }
    int b;
    void printb()
    {
        cout<<"b= "<<b<<endl;
    }


};


//类模板派生模板类


template <class T>
class child2:public parent<T>
{
public:
    child2(T a,T b):parent<T>(a)
    {
        this->b=b;
    }
    void printb()
    {
        cout<<"b= "<<b<<endl;

    }

    T b;

};


int main()
{
    parent<int> com1(10);
    com1.vocie();

    child1 com2=child1(11,22);
    com2.vocie();
    com2.printb();

    child2<double > com3=child2<double >(12.1,23.2);
    com3.vocie();
    com3.printb();

};
 */


//4.类模板的类内实现

//5.类模板的类外实现


//basecase,类的类内实现
//template <class T>
//class person
//{
//public:
//    person(T id,T age)
//    {
//        this->id=id;
//        this->age=age;
//    }
//
//    void show()
//    {
//        cout<<"id ="<<id<<"age= "<<age<<endl;
//    }
//    T id;
//    T age;
//};


//case1 类模板在类的外部实现,.cpp和.h不分离编写
//类模板类外实现的时候,不要滥用友元

/*
template <class T> class person;//类外声明
template <class T> void print(person<T>& p);
//template <class T> ostream& operator<< (ostream& os,person<T>& p);

template <class T>
class person
{
public:
    //template<class T>//window下的编译器可以通过
//    friend ostream& operator<<<T>(ostream& os,person<T>& p);
    friend void print<T>(person<T>& p);//linux 下的写法,在友元函数后加<T>
    person(T id,T age);
    void show();

private:
    T id;
    T age;
};

template <class T>
person<T>::person(T id, T age)
{
    this->id=id;
    this->age=age;

}
template <class T>
void person<T>::show()
{
    cout<<"id= "<<id<<"age= "<<age<<endl;

}

//重载左移操作符
//template <class T>
//ostream& ::operator<<(ostream& os,person<T>& p)
//{
//    os <<"id= "<<p.id<<"age= "<<p.age<<endl;
//    return os;
//
//}

template <class T>
void print(person<T>& p)
{

    cout<<p.id<<" "<<p.age;
}

void test001()
{
    person<int> p(10,12);
//    p.show();
//    cout<<"--------------------------"<<endl;
//
//    cout<<p;
    print(p);

}

int main()
{
    test001();

}

 */

//case2 类模板类的外部实现,.cpp和.h分离编写

//类模板类不可以可分离编译问题原因(模板需要二次编译,而在可分离编写中,.cpp中的模板并没有实例,因此不能发生第二次编译,故在链接器链接含有模板的.cpp时候,报错,
// 解决办法:将类模板的声明和实现写在一个文件中)
//和模板实现机制 c++编译机制有关
//1.模板二次编译,第一次为模板编译,第二次编译发生在调用的时候生成具体函数
//2.c++中,文件独立编译.列如a.cpp文件在编译的时候,发现一个函数调用,在当前文件中找不到时,会在相应位置生成符号,以便链接的时候在其他文件找到对应的函数。


//6类模板中的static关键字

template<class T>
class person
{
public:

    static int a;


};

template<class T>  int person<T>::a=0;
int main()
{
    person<int > p1,p2,p3;
    person<char > c1,c2,c3;

    p1.a=10;
    c1.a=100;
    cout<<p1.a<<p2.a<<p3.a<<endl;
    cout<<c1.a<<c2.a<<c3.a<<endl;
    //101010
    //100100100
    //结果说明,类模板中有static的时候,类模板实例化不同类型的类,其实例化类的不同对象共享类模板中static
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值