C++template03 class template

//类模板
//类模板的作用:建立一个同类,类中的成元数据类型不确定,使用虚拟的代表类型T
#include <iostream>    
#include <string>
using namespace std;

//类模板
template<typename NameT, class AgeT> // 需要几个T 就写几个T ,用 ,, 个开就好了
//后面这就算类模板了
class Person
{

public:
    Person(NameT name, AgeT age)
    {
        this->name = name;
        this->age = age;
    }
   
    NameT name;
    AgeT age; 
   
   void showPerson()
   {
       cout << "name :" << this->name << endl;
   }
};

void test01()
{
    Person<string, int> p1("Tom", 10); // 指定好型参的类型就好了
    p1.showPerson();
}


/**********************类模板和函数模板的区别
 * 类模板没有自动类型推倒的使用方式
 * 类模板在模板参数列表中可以有默认的参数
 * ************************/
//类模板与函数模板的区别
//类模板
template<class NameType, class AgeType=int>
class Person2
{
    public:
        NameType name;
        AgeType age;

        Person2(NameType name, AgeType age)
        {
            this->name = name;
            this->age = age;
        }
        
    void showPerson()
    {
        cout << "age :" << this->age << endl;
    }
};
void test02()
{
    // Person2 p3("suiwukong" , 90);//这里是错误的,无法用自动类型推倒

    Person2<string, int> p3("Tom", 10); // 指定好型参的类型就好了,显示指定

// 类模板在模板参数列表中可以有默认的参数
    Person2<string> P4("猪八戒", 999);  //如果在template<class NameType, class AgeType> 换成template<class NameType, class AgeType =int > 就可以;了


}



int main(int argc, char** argv)
{
    test02();
    return 0;
}
#include <iostream>
#include <string>
  
using namespace std;
/**************类模板中成员函数的创建时机 **********/
/*
笔记:
普通类中的成员函数一开始就创建
类模板中的成员函数在调用时才会创建
*/


class Person1 
{
    public:
        void showPerson1()
        {
            cout << "Person1 show" << endl;
        }
};

class Person2
{
    public:
        void showPerson2()
        {
            cout << "Person2 show" << endl;
        }
};

template<class T>
class Myclass
{
    public:
        T obj;

        //类模板中的成员函数
        void func1()
        {
            obj.showPerson1(); //因为obj还没有创建实例,不知道类型,所以成员函数不会被创建,不会运行这里
        }
        void func2()
        {
            obj.showPerson2();
        }
};

void test(){

    Myclass<Person1> m; //这里创建了一个Myclass类的实例 m,并且制定了m中的对象obj的类型
    m.func1(); // 这里没毛病  重点:::::::::只又在调用的时候,func1  2 才会创建,,,因为不调用的时候不知道obj的类型
    // m.func2();//错误!!!!!!!!!1
}



/**************类模板对象作函数的参数 **********/
/*
笔记:
一共有三个传递方式:
1.指定传入的类型:直接显示对象的数据类型
2.参数模板化
3.整个类模板化
总结:我写了一个类模板,也就是模板后面接着的是类,类中的一些变量的类型不是确定的 ,这个时候如果类模板(就是这个类)想作为函数参数,一共有三个方式,1 2 3
总之想方设法的让人知道不确定的 T1 T2 是个沙
*/

template<class T1, class T2>
class Person01{
    public:
        Person01(T1 name, T2 age)
        {
            this ->age = age;
            this ->name = name;
        }
        T1 name;
        T2 age;
        void showPerson()
        {
            cout << "姓名:" << this ->name << endl;
        }
}; // class template!!!


void PrintPerson01(Person01<string, int> &p) // class template  作函数形参   指定传入的参数类型
{
    p.showPerson();
}
void test02(){
    Person01<string, int> p1("suiwuh", 100);
    PrintPerson01(p1);
}
//参数模板化
template<class T1, class T2>
void PrintPerson2(Person01<T1, T2>p)
{
    p.showPerson();
}
void test03(){
    Person01<string, int> p1("zhubajie", 99);
    PrintPerson01(p1);
}

//将整个类进行模板化
template<class T>
void PrintPerson3( T &p )
{
    p.showPerson();
}

void test04()
{
    Person01<string, int> p2("tangseng", 8888);
    PrintPerson3(p2);
}
int main()
{
    test04();
    //test01();
    return 0;
}
#include <string>
#include <iostream>
using namespace std;
/**************类模板与继承 **********/
/*
笔记:
类模板遇到继承的时侯:需要注意:

*/
template<class T>
class Base
{
    T m;
};
// class son :public Base // 需要知名父类中的T的数据类型,才能继承给子类
// {

// };

class son :public Base<int> // 需要知名父类中的T的数据类型,才能继承给子类
{

};

void test(){
    son s1;
}

//如果想灵活的指定父类中的T 的类型, 子类需要也变成类模板
template<class T , class T1>  //<> 这里里面的顺序等与创建实例对象是<> 的顺序
class Son2 :public Base<T>
{
public:
    Son2()
    {
        cout << typeid(T1).name() << endl;
        cout << typeid(T).name() << endl;
    }
    T1 obj;
};

void test01(){
  Son2<char ,int> s2;
}
/*************
 * 
 * 
 * 类模板成员函数类外实现
 * 
 * **********/
/*
笔记:

template<class T1, class T2>
Person01<T1 ,T2>::Person01(T1 name, T2 age)

*/
template<class T1, class T2>
class Person01{
    public:
        Person01(T1 name, T2 age);
        // {
        //     this->name = name;
        //     this->age = age;

        // }
        void showPerson(); //类外实现
        // {
        //     cout << "xingming" << this->name <<  endl;
        // }
        T1 name;
        T2 age;

        
};
template<class T1, class T2>
Person01<T1 ,T2>::Person01(T1 name, T2 age)
{
        this->name = name;
        this->age = age;
}
template<class T1, class T2>
void Person01<T1, T2>::showPerson()
{
  cout << "xingming" << this->name <<  endl;
}

void test02(){
    Person01<string, int > P2("dfdf" , 333);
    P2.showPerson();
}



int main()
{
    //test();
    test02();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值