Object oriented programming assignment 04 70146903
Object oriented programming assignment 04 70146903
Assignment no 4
Name Talha Nadeem
Section e
Sap id 7014693
#include <iostream>
#include <vector>
#include <memory>
#include <string>
class Pet {
protected:
string name;
int hunger;
int mood;
int energy;
int health;
public:
Pet(string petName) : name(petName), hunger(50), mood(50), energy(50), health(100) {}
void feed() {
hunger -= 15;
if (hunger < 0) hunger = 0;
}
void play() {
if (energy >= 10 && hunger <= 80) {
mood += 10;
hunger += 10;
energy -= 10;
}
}
void rest() {
energy += 15;
if (energy > 100) energy = 100;
health += 5;
if (health > 100) health = 100;
}
void update() {
if (health < 20) {
cout << name << " looks sick. Needs attention!" << endl;
health += 5; // try to recover a bit
}
if (health <= 0) {
cout << name << " has passed away..." << endl;
}
}
public:
User(string n) : name(n) {}
void adopt(shared_ptr<Pet> p) {
if (pets.size() < 3) {
pets.push_back(p);
cout << name << " adopted " << “pet”<< "!\n";
} else {
cout << "You can't adopt more than 3 pets.\n";
}
}
int main() {
vector<shared_ptr<Pet>> store = {
make_shared<Pet>("Buddy"),
make_shared<Pet>("Mittens"),
make_shared<Pet>("Goldie")
};
User user("Talha");
int choice;
while (true) {
cout << "\n1. View Pets\n2. Adopt\n3. Feed\n4. Play\n5. Rest\n0. Exit\n";
cin >> choice;
if (choice == 0) break;
if (choice == 1) {
user.showPets();
} else if (choice == 2) {
if (!store.empty()) {
user.adopt(store.back());
store.pop_back();
} else {
cout << "No more pets available.\n";
}
} else if (choice >= 3 && choice <= 5) {
user.showPets();
cout << "Pick a pet by number: ";
int idx;
cin >> idx;
--idx;
if (choice == 3) pet->feed();
else if (choice == 4) pet->play();
else if (choice == 5) pet->rest();
pet->update();
if (pet->isDead()) {
user.removePet(idx);
}
}
}
Output;