栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++,模板

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++,模板

模板应用案例

template

void swapl(T &a,T &b)

{

    T temp=a;

    a=b;

    b=temp;

}

template

void mysort(T arr[],int len)

{

    for(int i=0;i

    {

        int max=i;

        for(int j=i+1;j

        {

            if(arr[max]

            {

                max=j;

            }

        }

        if(max!=i)

            {

                swapl(arr[i],arr[max]);

            }

    }

}

template

void printf(T arr[],int len)

{

    for(int i=0;i

    {

        cout<

    }

    cout<

}

void test()

{

    char chararr[]="abdfre";

    int num=sizeof(chararr)/sizeof(char);

    mysort(chararr,num);

    printf(chararr,num);

}

int main()

{

    test();

    system("pause");

    return 0;

}

函数模板

template

T myadd(T a,T b)

{

    return a+b;

}

void test()

{

    int a=1;

    int b=2;

    char c='c';

    cout<(a,c)<

}

int main()

{

    test();

    system("pause");

    return 0;

}

 并不是所有的都可以直接用函数

class Person

{

public:

    Person(string name,int age)

    {

        this->m_age=age;

        this->m_name=name;

    }

    string m_name;

    int m_age;

};

template

bool mycompare(T &a,T &b)

{

    if(a==b)

    {

        return true;

    }

    else

    {

        return false;

    }

}

template<> bool mycompare(Person &p1,Person &p2)

{

    if(p1.m_name==p2.m_name&&p1.m_age==p2.m_age)

    {

        return true;

    }

    else

    {

        return false;

    }

}

void test01()

{

    int a=10;int b=20;

    bool ret=mycompare(a,b);

    if(ret)

    {

        cout<<"niniu";

    }

    else

    {

        cout<<"lala";

    }

}

void test02()

{

    Person p1("to",10);

    Person p2("to",10);

    bool ret=mycompare(p1,p2);

    if(ret)

    {

        cout<<"6";

    }

    else

    {

        cout<<"88";

    }

}

int main()

{

    test02();

    system("pause");

    return 0;

}

类模板

template

class Person

{

public:

    Person(NameType name,AgeType age)

    {

        this->m_Name=name;

        this->m_Age=age;

    }

    void showPerson()

    {

        cout<m_Name<<" "<m_Age<

    }

    NameType m_Name;

    AgeType m_Age;

};

test01()

{

    Person p("张子豪",999);

    p.showPerson();

}

int main()

{

    test01();

    system("pause");

    return 0;

}

类模板对象做函数参数

class Person1

{

public:

    void showPerson1()

    {

        cout<<"Person1 show"<

    }

};

class Person2

{

public:

    void showPerson2()

    {

        cout<<"Person2 show"<

    }

};

template

class MyClass

{

public:

    T obj;

    void func1()

    {

        obj.showPerson1();

    }

    void func2()

    {

        obj.showPerson2();

    }

};

void test01()

{

    MyClassm;

    m.func1();

    //m.func2();

}

int main()

{

    test01();

    system("pause");

    return 0;

}

类模板对象做函数参数

template

class Person

{

public:

    Person(T1 name,T2 age)

    {

        this->m_Name=name;

        this->m_Age=age;

    }

    void showPerson()

    {

        cout<<"姓名"<m_Name<<" "<<"年龄"<m_Age<

    }

    T1 m_Name;

    T2 m_Age;

};

void printPerson1(Person&p)

{

    p.showPerson();

}

void test01

类模板与继承

template

class Base

{

    T m;

};

class Son:public Base

{

public:

};

void test01()

{

    Son s1;

}

template

class Son2:public Base

{

public:

    Son2()

    {

        cout<

    }

    T1 obj;

};

void test02()

{

    Son2S2;

}

int main()

{

    test02();

    system("pause");

    return 0;

}

类模板成员函数类外实现

template

class Person

{

public:

    Person(T1 name,T2 age);

    

    void showPerson();

    

    T1 m_Name;

    T2 m_Age;

};

template

Person::Person(T1 name,T2 age)

{

    this->m_Name=name;

    this->m_Age=age;

}

template

void Person::showPerson()

{

    cout<m_Name<<" "<m_Age<

}

void test01()

{

    Personp("tom",20);

    p.showPerson();

}

int main()

{

    test01();

    system("pause");

    return 0;

}

类模板用

Hpp

类模板友元

#include

#include

#include

#include //读写文件

#define MAX 1000

#include

#include

#define FILE "s111.txt"

using namespace std;

template

class Person

{

    friend void printPerson(Person p)

    {

        cout<

    }

public:

    Person(T1 name,T2 age)

    {

        this->m_Name=name;

        this->m_Age=age;

    }

private:

    T1 m_Name;

    T2 m_Age;

};

void test01()

{

    Personp("tom",20);

    printPerson(p);

}

int main()

{

    test01();

    system("pause");

    return 0;

}

类模板案例,数组类封装

#include

#include

#include

#include //读写文件

#include

#include

#define FILE "s111.txt"

using namespace std;

template

class MyArray

{

public:

    MyArray(int capacity)

    {

        cout<<"有参构造"<

        this->m_Capacity=capacity;

        this->m_Size=0;

        this->pAddress=new T[this->m_Capacity];

    }

    MyArray(const MyArray&arr)

    {

        cout<<"拷贝构造"<

        this->m_Capacity=arr.m_Capacity;

        this->m_Size=arr.m_Size;

        this->pAddress=new T[arr.m_Capacity];

        for(int i=0;im_Size;i++)

        {

            this->pAddress[i]=arr.pAddress[i];

        }

    }

    MyArray &operator=(const MyArray&arr)

    {

        cout<<"operator构造"<

        if(this->pAddress!=NULL)

        {

            delete[]this->pAddress;

            this->pAddress=NULL;

            this->m_Capacity=0;

            this->m_Size=0;

        }

        this->m_Capacity=arr.m_Capacity;

        this->m_Size=arr.m_Size;

        this->pAddress=new T[arr.m_Capacity];

        for(int i=0;im_Size;i++)

        {

            this->pAddress[i]=arr.pAddress[i];

        }

        return *this;

    }

    void Push_Back(const T&val)

    {

        if(this->m_Capacity==this->m_Size)

        {

            return;

        }

        this->pAddress[this->m_Size]=val;//在数组末尾插入数据

        this->m_Size++;

    }

    void pop_Back()

    {

        if(this->m_Size==0)

        {

            return;

        }

        this->m_Size--;

    }

    T &operator[](int index)

    {

        return this->pAddress[index];

    }

    int getCapacity()

    {

        return this->m_Capacity;

    }

    int getSize()

    {

        return this->m_Size;

    }

    ~MyArray()

    {

        if(this->pAddress!=NULL)

        {

            cout<<"析构"<

            delete[] this->pAddress;

            this->pAddress=NULL;

        }

    }

private:

    T*pAddress;

    int m_Capacity;

    int m_Size;

};

void printInArray(MyArray&arr)

{

    for(int i=0;i

    {

        cout<

    }

}

void test01()

{

    MyArray arr1(5);

    for(int i=0;i<5;i++)

    {

        arr1.Push_Back(i);

    }

    cout<<"输出"<

    printInArray(arr1);

    cout<<"容量"<

    cout<<"arr1的大小"<

    MyArray arr2(arr1);

    cout<<"输出2"<

    printInArray(arr2);

    arr2.pop_Back();

    cout<<"arr尾删除后"<

    cout<<"容量"<

    cout<<"arr的大小"<

}

class Person

{

public:

    Person(){};

    Person(string name,int age)

    {

        this->m_Name=name;

        this->m_Age=age;

    }

    string m_Name;

    int m_Age;

};

void printPersonArray(MyArray&arr)

{

    for(int i=0;i

    {

        cout<<"姓名"<

    }

}

void test02()

{

    MyArray arr(10);

    Person p1("孙悟空",999);

    Person p2("韩信",999);

    Person p3("啦啦啦信",999);

    Person p4("韩222信",999);

    Person p5("啦啦20132啦信",999);

    arr.Push_Back(p1);

    arr.Push_Back(p2);

    arr.Push_Back(p3);

    arr.Push_Back(p4);

    arr.Push_Back(p5);

    printPersonArray(arr);

    cout<<"array的容量"<

    cout<<"arr的容量大小"<

}

int main()

{

    test02();

    system("pause");

    return 0;

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847135.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号