CS31 Worksheet 6
CS31 Worksheet 6
This worksheet is entirely optional, and meant for extra practice. Some problems will be
more challenging than others and are designed to have you apply your knowledge beyond
the examples presented in lecture, discussion or projects. All exams will be done on paper,
so it is in your best interest to practice these problems by hand and not rely on a compiler.
1. Consider the datatype required to store a day of the week value. Why would an
enumeration be an appropriate choice for this kind of information? Consider the
datatype required to store the last name of a textbook author. Why would an
enumeration not be an appropriate choice for this kind of information?
2. For the declaration:
enum class Color { BLUE = 1, YELLOW, RED = 0, PINK = 2,
BLACK, ORANGE, TEAL=3, GREEN=4 };
Which of the named constant values will have the value 0?
Which of the named constant values will have the value 1?
Which of the named constant values will have the value 2?
Which of the named constant values will have the value 3?
Which of the named constant values will have the value 4?
4. Find the four errors in the following code, and write the fixes.
class Cat {
int m_age;
string m_name;
string m_type;
Cat(int age, string name, string type) {
m_age = age;
m_name = name;
m_type = type;
}
void introduce() {
cout << "Hi! I am a " + type + " cat" << endl;
}
};
class Sheep {
string m_name;
int m_age;
Sheep(int age) {
m_age = age;
}
void introduce() {
cout << "Hi! I am " + m_name + " the sheep" << endl;
}
};
int main() {
Cat schrodinger(5, "Schrodinger's cat", "Korat");
schrodinger.introduce();
cout << schrodinger.m_age << endl;
Sheep dolly(6);
dolly.introduce();
What will the program above successfully print once all the fixes have been made?
struct ZooAnimal
{
string name;
int cageNumber;
int weightDate;
int weight;
};
At first, make the class you create operate identically to the way this structure
behaves.
Then make a second version which hides all the data members from driver code
access and define and create a constructor to initialize all your class’ data members.
6. Complete the class Clock below by implementing each of its member functions as
designed.
class Clock
{
private:
//declarations of data members that are private
int hour; //an hour in the range 1 - 12
int minute; //a minute in the range 0 - 59
int second; //a second in the range 0 - 59
bool isAM; //is the time AM or PM
public:
// publically accessible methods
};