Basic Structure
Your assignment is to learn how to use the Dog class, which is reprinted below, but also available as attachments (Dog.cpp, Dog.h). Download and save the attachments as your starting point.
You will then create a file called Lab5.cpp, which contains a main() function, that uses the Dog class in the following ways:
- Create at least two different dogs
- Give the dogs to people
- Abuse and pat the dogs several different times
- Talk to the dogs
Use the following source files as the starting skeleton for your program:
// File: Dog.cpp // // Implementation of Dog class. // #include <iostream> #include "Dog.h" // ------------- Constructors ---------------------- // Default constructor, does nothing // Dog::Dog() { // do nothing } // Alternate constructor // Note the use of member initializers. // Dog::Dog(const string &name, unsigned int birthyear) : m_name(name), m_birthyear(birthyear), m_owner("I'm an orphan"), m_happiness(0) { // do nothing } // ------------- Accessors ------------------------- // // These do what you think. string Dog::GetOwner() const { return m_owner; } void Dog::SetOwner(const string &owner) { m_owner = owner; } string Dog::GetName() const { return m_name; } unsigned int Dog::GetAge(unsigned int current_year) const { return current_year - m_birthyear; } // ------------- Mutators -------------------------- // See Lab 3 description for what these mean. void Dog::Abuse() { m_happiness--; } void Dog::Pat() { m_happiness++; } // ------ Service/Facilitator Methods -------------- // See Lab 3 description for what these mean. string Dog::GetHappiness() const { if(m_happiness > 1) { return "happy"; } else if(m_happiness < 1) { return "sad"; } else { return "ok"; } } void Dog::Talk() const { cout << "woof" << endl; }
// File: Dog.h // // Modified class definition from Lab 3. // Added #ifndef ... macros, default constructor, // GetName accessor. #ifndef DOG_H #define DOG_H #include <string> using namespace std; class Dog { public: // Default constructor, does nothing. Dog() ; // Creates a dog. A dog must have a name and a birth year. Dog(const string &name, unsigned int birthyear); // Returns the name of the owner string GetOwner() const; // Gives the Dog a new owner void SetOwner(const string &owner); // Returns the name of the dog string GetName() const ; // Returns the age of the dog at a given year unsigned int GetAge(unsigned int current_year) const; // Abuse the dog void Abuse(); // Pat the dog void Pat(); // See how the dog is feeling string GetHappiness() const; // Prompt the dog to talk to you void Talk() const; private: // Name of Dog string m_name; unsigned int m_birthyear; // Name of owner string m_owner; // Happiness rating from Pat() and Abuse() int m_happiness; }; #endif