Original Post
Hello! I recently made a thread about making multiple files, but this thread has a totally different question, so I made him his own home. [lol] Question: I made a project, and split it up into 4 different files, 2 .cpp files and 2 header files. So for my question, In main(), i never include the pet.cpp file, but it knows the function definitions. How does this work, and why? main.cpp pet_class.h pet.cpp Library.h
#include "pet_class.h"
#include "Library.h"
void Wait()
{
std::cout << "\n\nPress ENTER to continue.";
std::cin.ignore();
std::cin.get();
}
int main()
{
Pet pet(10,10,10);
int menu;
bool quit = false;
do
{
system("cls");
std::cout << "Welcome to the Pet Caretaking Center!\n"
<< "Programmed by: Brandon Wall\n\n"
<< "1 = Listen to your pet\n"
<< "2 = Feed your pet\n"
<< "3 = Play with your pet\n"
<< "4 = See Pet's Stats\n"
<< "5 = quit\n\n"
<< "Selection: ";
std::cin >> menu;
switch (menu)
{
case 1:
pet.Talk();
break;
case 2:
pet.Feed();
break;
case 3:
pet.Play();
break;
case 4:
pet.Stats();
break;
case 5:
quit = true;
break;
default:
std::cout << "\nSorry, " << menu << " is an invalid option.";
Wait();
}
}while(!quit);
return 0;
}
#include "Library.h"
class Pet
{
public:
Pet(int hunger, int lazy, int boredom): m_hunger(hunger), m_boredom(boredom), m_lazy(lazy)
{}
void Talk(const int bored = 3);
void Feed(const int food = 4);
void Play(const int activity = 4);
void Stats();
void CheckStats();
std::string SetMood();
private:
int m_hunger;
int m_boredom;
int m_lazy;
int m_mood;
void PassTime(const int time = 2);
};
#include "Library.h"
#include "pet_class.h"
//////////////////////////////////////////////////////////////////////////////////////
//
// GLOBAL FUNCTION DEFINITION
//
//////////////////////////////////////////////////////////////////////////////////////
void Pause()
{
std::cout << "\n\nPress ENTER to continue.";
std::cin.ignore();
std::cin.get();
}
//////////////////////////////////////////////////////////////////////////////////////
//
// CLASS PET FUNCTION DEFINITIONS
//
//////////////////////////////////////////////////////////////////////////////////////
std::string Pet::SetMood()
{
m_mood = (m_boredom + m_lazy + m_hunger);
std::string mood;
if(m_mood >= 15)
mood = "Happy";
else if(m_mood >= 10)
mood = "Fine";
else if(m_mood >= 5)
mood = "Sad";
else if(m_mood < 5)
mood = "Angry";
else
{
m_mood = 0;
mood = "Sad and Angry";
}
return mood;
}
void Pet::CheckStats()
{
if(m_hunger < 0)
m_hunger = 0;
if(m_boredom < 0)
m_boredom = 0;
if(m_lazy < 0)
m_lazy = 0;
}
void Pet::PassTime(const int time)
{
m_hunger -= time;
m_boredom -= time;
m_lazy -= time;
}
void Pet::Talk(const int bored)
{
std::vector<std::string> phrase;
phrase.push_back("Pet: Bark Bark Baaaark!!!");
phrase.push_back("Pet: Arrrrrrr!!!");
phrase.push_back("Pet: Raaaaawwr!!!!");
phrase.push_back("Pet: Ahhhhwwwww!!!");
// Gets random phrase for pet to say to the user
random_shuffle(phrase.begin(), phrase.end());
std::cout << "\n" << phrase[0];
m_boredom += bored;
PassTime();
Pause();
}
void Pet::Feed(const int food)
{
std::cout << "\nPet: Buuuuuurp!!!";
m_hunger += food;
PassTime();
Pause();
}
void Pet::Play(const int activity)
{
std::cout << "\nPet: Weeeeeeeeee! Bark! :)";
m_lazy += activity;
PassTime();
Pause();
}
void Pet::Stats()
{
system("cls");
CheckStats();
std::string current_mood = SetMood();
std::cout << "Pat Caretaker Stats:\n"
<< "\nHunger Level = " << m_hunger
<< "\nPlayful Level = " << m_lazy
<< "\nBoredom Level = " << m_boredom
<< "\n\nOverall Mood: " << current_mood
<< "\n\nHint: The Higher the Level, the Happy your Pet is.";
Pause();
}
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>