#函数模板

#函数模板,第1张

模板技术 类型参数化 编写代码可以忽略类型

为了让编译器区分是普通函数还是模板函数

template// template  //告诉编译器 不报错 作用域只对后面第一个函数起作用模板函数内部可以多写  template

void MySwap(T& a,T& b)

{

   T temp=a;

   a=b;

   b=temp;

}

void test01()

{

   int a=10;

   int b=20;

//1.自动类型推导

  MySwap(a,b );

//2.显式的指定类型

MySwap(a,b);

}

函数模板和普通函数的区别?

1. 函数模板不允许自动类型转化

2.普通函数能够自动进行类型转化

函数模板和普通函数在一起调用规则:

1.函数模板可以像普通函数那样被重载

2.c++编译器优先考虑普通函数

3.如果函数模板可以产生一个更好的匹配,那么选择模板

4.可以通过空模板实参列表的语法限定编译器只能通过模板匹配

原理:函数模板->模板函数->被调用

类模板

  类模板和函数模板的定义和使用类似,我们已经进行了介绍。有时,有两个或多个类,其功能是相同的,仅仅是数据类型的不同。

1.类模板用于实现类所需数据的类型参数化。

2.类模板在表示如数组、表、图等数据结构显得特别重要,这些数据结构的表示和算法不受所包含的数据类型的影响。

template

class Person{

public:

      Person(T id,T age){

         mAge=age;

Mid=id;

}

public:

     T mid;

     T mAge;

};

void test(){

  //函数模板在调用的时候,可以自动类型推导

  //类模板必须显式指定类型

 Person p(10,20);

 p.show();

}

 类模板派生普通类_类模板派生类模板

template

class Person{

public:

     Person(){

    mAge=0;

}

public:

    T mAge;

};

//为什么?

//类区定义对象,这个对象是不是需要编译分配内存

class SubPerson:public Person

{

};

template

class Animal{

public:

       void Voice(){

         cout<

}

public:

      T mAge;

};

template

class Cat:public Animal{

};

int maim(void)

{

  Cat cat;

}

普通类的.h和.cpp分离编写方式

#ifndef PERSON_H  //防止头文件被重复包含 ct+shift+u  #pragma once  

#define   PERSON_H

#include

#include

using namespace std;

class Person{

public:

     Person(string name,int age);

void  Show();

public:

   string mName;

  int  mAge;

int mID;

};

#include"Person.h"

Person::Person(string name,int age){

this->mName=name;

this->mAge=age;

void Person::Show(){

 cout<<"Name:"<mName<<"Age:"<mAge<

}

类模板_类内核类外实现_类模板和友元

1.类模板实现在类内实现

template

class Person{

public:

    Person(T1 Name,T2 age)

{

   this->mNmae=name;

  this->mAge=age; 

}

void show()

{

  cout<<

}

public:

   T1 mName;

   T2 mAge;

};

void test01()

{

   Person p("AAA",20);

   p.show();

}

 不要滥用友元:

template class Person;

template  void PrintPerson(Person& p);

 template

class Person{

public:

    //重载左移 *** 作符

 

 friend ostream& operate<<(ostream& os,Person& p);

 //普通友元函数

   friend void PrintPerson( Person &p);

  Person(T age,T id);

void Show();

private:

  T mAge;

T  mID;

};

template

Person::Person(T age,T id)

{z

    this->mID=id;

    this->mAge=age;

}

template

void Person::Show(){

 cout<<"Age:"<

}

//重载左移运算 *** 作符

template

ostream& operate<<(ostream& os,Person& p)

{

    os<<"Age:"<

   return os;

}

template

void PrintPerson(Person& p){

    cout<<"Age:"<

}

类模板外部实现:

首先在.h文件中声明

#pragma once

template

class Person{

public:

     Person(T age);

    void Show();

public:

     T age;

}; 

 .cpp文件定义

#include"Person.h"

template

Person::Person(T age){

   this->age=age;

}

template

void Person::Show(){

    cout<<"Age: "<

}

 实现:

#include"Person.cpp"

int main()

{

    Person p(10);

    p.show();

}

 类模板中的static关键字

template

class Person{

public:

      static int a;

};

//类外初始化

template

int Person::a=0;

int main()

{

    Personp1,p2,p3;

    Personpp1,pp2,pp3;

p1.a=10;

pp1.a=100;

cout<

cout<

//实力化模板类后每个对象的静态成员都是单独的,只是同一个类模板共享一个静态成员

//比如,p1,p2,p3输出的静态成员都为10     pp1,pp2,pp3输出的静态成员都为100

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/3002539.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-27
下一篇 2022-09-27

发表评论

登录后才能评论

评论列表(0条)

保存