美文网首页
C++的重载

C++的重载

作者: Virtualer | 来源:发表于2019-09-25 15:02 被阅读0次

C++允许函数和运算符多个定义,当重载函数或者重载运算符被调用的时候,编译器将决定使用的参数类型与定义函数中参数类型进行比对,选择对合适的函数,这个过程也就是重载决策。

1. 函数重载

函数同名,参数不同。

#include <iostream>

using namespace std;

class Print
{
public:
    void print(int n)
    {
        cout << "int : " << n << endl;
    }
    void print(float f)
    {
        cout << "float : " << f << endl;
    }
};

int main(int argc, char **argv)
{
    Print p;
    p.print(3);
    p.print(3.14);
    return 0;
}
//输出:
//int : 3
//float : 3.14

2. 运算符重载

大部分运算符都可以进行重载,用于自定义运算类型,当然也有例外的运算符:

#include <iostream>

using namespace std;

class AREA
{
private:
    int length;
    int width;
public:
    AREA()
    {
        length = 0;
        width = 0;
    }
    AREA(int len, int wid)
    {
        length = len;
        width = wid;
    }
public:
    void SetLength(int len)
    {
        length = len;
    }
    void SetWidth(int wid)
    {
        width = wid;
    }
    int GetLength()
    {
        return length;
    }
    int GetWidth()
    {
        return width;
    }
public:
    AREA operator+(const AREA &area)
    {
        AREA temp;
        temp.length = length + area.length;
        temp.width = width + area.width;
        return temp;
    }
    AREA operator-()
    {
        AREA temp;
        temp.length = -length;
        temp.width = -width;
        return temp;
    }
    void operator=(const AREA &area)
    {
        length = area.length;
        width = area.width;
    }
    bool operator==(const AREA &area)
    {
        return length == area.length && width == area.width;
    }
    bool operator<(const AREA &area)
    {
        if (length < area.length)
        {
            return true;
        }
        if (length == area.length && width < area.width)
        {
            return true;
        }
        return false;
    }
    bool operator>(const AREA &area)
    {
        if (length > area.length)
        {
            return true;
        }
        if (length == area.length && width > area.width)
        {
            return true;
        }
        return false;
    }
    friend ostream & operator<<(ostream & output, const AREA &area)
    {
        output << "len : " << area.length << ", wid : " << area.width << endl;
        return output;
    }
    friend istream & operator>>(istream & input, AREA &area)
    {
        input >> area.length >> area.width;
        return input;
    }
    AREA operator()(int a, int b, int c, int d)
    {
        AREA area;
        area.length = a + b;
        area.width = c + d;
        return area;
    }
    AREA operator++()
    {
        ++length;
        ++width;
        return AREA(length, width);
    }
    AREA operator++(int)
    {
        AREA area(length, width);
        ++area.length;
        ++area.width;
        return area;
    }
};

int main(int argc, char **argv)
{
    AREA a, b, c, d, e;
    
    a.SetLength(3);
    a.SetWidth(4);
    b.SetLength(5);
    b.SetWidth(5);

    // 求和
    c = a + b;
    cout << "c.length : " << c.GetLength()<< endl;
    cout << "c.width : " << c.GetWidth()<< endl;
    // 大于
    cout << "a > b : " << (a > b) << endl;
    // 小于
    cout << "a < b : " << (a < b) << endl;
    // 取反
    d = -a;
    cout << "d.length : " << d.GetLength()<< endl;
    cout << "d.width : " << d.GetWidth()<< endl;
    // >>
    // 输入length和width
    cin >> e;
    cout << "e.length : " << e.GetLength()<< endl;
    cout << "e.width : " << e.GetWidth()<< endl;
    // <<
    cout << "a : " << a << endl;
    // ()
    a(1, 2, 1, 2);
    // 前缀++
    ++b;
    // 后缀++
    c++;
    return 0;
}

有一个特殊的运算符是[],不是因为用法特殊,而是集成不到上面这个类里(笑),所以单独写出来

#include <iostream>

using namespace std;

const int ARRAY_SIZE = 10;

class SPECIAL
{
private:
    int array[ARRAY_SIZE];
public:
    SPECIAL()
    {
        for (int i = 0; i < ARRAY_SIZE; ++i)
        {
            array[i] = i;
        }
    }
    int & operator[](int pos)
    { // 这里只是举个使用的示例,最好还是在使用数组下标前确认是否合法
        if (pos < 0)
        {
            return array[0];
        }
        else if (pos > ARRAY_SIZE)
        {
            return array[ARRAY_SIZE - 1];
        }
        return array[pos];
    }
};

int main(int argc, char **argv)
{
    SPECIAL ps;
    cout << "-1 : " << ps[-1] << endl;
    cout << "100 : " << ps[100] << endl;
    cout << "3 : " << ps[3] << endl;
    return 0;
}

常见的运算符重载就这些,如果有遗漏,以后再补充。

相关文章

  • c++的运算符重载

    C++中的加号重载:例:实现复数的相加 C++中的前置++重载:例:点的移动 C++中的后置++重载:例:点的移动

  • 1.2.15_C++ 关系运算符重载

    C++ 重载运算符和重载函数 C++ 语言支持各种关系运算符( < 、 > 、 <= 、 >= 、 == 等等),...

  • C++基础-(重载)

    C++基础 重载 哪些运算符可以被重载:::,.,->,*,?:不能被重载 重载操作符的标志(operator) ...

  • C++运算符重载

    C++运算符重载的实质:运算符重载的实质就是函数重载或函数多态。运算符重载是一种形式的C++多态。目的在于让人能够...

  • 30小时快速精通C++拾遗

    C语言不支持重载,为什么C++支持重载? C语言不支持函数重载,编译时函数名加上_或者其他标识C++为什么能够重载...

  • 第十一章 使用类

    运算符重载 运算符重载是一种形式的C++多态。运算符重载将重载的概念扩展到运算符上,允许赋予C++运算符多种含义。...

  • 1.2.20_C++ 下标运算符 [] 重载

    C++ 重载运算符和重载函数 下标操作符 [] 通常用于访问数组元素。重载该运算符用于增强操作 C++ 数组的功能...

  • C++ 重载 [] = == !=

    一 重载 [] = == != 1.1 重载[] C++ 规定,下标运算符[ ]必须以成员函数的形式进行重载。该重...

  • C++ 重载运算符

    C++重载运算符

  • 2.C++对C的扩展

    重载(overload) C++中,引用了函数重载的概念,函数名同名,参数列表不同形成重载。重载规则: 函数名相同...

网友评论

      本文标题:C++的重载

      本文链接:https://www.haomeiwen.com/subject/nnoiuctx.html