c++中friend function

来源:互联网 发布:c语言编程工具哪个好 编辑:程序博客网 时间:2024/06/11 18:41
#include <iostream>using namespace std;class Rectangle {    int width, height;public:    Rectangle () {}    Rectangle (int x, int y) : width (x), height (y) {}    int area () {return width * height;}    friend Rectangle duplicate (const Rectangle&) ;};Rectangle duplicate (const Rectangle& param) {    Rectangle res;    res.width = param.width * 2;    res.height = param.height * 2;    return res;}int main(int argc, char const *argv[]){    Rectangle foo;    Rectangle bar (2, 3);    foo = duplicate (bar);    cout << foo.area() << "\n";    return 0;}