C++ 之友元函数
发布日期:2021-05-14 16:35:31 浏览次数:19 分类:精选文章

本文共 6030 字,大约阅读时间需要 20 分钟。

������������������������������

���C++������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������

������������������������������������������������������������������������������������������������������friend���������������������������������������������������������������������������������������������public���protected������private���������

���������������������������������������

  • ������������������������������������������������������������
  • ������������������������������<<���>>���
  • ���������������������������������const T& operator+(const T&, const int&)���
  • ������������������������������
  • ���������������������������������������������������������������������������������������������������������������������������������������������������������������������


    ���������������������������������

    ������������������������������������������������������������������

    ���������������������������������������

    class Integer {
    public:
    Integer(int value) : m_value(value) {}
    friend const Integer operator+(const Integer& left, const Integer& right);
    friend const Integer operator+(int value, const Integer& other);
    // ������������������������
    private:
    int m_value;
    };
    const Integer operator+(const Integer& left, const Integer& right) {
    return Integer(left.m_value + right.m_value);
    }

    ���������

    • friend const Integer operator+(const Integer& left, const Integer& right); ������Integer������������������������������Integer���������������������������������private���������
    • friend const Integer operator+(int value, const Integer& other); ������������������������Integer������������������������������

    ���������������������������������������������������������������������������Integer���������������������������������������������

    ������������������������������������

    friend ostream& operator<<(ostream& out, const Integer& num) {
    out << num.m_value;
    return out;
    }

    ������������������������������������������������������������_customstream_���������������������������������������������������������������������������������������������������������������������������������


    ������������������������������

    ���C++���������������������������������������������������

  • ���������������������������������������������������������������������������������������������������
  • ������������<<���>>������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������������������������������������������������
  • ���������������������������������������������������������������������������������������������������������������������friend������������������������������������


    ���������������������������������������

    ####������������������������������������������

    #include "Integer.h"
    #include
    using namespace std;
    class Integer {
    public:
    Integer(int value) : m_value(value) {}
    const Integer operator+(const Integer& other) const {
    return Integer(m_value + other.m_value);
    }
    const Integer operator-(const Integer& other) const {
    return Integer(m_value - other.m_value);
    }
    const Integer operator*(const Integer& other) const {
    return Integer(m_value * other.m_value);
    }
    const Integer operator/(const Integer& other) const {
    return Integer(m_value / other.m_value);
    }
    const Integer operator%(const Integer& other) const {
    return Integer(m_value % other.m_value);
    }
    friend const Integer operator+(int intValue, const Integer& other) {
    return Integer(intValue + other.m_value);
    }
    friend const Integer operator-(int intValue, const Integer& other) {
    return Integer(intValue - other.m_value);
    }
    // ���������������������
    const Integer& operator=(const Integer& other) {
    if (this == &other) return *this;
    this->m_value = other.m_value;
    return *this;
    }
    int IntValue() const { return m_value; }
    protected:
    private:
    int m_value;
    };
    ostream& operator<<(ostream& out, const Integer& num) {
    out << num.IntValue();
    return out;
    }
    int main() {
    Integer num1(1024), num2(24);
    Integer num3 = num1 + num2;
    Integer num4(1024) + num1; // 1024 ��� int ���������������������������������
    cout << "num3 = " << num3.IntValue() << endl;
    cout << "num4 = " << num4.IntValue() << endl;
    return 0;
    }

    ���������������������

    • Integer������������������������������������������������������������������������������������������������
    • operator+���operator-������������������������������������������������������Integer������������������������������int������������Integer���������������������������������������������������������
    • operator<<(ostream&, const Integer&) ���������������������������Integer���������������������

    ������������������������������������

  • ���������������������������������������������������������������C++���������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������
  • ���������������ostream���������������������������������������������������������������������������������������������������������������������������-defined������������������������
  • ���������������������������������������������������������������������������������������������������

  • ������������

    ���������������C++���������������������������������������������������������������������������������������������������������������������������������������������friend������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������C++������������������

    上一篇:C++ 之构造函数(拷贝/复制构造函数)
    下一篇:C++ 之const详解

    发表评论

    最新留言

    不错!
    [***.144.177.141]2025年04月21日 15时01分01秒