-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample20.cpp
More file actions
27 lines (21 loc) · 823 Bytes
/
example20.cpp
File metadata and controls
27 lines (21 loc) · 823 Bytes
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
#include <iostream>
using namespace std;
// purpose is to demonstrate using classes in the same file in main classes as well as accessing variables of different classes by other functions
class StankFist
{
public:
StankFist() {stinkyVar = 0;}
private:
int stinkyVar;
friend void stinkysFriend(StankFist &sfo); // because we said friend, we can access stinkyVar from StankFist class, we have to pass in (StankFist &sfo)
};
void stinkysFriend(StankFist &sfo)
{
sfo.stinkyVar = 99;
cout << sfo.stinkyVar << endl;
}
int main()
{
StankFist obj; // stinkyVar = 0 but no output since no cout, StankFist object needs to be created to allow us to access stinkyVar
stinkysFriend(obj); // changes stinkyVar = 99 and output prints since there is cout
}