pgrm_015.cpp
Problem Statement
pgrm_015.cpp
friend function
Source Code
cpp
#include<iostream>
using namespace std;
class rectangle {
int length, breadth;
public:
rectangle() {};
rectangle(int l, int b) {length = l; breadth = b;}
int area() {
return length * breadth;
}
friend rectangle double_dimen(const rectangle &);
};
rectangle double_dimen(const rectangle ¶m) {
rectangle res;
res.length = param.length * 2;
res.breadth = param.breadth * 2;
return res;
}
int main() {
rectangle obj;
rectangle obj2(2, 3);
cout << "Area of obj2: " << obj2.area() << endl;
obj = double_dimen(obj2);
cout << "After magnify: \nArea of obj: " << obj.area() << endl;
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
● Author - Amit Dutta