Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Clever Crow In this quest you get to implement an entire class. This class is called Pet. In addition to implementing this class, you also
Clever Crow
In this quest you get to implement an entire class.
This class is called Pet. In addition to implementing this class, you also get to implement a fun little function to make up names for the millions of pets you will be able to create.
Your first miniquest Make a name for yourself
This is just warm up before you get to implement a class. You must implement:
string makeanameint len;
When I invoke this method, I will supply it with a length parameter which tells you my desired name length.
You must then algorithmically manufacture a name and return it to me
Before you start, read about the rand function and srand in the book, modules or reference material online. Ask questions in the forums to clarify if necessary.
Since I need to match up names generated by me and you to make sure your implementation is to spec, there are a few precautions you have to follow in implementing this function.
Specifically, you must:
define your vowels to be the characters in the string "aeiou"
define your consonants to be the characters in the string bcdfghjklmnpqrstvwxyz
The name you return must be created by alternately selecting a random letter from
each of the above two strings.
The very first letter of your name must be either a vowel or consonant chosen using the
condition rand Specifically, if your random is even, then the first letter of
the name must be a consonant.
To select the index of the letter to use in each iteration, you must invoke rand
exactlyonce.Forexample:rand consonants.length
IMPORTANT: You must NOT invoke srand anywhere in your code.
Now you must implement the Pet class as specified below and incorporate makeaname into it as a public static helper method.
Pageof
The Pet Class
Your Pet object ought to contain the following four private members of which one is also static static means that there is only one copy of the variable and it is attached to the class definition the blueprint of the class rather than several copies, one in each object created from that class definition. Discuss this in the forums to understand more clearly if necessary.
string name;
long id;
int numlimbs;
static sizet population;
Note that I have prefixed each member's name with an underscore. This is a convention some programmers follow to help fellow programmers instantly spot private members in code manipulating an object. Try this strategy in this lab and see if it helps. My starter code will be set up to help you do it this way. It has the nice sideeffect that you can now freely use the words name, id and numlimbs as names for your local variables without needing to use this to disambiguate.
Miniquest Pet class constructors and destructor
Implement the default and nondefault constructors of your Pet class as follows:
Petstring name long id int numlimbs ;
By providing the default values for your members in the declaration, you can skip defining an explicit default constructor. Your implementation of the above constructor gives you both the default and nondefault varieties. Both will be tested. Make sure to retain the exact default values you see above.
~Pet;
The public destructor of the Pet object, with the above signature, will be automatically invoked by the compiler when the memory of a Pet object is to be released back into the resource pool. Most of the time you don't need to implement explicit destructors for simple tasks. In this case, we're using it because it gives us an opportunity to experiment with static or class variables. Read up what a class variable is and how it is different from regular class members.
Your destructor must decrement the value of your population member Just like a constructor should increment it Make sure that after you create and destroy an arbitrary number of Pet objects your population count always shows
Miniquest Getters aka accessors
Implement getters for each of your members. They should simply return the requested value while leaving the object untouched.
Pageof
To accidentally prevent this from happening, we often annotate such methods with the const keyword purple below This tells the compiler to have our back. It will look out for us if we do something stupid like make getters or functions it calls change an object's properties.
string getname const;
long getid const;
int getnumlimbs const;
Miniquest Setters aka mutators
Implement setters for each of the three members as follows:
bool setnamestring name;
bool setidlong id;
bool setnumlimbsint numlimbs;
Note that each setter returns a boolean value. Whenever possible, I recommend you make your setters return a successfailure value so the caller can know whether they succeeded. I think it is usually overkill to throw an exception from a setter. So this habit will serve you well even after you learn about exceptions and start coding advanced structures.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started