1// friend functions
2#include <iostream>
3using namespace std;
4
5class Rectangle {
6 int width, height;
7 public:
8 Rectangle() {}
9 Rectangle (int x, int y) : width(x), height(y) {}
10 int area() {return width * height;}
11 friend Rectangle duplicate (const Rectangle&);
12};
13
14Rectangle duplicate (const Rectangle& param)
15{
16 Rectangle res;
17 res.width = param.width*2;
18 res.height = param.height*2;
19 return res;
20}
21
22int main () {
23 Rectangle foo;
24 Rectangle bar (2,3);
25 foo = duplicate (bar);
26 cout << foo.area() << '\n';
27 return 0;
28}
1class className{
2 // Other Declarations
3 friend returnType functionName(arg list);
4};
5
1class cl1{
2private:
3 int x;
4 double u;
5public:
6 cl1();
7 meth1();
8
9friend class cl2; //to make friend class we are using friend keyword
10};
11
12class cl2{
13private:
14 int x;
15 string y;
16public:
17 cl2();
18 meth1();
19
20friend class cl1;
21};