Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //C++ Vector Class Iterations and Dynamic Memory Allocation: By Ben Bollinger
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- //Animal Class
- class Animals{
- public:
- Animals(string name);
- void setHealth(int hp);
- string getName();
- int getHealth();
- void giveHealth();
- private:
- string newName;
- int newHealth;
- };
- Animals::Animals(string name){
- newName = name;
- }
- void Animals::setHealth(int hp){
- newHealth = hp;
- }
- inline string Animals::getName(){
- return newName;
- }
- int Animals::getHealth(){
- return newHealth;
- }
- //Farm Class
- class Farm{
- public:
- Farm(int spaces);
- void AddAnimal(Animals);
- void displayFarm();
- void showHeath();
- void setAllHP();
- void displayMessage(string message);
- int getSize();
- Animals getAnimal(int indexPos);
- private:
- vector<Animals> m_Farm;
- };
- //Create a new farm
- Farm::Farm(int spaces){
- m_Farm.reserve(spaces);
- }
- //Add animal to farm
- void Farm::AddAnimal(Animals newAnimal){
- m_Farm.push_back(newAnimal);
- }
- //Display all animals in farm
- void Farm::displayFarm(){
- int i = 0;
- for(vector<Animals>::iterator iter = m_Farm.begin(); iter != m_Farm.end(); ++iter){
- i++;
- cout << "Animal #" << i << ": " << iter->getName() << "\n";
- }
- }
- //Allow user to see health of all animals
- void Farm::showHeath(){
- cout << "\n";
- for(vector<Animals>::iterator iter = m_Farm.begin(); iter != m_Farm.end(); ++iter){
- cout << iter->getName() << "'s Health is " << iter->getHealth() << "\n";
- }
- }
- //Allow user to set health for all animals
- void Farm::setAllHP(){
- for(vector<Animals>::iterator iter = m_Farm.begin(); iter != m_Farm.end(); ++iter){
- int newHP;
- cout << "Set Health For " << iter->getName() << ": ";
- cin >> newHP;
- iter->setHealth(newHP);
- }
- }
- void Farm::displayMessage(string message){
- cout << "\n---------------";
- cout << "\nFARM BROADCAST: '" << message << "'\n";
- cout << "---------------\n";
- }
- Animals Farm::getAnimal(int indexPos){
- vector<Animals>::iterator iter = m_Farm.begin() + indexPos - 1;
- Animals returnAni = *iter;
- return returnAni;
- }
- int Farm::getSize(){
- int farmSize;
- farmSize = m_Farm.size();
- return farmSize;
- }
- int main(){
- cout << "-------------------------------------------\n";
- cout << "Welcome to C++ Class Farm: By Ben Bollinger\n";
- cout << "-------------------------------------------\n";
- int fSize;
- cout << "Enter How many Animals You Want: ";
- cin >> fSize;
- Farm newFarm(fSize);
- //User enters animals to farm
- for(int i = 0; i < fSize; i++){
- string uInput;
- cout << "Enter Name For Animal " << i + 1 << ": ";
- cin >> uInput;
- Animals newAni(uInput);
- newFarm.AddAnimal(newAni);
- }
- newFarm.displayMessage("Showing Animals");
- newFarm.displayFarm();
- cout << "\n";
- cout << "There are " << newFarm.getSize() << " animals in your farm\n\n";
- //Set and display Farm's health:
- newFarm.setAllHP();
- newFarm.showHeath();
- string yesOrNo;
- newFarm.displayMessage("Change health of an animal? (y/n)");
- cin >> yesOrNo;
- if(yesOrNo == "y"){
- int ani;
- newFarm.displayMessage("Which Animal? (Animal Number)");
- cin >> ani;
- Animals setAni = newFarm.getAnimal(ani);
- int hp;
- newFarm.displayMessage("Set Health to?");
- cin >> hp;
- setAni.setHealth(hp);
- cout << "\n" << setAni.getName() << "'s new health is " << setAni.getHealth() << "\n\n";
- }else{
- newFarm.displayMessage("Game Over");
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement