Answers for "friend function in c++"

C++
0

friend function cpp reference

// friend functions
#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 () {
  Rectangle foo;
  Rectangle bar (2,3);
  foo = duplicate (bar);
  cout << foo.area() << '\n';
  return 0;
}
Posted by: Guest on December-02-2020
0

friend function in c++

class className{
  // Other Declarations
  friend returnType functionName(arg list);
};
Posted by: Guest on September-01-2021

Browse Popular Code Answers by Language