-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorage_Display.cpp
More file actions
46 lines (39 loc) · 1.09 KB
/
Storage_Display.cpp
File metadata and controls
46 lines (39 loc) · 1.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
#include <iostream>
#include <string>
class Storage;
class Display
{
private:
bool m_displayIntFirst{};
public:
explicit Display(bool displayIntFirst)
: m_displayIntFirst{displayIntFirst}
{}
void displayStorage(const Storage &storage); // compiler needs to know what the Storage is
};
class Storage
{
private:
int m_nValue{};
double m_dValue{};
public:
explicit Storage(int nValue, double dValue)
: m_nValue{nValue}, m_dValue{dValue}
{}
// friend can access all members in this class
friend void Display::displayStorage(const Storage &storage); // compiler needs to know Full context of class Display, so it should be front of this class
};
void Display::displayStorage(const Storage &storage)
{
if(m_displayIntFirst)
std::cout << storage.m_nValue << ' ' << storage.m_dValue << '\n';
else
std::cout << storage.m_dValue << ' ' << storage.m_nValue << '\n';
}
int main()
{
Storage s1{5, 6.7};
Display display_mode{false};
display_mode.displayStorage(s1);
return 0;
}