Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- //Demonstrate
- #include <iostream>
- using namespace std;
- class Critter //Class definition > defines a new type, Critter
- {
- private:
- int m_Rage;
- public :
- int m_Hunger; // data member
- Critter(int hunger = 0, int rage = 0); //Constructor prototype
- int GetRage() const;
- void SetRage(int rage);
- void Greet(); //member function prototype
- static int s_Total; //Static member variable declaration // total numbers of Critters obj in existence
- static int GetTotal(); //Static function member prototype
- };
- int Critter::s_Total = 0; //Static member variable initialization
- Critter::Critter(int hunger, int rage): //Constructor definition
- m_Rage(rage)
- {
- cout << "New critter has been born" << endl;
- m_Hunger = hunger;
- ++s_Total;
- }
- int Critter::GetTotal() { //Static member function defition
- return s_Total;
- }
- void Critter::Greet() //Member function definition
- {
- cout << "Hi. I'm a critter. My hunger level is " << m_Hunger << ".\n";
- }
- int Critter::GetRage() const
- {
- return m_Rage;
- }
- void Critter::SetRage(int rage) {
- if (rage < 0)
- {
- cout << "You can set the critters rage to a negative n. \n\n";
- }
- else
- {
- m_Rage = rage;
- }
- }
- int main() {
- Critter crit1(4);
- Critter crit2;
- //crit1.m_Hunger = 9;
- cout << "crit1's hunger lvl is: " << crit1.m_Hunger << ".\n";
- crit2.m_Hunger = 3;
- cout << "crit2's hunger lvl is: " << crit2.m_Hunger << ".\n\n";
- crit1.Greet();
- crit2.Greet();
- crit1.SetRage(6);
- cout << "Get the rage: " << crit1.GetRage() << ".\n\n";
- cout << "\n The number of Critters is: " << Critter::GetTotal() << "\n";
- //I can get access by any object of the class Ex:. cout << crit1.GetTotal();
- return 0;
- }
Add Comment
Please, Sign In to add comment