-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFriendFunction.cpp
More file actions
128 lines (121 loc) · 3.09 KB
/
Copy pathFriendFunction.cpp
File metadata and controls
128 lines (121 loc) · 3.09 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Friend Function
#include <iostream>
using namespace std;
class House
{
private:
int chocolates;
int jellies;
int pastries;
public:
House() //Default constructer
{
chocolates = 0;
jellies = 0;
pastries = 0;
}
House(int NumOfChocolates, int NumOfJellies, int NumOfPastries) //Parametrized constructor
{
chocolates = NumOfChocolates;
jellies = NumOfJellies;
pastries = NumOfPastries;
}
House(House &object) //copy constructor
{
chocolates = object.chocolates;
jellies = object.jellies;
pastries = object.pastries;
}
void eat(); // Instant mamber function
void set()
{
cout << "\nEnter the value of chocolates => ";
cin >> chocolates;
cout << "Enter the value of jellies => ";
cin >> jellies;
cout << "Enter the value of pastries => ";
cin >> pastries;
cout << "*******************************************" << endl;
}
void show()
{
cout << "Chocolates left => " << chocolates << "\njellies left => " << jellies << "\npastries left => " << pastries << endl;
cout << "**************************************************" << endl;
}
friend House hungary(House); //Friend function
~House() //Destructor
{
}
};
void House::eat()
{
int n, m;
cout << "What you would like to eat:->" << endl;
cout << "1. Chocolates" << endl;
cout << "2. Jellies" << endl;
cout << "3. pastries" << endl;
cout << "4. nothing" << endl;
cout<<"******************************"<<endl;
while (1)
{
cout << "\tEnter your choice => ";
cin >> n;
switch (n)
{
case 1:
cout << "\t\tEnter the amount of chocolates you want to eat => ";
cin >> m;
if (m <= chocolates)
{
chocolates -= m;
}
else
{
cout << "\t\tDoesn't having the sufficient amount of chocolates" << endl;
}
break;
case 2:
cout << "\t\tEnter the amount of jellies you want to eat => ";
cin >> m;
if (m <= jellies)
{
jellies -= m;
}
else
{
cout << "\t\tDoesn't having the sufficient amount of jellies" << endl;
}
break;
case 3:
cout << "\t\tEnter the amount of pastries you want to eat => ";
cin >> m;
if (m <= pastries)
{
pastries -= m;
}
else
{
cout << "\t\tDoesn't having the sufficient amount of pastries" << endl;
}
break;
case 4:
exit(0);
break;
default:
cout << "\t\tInvalid choice " << endl;
break;
}
}
}
House hungary(House &obj) //Friend function
{
obj.eat();
return obj;
}
int main()
{
House Rohan; //creation of object
Rohan.set();
hungary(Rohan);
Rohan.show();
}