数据共享保护与操作符重载
  • 板块题目总版
  • 楼主TheSkyFuker
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/3/30 10:11
  • 上次更新2024/3/30 12:46:40
查看原帖
数据共享保护与操作符重载
1274996
TheSkyFuker楼主2024/3/30 10:11

输入样例: 1 2

3 4

输出样例: [1,2] -> 6

代码框架:

#include <iostream>
#include <string>

class A;

namespace myns
{
    class COUT
    {
    public:
        COUT() {}
        void operator<<(const char a[]); // 成员函数
        myns::COUT operator<<(A b);      // 成员函数
    };
    COUT cout;
}

class A
{
public:
    A(int x, int y) : w(x), h(y) {}
    int getLen() const
    {
        if (len < 0)
            len = 2 * (w + h);
        return len;
    }

private:
    // todo ____qcodep____

    int w, h;
    mutable int len = -1;
};

// todo ____qcodep____
                    
using myns::cout;
using std::cin;
int main()
{
    int x, y;
    cin >> x >> y;
    A a1(x, y);
    cout << a1 << "\n"; // first myns::COUT operator<<, then void operator<<
    cin >> x >> y;
    A a2(x, y);
    cout << a2 << "\n";
    return 0;
}
2024/3/30 10:11
加载中...