MST

星途 面试题库

面试题:C++ 流运算符重载之简单类输出

假设有一个简单的C++类 `Point`,包含两个整型成员变量 `x` 和 `y`,请编写代码重载 `<<` 运算符,使得能够通过 `cout` 输出 `Point` 对象,格式为 "(x, y)"。
27.8万 热度难度
编程语言C++

知识考点

AI 面试

面试题答案

一键面试
#include <iostream>

class Point {
public:
    int x;
    int y;
};

std::ostream& operator<<(std::ostream& os, const Point& p) {
    os << "(" << p.x << ", " << p.y << ")";
    return os;
}