C++函数——重载/模板

  • 重载

overload:重载
override:覆盖
多态:子类继承父类,子类覆盖父类,父类指向之类
函数重载即相同的函数名,但是参数列表不同时,实现不同的功能

//干吃
void eating();
//吃东西
void eating(string);
//吃非常多
void eating(string[]);
//定个日子吃
void eating(int, int, int);

1、这里需要注意一下,eating(string)和eating(string&)的特征标是相同的,调用时都可以写作eating(string)
2、特征标:重载–编译器在编译时,根据参数列表对函数进行重命名,不允许有同名的函数。

重载时:void swap(int, int);——>编译时:命名为swap_int_int;
重载时:void swap(double, double);——>编译时:命名为swap_double_double;

//头文件

#ifndef CHAPTURE13DEMO_H_INCLUDED
#define CHAPTURE13DEMO_H_INCLUDED

#include <iostream>

using namespace std;

#endif // CHAPTURE13DEMO_H_INCLUDED
//干吃
void eating();
void eating()
{
    cout << "干吃面值得拥有!" << endl;
}
//粉皮10种
void eating(int);//声明函数
void eating(int food_count)//定义函数
{
    cout << food_count << "种擀面皮擀粉皮!" << endl;
}
//吃东西
void eating(string);
void eating(string food_name="福鼎肉片")
{
    cout << "我贼喜欢吃" <<  food_name << endl;
}
//吃非常多
void eating(string[],int len);
void eating(string* ptr_foods, int len)
{
    cout << "我喜欢的食物有\t" ;
    for(int i = 0; i < len; i++)
    {
        cout << *(ptr_foods + i) << "\t";
    }
    cout << endl;

}
//定个日子吃
void eating(int, int, int);
void eating(int year, int month, int day)
{
    cout << "我打算在" <<  year << "年" << month << "月" << day << "号吃湘格里辣!!!" << endl;
}

//主文件

#include <iostream>
#include "chapture13demo.h"//引入头文件
using namespace std;

int main()
{
    string foods_name[] = {"咖喱鸡肉饭","福鼎肉片","莆田卤面"};
    int year = 2019, month = 11, day = 22;
    //函数重载对比,不同的输入有不同的输出
    eating();
    eating(10);
    eating("醋溜土豆丝");
    eating(foods_name, 3);
    eating(year, month, day);
    return 0;
}

//程序输出

干吃面值得拥有!
10种擀面皮擀粉皮!
我贼喜欢吃醋溜土豆丝
我喜欢的食物有  咖喱鸡肉饭      福鼎肉片        莆田卤面
我打算在20191122号吃湘格里辣!!!

Process returned 0 (0x0)   execution time : 0.085 s
Press any key to continue.
  • 模板

上面的重载可以看到,虽然函数名一样,但是实现不同的功能时,函数设置非常的麻烦,这里就可以用函数模板来解决(这里声明一下:模板和重载没有太大的关联,或者说是相互独立的)。模板实际上就是建立一个通用函数
1、函数定义时不指定具体的数据类型,使用虚拟类型代替
2、函数被调用时,编译器根据实参反推数据类型一类型的参数化

//模板头与函数声明/定义永远是不可分割的整体
template <typename 类型参数1, typename 参数类型2,....>
//返回值类型 函数名 (形参列表)
{ 
    //在循环体中可以使用类型参数
}
//例如:三部分缺一不可。T在这里面只是指某一种类型,而非自由自在的多种类型
template<typename T> void Swap(T&, T&);
template<typename T>
void Swap(T& a, T& b){}

早期使用的template <calss T>和目前的template <typename T>一样

//头文件

#ifndef CHAPTURE13DEMO_H_INCLUDED
#define CHAPTURE13DEMO_H_INCLUDED

#include <iostream>

using namespace std;

#endif // CHAPTURE13DEMO_H_INCLUDED
//函数模板
//例1:打印斐波那契数列
template<typename T> void Swap(T&, T&);
template<typename T>
void Swap(T& a, T& b)
{
    T temp;
    cout << "斐波那契数列为:" << a << '\t' << b << '\t';
    while(b < 1024)
    {
        temp = b + a;
        a = b;
        b = temp;
    cout << b << "\t";
    }
    cout << endl;
}
//例2:冒泡排序
template<typename T> void Sort(T ptr_array[], int len);//T是属于同一类的,所以len的类型最好指定int
template<typename T>
void Sort(T ptr_array[], int len)
{
    T temp;
    cout << "冒泡排序结果为: ";
    for(int i = 0; i < len - 1;i++)
    {
        for(int j = 0; j < len - i - 1; j++)
        {
            if(ptr_array[j] > ptr_array[j + 1])//冒泡排序,大的数放前面
            {
                temp = ptr_array[j];
                ptr_array[j] = ptr_array[j+1];
                ptr_array[j+1] = temp;
            }
        }
    }
    for(int i = 0; i < len; i++)
    {
        cout << ptr_array[i] << "\t";
    }
    cout << endl;
}

//主文件

#include <iostream>
#include "chapture13demo.h"
using namespace std;

int main()
{
    int nums1[] = {11,123,43,45,65,76,22,31};
    double nums2[] = {1112,123123,43,45,6513,769,220,310};
    double nums3[] = {11.12,12.3123,4.3,4.5,6.513,7.69,22.0,3.10};
    int num1 = 1, num2 = 1;
    Swap(num1,num2);//斐波那契数列
    Sort(nums1, 8);//整型
    Sort(nums2, 8);//double型
    Sort(nums3, 8);//浮点型

    return 0;
}

//程序输出

斐波那契数列为:1       1       2       3       5       8       13      21      34      55      89      144     233
377     610     987     1597
冒泡排序结果为: 11     22      31      43      45      65      76      123
冒泡排序结果为: 43     45      220     310     769     1112    6513    123123
冒泡排序结果为: 3.1    4.3     4.5     6.513   7.69    11.12   12.3123 22

Process returned 0 (0x0)   execution time : 0.099 s
Press any key to continue.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值