-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprog3.cpp
More file actions
61 lines (45 loc) · 1.07 KB
/
prog3.cpp
File metadata and controls
61 lines (45 loc) · 1.07 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Write a C++ Program with two classes ABC and XYZ with one integer data member
in each class. Write member functions to read and display, place a friend
function called max() in these classes which takes the data members.
Demostrate using the main() function.
Note: Here, one member is a member of one class and the other member is a
member of some other class.
Concept: Friend functions and reference variables.
*/
#include <iostream>
using namespace std;
class ABC;
class XYZ {
private:
int x;
public:
void setVal(int x) { this->x = x; }
friend void max(ABC, XYZ);
};
class ABC {
private:
int y;
public:
void setVal(int y) { this->y = y; }
friend void max(ABC, XYZ);
};
void max(ABC a, XYZ b) {
if (a.y > b.x) {
cout << "The largest number is " << a.y << endl;
} else {
cout << "The largest number is " << b.x << endl;
}
}
int main() {
ABC abc;
XYZ xyz;
int x, y;
cout << "Enter the value for x: ";
cin >> x;
xyz.setVal(x);
cout << "Enter the value for y: ";
cin >> y;
abc.setVal(y);
max(abc, xyz);
return 0;
}