-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoopPillarUse.cpp
242 lines (233 loc) · 6.99 KB
/
oopPillarUse.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <iostream>
#include <string>
/**
* * This is Abstraction
* nothing method is pure virtual method,
* this class is now an abstract class
* means creating an object of this class will cause error
* to utilize this class, create a child class of this class
*
* ```cpp
* Person person1("Jaipal", 18);
* ```
* this throws an error as an abstract class
* object is cannot be created
*/
class Person {
/**
* * This is Encapsulation
* private keyword is used,
* every method and variable until
* public or protected keyword will become
* a private member of class until end of class
*
* ```cpp
* std::cout << person1.name << std::endl;
* person1.age= 19;
* ```
* both throws error as private members of class are cannot be accessed
* outside class directly use getters and setters instead
*/
private:
std::string name;
float age;
bool isAlive;
bool isHungry;
public:
/**
* @param name Name of person
* @param age Age of person
*/
Person(std::string name, float age) {
this->name = name;
this->age = age;
this->isAlive = true;
this->isHungry = true;
}
bool getIsHungry() { return this->isHungry; }
std::string eat() {
this->isHungry = false;
return this->name + " has eaten food";
}
void setIsHungry() { this->isHungry = true; }
void setName(std::string newName) {
char confirm;
std::cout << "Are you sure you want to change your name [Yn]: ";
std::cin >> confirm;
if (confirm == 'y' || confirm == 'Y') {
if (this->name == newName) {
std::cout << "Cannot change name, please chose a new name" << std::endl;
} else {
this->name = newName;
}
} else {
}
}
std::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) {
std::std::cout << this->name << " is dead" << std::std::endl;
} else {
}
this->isAlive = aliveStatus;
}
/**
* To make Person class an abstract class,
* we have to create atleast 1 pure virtual method
* so I create 1 pure virtual method named nothing which actually does nothing
* and is just used to make Person class an abstract class
* `virtual void nothing() =0;`
*/
};
/**
* When a class inherites other class,
* it get all default features of parent class
* you can even customize the methods and
* add your own new variables and method and constructor parameter
* * This is Inheritance
*
* ```cpp
* Programmer jai("Jai", 21, 50000);
* ```
* If you are inheriting an abstract class,
* don't forget to do function defination of virtual methods, which were set =0;
* in abstract class
*/
class Programmer : public Person {
private:
int salary;
public:
Programmer(std::string name, float age, int salary1) : Person(name, age) {
this->salary = salary1;
}
int getSalary() { return this->salary; }
void setSalary(int newSalary) { this->salary = salary; }
void nothing() {}
};
class Manager : public Person {
private:
int salary;
public:
Manager(std::string name, float age, int salary1) : Person(name, age) {
this->salary = salary1;
}
int getSalary() { return this->salary; }
void nothing() {}
};
/**
* Multiple classes can inherit a single class
* Programmer and Manager both inherits a single Person class
* it can be used by creating a Person pointer or
* initiaing a variable with Person type
* * This is Polymorphism
*
*:
* ```cpp
* // With person pointer:
* Person* person1;
* person = new Programmer("Jaipal", 50000);
* person::getName();
* ```
* I will use another method of polymorphism in below method
*/
void OOPPillarUse() {
std::string name;
std::cout << "Hello, enter your name: ";
std::cin >> name;
float age;
std::cout << "Enter your age: ";
std::cin >> age;
int salary;
std::cout << "Enter salary: ";
std::cin >> salary;
int choice1;
std::cout << "Choose one: \n1. Programmer\n2. Manager\n3. Enter value: ";
std::cin >> choice1;
if (choice1 == 1 || choice1 == 2) {
Person *person;
if (choice1 == 1) {
person = new Programmer(name, age, salary);
} else {
person = new Manager(name, age, salary);
}
std::cout << "" << std::endl;
std::cout << "Current data" << std::endl;
std::cout << "Name: " << person->getName() << std::endl;
std::cout << "Age: " << person->getAge() << std::endl;
std::cout << "Is Person alive?: " << person->getIsAlive() << std::endl;
std::cout << "Is person hungry: " << person->getIsHungry() << std::endl
<< std::endl;
while (true) {
std::cout << "Options: " << std::endl;
std::cout << "1. Change name" << std::endl;
std::cout << "2. Change age" << std::endl;
std::cout << "3. Get hungry" << std::endl;
std::cout << "4. Eat food" << std::endl;
std::cout << "5. Get current data" << std::endl;
std::cout << "6. Die and exit" << std::endl;
std::cout << "7. Exit or quit" << std::endl;
std::cout << "Enter your choice: ";
int choice;
std::cin >> choice;
std::cout << "" << std::endl;
if (choice == 1) {
std::string newName;
std::cout << "Enter your new name: ";
std::cin >> newName;
person->setName(newName);
} else if (choice == 2) {
float newAge;
std::cout << "Enter new age: ";
std::cin >> newAge;
person->setAge(newAge);
} else if (choice == 3) {
person->setIsHungry();
if (person->getIsHungry()) {
std::cout << person->getName() << " is hungry" << std::endl;
} else {
std::cout << person->getName() << " is not hungry" << std::endl;
}
} else if (choice == 4) {
std::cout << person->eat() << std::endl;
} else if (choice == 5) {
std::cout << "Current data: " << std::endl;
std::cout << "Name: " << person->getName() << std::endl;
std::cout << "Age: " << person->getAge() << std::endl;
if (person->getIsHungry()) {
std::cout << person->getName() << " is hungry" << std::endl;
} else {
std::cout << person->getName() << " is not hungry" << std::endl;
}
if (person->getIsAlive()) {
std::cout << person->getName() << " is alive" << std::endl;
} else {
std::cout << person->getName() << " is dead" << std::endl;
}
} else if (choice == 6) {
person->setIsAlive(false);
std::cout << "Quiting process" << std::endl;
free(person);
break;
} else if (choice == 7) {
std::cout << "Quiting process" << std::endl;
free(person);
break;
} else {
}
std::cout << "" << std::endl;
}
} else {
std::cout << "Invalid input, please do it again" << std::endl << std::endl;
OOPPillarUse();
/* Use recursion on wrong input
* Polymorphism on 1 or 2
*/
}
}
int main() {
OOPPillarUse();
return 0;
}