-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.cpp
More file actions
92 lines (71 loc) · 1.8 KB
/
device.cpp
File metadata and controls
92 lines (71 loc) · 1.8 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
#include <iostream>
#include "device.h"
using std::cout;
using std::endl;
using std::ostream;
// Public
ostream &operator<<(ostream &output, const Device &device) {
output << (device.isOn ? "On" : "Off") << " Device";
return output;
}
bool Device::operator== (const Device &other) const {
if (this->hasBatterySupply != other.hasBatterySupply)
return false;
if (this->isOn != other.isOn)
return false;
return true;
}
// Default constructor
Device::Device() {
this->isOn = false;
this->hasBatterySupply = false;
};
// Constructor
Device::Device(bool isOn, bool hasPowerSupply) {
this->isOn = isOn;
this->hasPowerBattery = hasBatterySupply;
};
Device::Device(const Device &device) {
this->isOn = device.isOn;
this->hasBatterySupply = device.hasBatterySupply;
}
// Destructor
Device::~Device() {
cout << "Device getting destroyed"<< endl;
}
void Device::turnOn() {
if (!this->hasBatterySupply) {
throw "You Battery is missing or getting problem and your iPhone can't turn on'";
}
if (this->isOn) {
throw "Already On";
}
this->isOn = true;
}
void Device::turnOff() {
if (!this->hasBatterySupply) {
throw "You Battery is missing or getting problem and your iPhone can't turn off";
}
if (!this->isOn) {
throw "Already Off";
}
this->isOn = false;
}
void Device::plugBatterySupply() {
if (this->hasBatterySupply) {
throw "Your battery was already inserted.";
}
this->hasPowerSupply = true;
}
void Device::unPlugBatterySupply() {
if (!this->hasBatterySupply) {
throw "No battery detected.";
}
this->isOn = false;
this->hasBatterySupply = false;
}
const Device& Device::operator=(const Device &right) {
this->isOn = right.isOn;
this->hasBatterySupply = right.hasBatterySupply;
return *this;
}