-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclassUseWithBash.cpp
132 lines (128 loc) · 3.26 KB
/
classUseWithBash.cpp
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
129
130
131
132
#include <iostream>
#include <string>
using namespace std;
class Person{
private:
string name;
float age;
bool isAlive;
bool isHungry;
public:
Person(string name, float age) {
this->name= name;
this->age= age;
this->isAlive= true;
this->isHungry= true;
}
bool getIsHungry() {
return this->isHungry;
}
string eat() {
this->isHungry= false;
return "Person has eaten food";
}
void setIsHungry() {
this->isHungry= true;
}
void setName(string newName) {
char confirm;
cout << "Are you sure you want to change your name [Yn]: ";
cin >> confirm;
if (confirm == 'y' || confirm == 'Y') {
this->name= newName;
} else {}
}
string getName() {
return this->name;
}
float getAge() {
return this->age;
}
void setAge(float newAge) {
this->age= newAge;
}
bool getIsAlive() {
return this->isAlive;
}
void setIsAlive(bool aliveStatus) {
if (aliveStatus == false) {
cout << this->name << " is dead" << endl;
} else {}
this->isAlive= aliveStatus;
}
};
void PersonDynamicUse() {
string name;
cout << "Hello, enter your name: ";
cin >> name;
float age;
cout << "Enter your age: ";
cin >> age;
Person person(name, age);
cout << "" << endl;
cout << "Current data" << endl;
cout << "Name: " << person.getName() << endl;
cout << "Age: " << person.getAge() << endl;
cout << "Is Person alive?: " << person.getIsAlive() << endl;
cout << "Is person hungry: " << person.getIsHungry() << endl << endl;
while (true) {
cout << "Options: " << endl;
cout << "1. Change name" << endl;
cout << "2. Change age" << endl;
cout << "3. Get hungry" << endl;
cout << "4. Eat food" << endl;
cout << "5. Get current data" << endl;
cout << "6. Die and exit" << endl;
cout << "7. Exit or quit" << endl;
cout << "Enter your choice: ";
int choice;
cin >> choice;
cout << "" << endl;
if (choice == 1) {
string newName;
cout << "Enter your new name: ";
cin >> newName;
person.setName(newName);
} else if (choice == 2) {
float newAge;
cout << "Enter new age: ";
cin >> newAge;
person.setAge(newAge);
} else if (choice == 3) {
person.setIsHungry();
if (person.getIsHungry()) {
cout << person.getName() << " is hungry" << endl;
} else {
cout << person.getName() << " is not hungry" << endl;
}
} else if (choice == 4) {
cout << person.eat() << endl;
} else if (choice == 5) {
cout << "Current data: " << endl;
cout << "Name: " << person.getName() << endl;
cout << "Age: " << person.getAge() << endl;
if (person.getIsHungry()) {
cout << person.getName() << " is hungry" << endl;
} else {
cout << person.getName() << " is not hungry" << endl;
}
if (person.getIsAlive()) {
cout << person.getName() << " is alive" << endl;
} else {
cout << person.getName() << " is dead" << endl;
}
} else if (choice == 6) {
person.setIsAlive(false);
cout << "Quiting process" << endl;
break;
} else if (choice == 7) {
cout << "Quiting process" << endl;
break;
} else {}
cout << "" << endl;
}
}
int main() {
PersonDynamicUse();
return 0;
}