forked from dodero/iiss-cpp-examples
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathoptionals.cpp
More file actions
109 lines (91 loc) · 2.33 KB
/
optionals.cpp
File metadata and controls
109 lines (91 loc) · 2.33 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
#include <optional>
#include <iostream>
#include <cstdlib>
class ScreenResolution {
private:
int width;
int height;
public:
ScreenResolution(int width, int height){
this->width = width;
this->height = height;
}
int getWidth() {
return width;
}
int getHeight() {
return height;
}
};
class DisplayFeatures {
private:
std::string size; // In inches
std::optional<ScreenResolution> resolution;
public:
DisplayFeatures(std::string size, std::optional<ScreenResolution> resolution){
this->size = size;
this->resolution = resolution;
}
std::string getSize() {
return size;
}
std::optional<ScreenResolution> getResolution(){
return resolution;
}
};
class Mobile {
private:
long id;
std::string brand;
std::string name;
std::optional<DisplayFeatures> displayFeatures;
// Like wise we can see MemoryFeatures, CameraFeatures etc.
// For simplicity, using only one Features
public:
Mobile(long id, std::string brand, std::string name, std::optional<DisplayFeatures> displayFeatures){
this->id = id;
this->brand = brand;
this->name = name;
this->displayFeatures = displayFeatures;
}
long getId() {
return id;
}
std::string getBrand() {
return brand;
}
std::string getName() {
return name;
}
std::optional<DisplayFeatures> getDisplayFeatures() {
return displayFeatures;
}
};
class MobileService {
public:
int getMobileScreenWidth(std::optional<Mobile> mobile){
int r=0;
if(mobile){
std::optional<DisplayFeatures> df=mobile.value().getDisplayFeatures();
if(df){
std::optional<ScreenResolution> sr=df.value().getResolution();
if(sr){
r= sr.value().getWidth();
}
}
}
return r;
}
};
int main(int argc, char **argv) {
ScreenResolution resolution(750,1334);
DisplayFeatures dfeatures("4.7", resolution);
Mobile mobile(2015001, "Apple", "iPhone 6s", dfeatures);
MobileService mService;
int width = mService.getMobileScreenWidth(mobile);
std::cout << "Apple iPhone 6s Screen Width = " << width << std::endl;
Mobile mobile2(2015001, "Apple", "iPhone 6s", std::nullopt);
int width2 = mService.getMobileScreenWidth(mobile2);
std::cout << "Apple iPhone 16s Screen Width = " << width2 << std::endl;
return 0;
}