类中的重载

函数重载:扩展已经存在函数的功能

函数重载的规则:

        1. 重载函数本质为不同的函数,有函数名和参数列表作为标识符。

        2. 函数的重载只能发生在同一作用域。(不同的命名空间,类空间,全局空间)

        

类中成员函数有五类:

        1. 构造函数

        2. 拷贝构造函数

        3. 析构函数

        4. 普通成员函数

        5. 静态成员函数

类中的构造函数,拷贝构造函数,普通成员函数,静态成员函数之间可以构成重载。

 全局作用域有两类函数:

          1. 普通成员函数。

          2. 静态成员函数。

全局作用域函数和成员函数不可以构成重载。

#include <stdio.h>

class Test
{
    int i;
public:
    Test()
    {
        printf("Test::Test()
");
        this->i = 0;
    }
    
    Test(int i)
    {
        printf("Test::Test(int i)
");
        this->i = i;
    }
    
    Test(const Test& obj)
    {
        printf("Test(const Test& obj)
");
        this->i = obj.i;
    }
    
    static void func()
    {
        printf("void Test::func()
");
    }
    
    void func(int i)
    {
        printf("void Test::func(int i), i = %d
", i);
    }
    
    int getI()
    {
        return i;
    }
};

void func()
{
    printf("void func()
");
}

void func(int i)
{
    printf("void func(int i), i = %d
", i);
}

int main()
{
    func();
    func(1);
    
    Test t;        // Test::Test()
    Test t1(1);    // Test::Test(int i)
    Test t2(t1);   // Test(const Test& obj)
    
    func();        // void func()
    Test::func();  // void Test::func()
    
    func(2);       // void func(int i), i = 2;
    t1.func(2);    // void Test::func(int i), i = 2
    t1.func();     // void Test::func()
    
    return 0;
}

在C语言中有很多strcpy功能的函数 

char *strcpy(char *dest, const char *src);   

char *strncpy(char *dest, const char *src, size_t n)

功能扩展

char* strcpy(char* buf, const char* str, unsigned int n)
{
    return strncpy(buf, str, n);
}
原文地址:https://www.cnblogs.com/zsy12138/p/10830591.html