0% found this document useful (0 votes)
11 views

Object oriented programming assignment 04 70146903

This document is an object-oriented programming assignment that implements a simple pet simulation in C++. It defines a Pet class with attributes and methods for managing pet states like hunger, mood, energy, and health, as well as a User class for adopting and managing pets. The main function allows users to interact with the simulation by viewing, adopting, feeding, playing with, and resting their pets.

Uploaded by

70146903
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Object oriented programming assignment 04 70146903

This document is an object-oriented programming assignment that implements a simple pet simulation in C++. It defines a Pet class with attributes and methods for managing pet states like hunger, mood, energy, and health, as well as a User class for adopting and managing pets. The main function allows users to interact with the simulation by viewing, adopting, feeding, playing with, and resting their pets.

Uploaded by

70146903
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Object oriented programming

Assignment no 4
Name Talha Nadeem

Section e

Sap id 7014693
#include <iostream>
#include <vector>
#include <memory>
#include <string>

using namespace std;

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 status() const {


cout << name << " -> Hunger: " << hunger << ", Mood: " << mood
<< ", Energy: " << energy << ", Health: " << health << endl;
}

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;
}
}

bool isDead() const { return health <= 0; }


};
class User {
string name;
vector<shared_ptr<Pet>> pets;

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";
}
}

void showPets() const {


if (pets.empty()) {
cout << name << " has no pets.\n";
return;
}

for (size_t i = 0; i < pets.size(); ++i) {


cout << i + 1 << ". ";
pets[i]->status();
}
}

vector<shared_ptr<Pet>>& getPets() { return pets; }

void removePet(int idx) {


if (idx >= 0 && idx < pets.size()) {
pets.erase(pets.begin() + idx);
}
}

string getName() const { return name; }


};

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 (idx < 0 || idx >= user.getPets().size()) {


cout << "Invalid choice.\n";
continue;
}

auto pet = user.getPets()[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);
}
}
}

cout << "Thanks for using Pet Simulator!\n";


return 0;
}

Output;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy