0% found this document useful (0 votes)
6 views3 pages

CS31 Worksheet 6

This worksheet provides optional practice problems for CS 31, focusing on concepts such as structs, enumerations, classes, and methods. It includes tasks like defining enumerations, correcting code errors, converting structs to classes, and implementing class member functions. The problems are designed to challenge students' understanding and application of programming concepts beyond classroom examples.

Uploaded by

kellyzzou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

CS31 Worksheet 6

This worksheet provides optional practice problems for CS 31, focusing on concepts such as structs, enumerations, classes, and methods. It includes tasks like defining enumerations, correcting code errors, converting structs to classes, and implementing class member functions. The problems are designed to challenge students' understanding and application of programming concepts beyond classroom examples.

Uploaded by

kellyzzou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CS 31 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.

Concepts: Structs, Enumerations, Class, Public/Private, Simple constructors, Methods and


Data Members

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?

3. Based on the earlier declaration of Color, define an array named colorArray of 4


colors and set each element to have a value that matches its index. In other words,
colorArray[ 0 ] should be set to a color that has the underlying value 0. colorArray[
1 ] should be set to a color value that has the underlying value 1. And so on…

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?

5. Convert the following struct into a class.

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

//Set the clock to the specified time


Clock (int desiredHour, int desiredMinute,
int desiredSecond, string ampmValue );

//Return it in standard notation as in “5:01 PM”


string displayStandard();

//Return it in military notation as in “17:01” for 5:01 PM


string displayMilitary();

};

Create driver code to work with this class.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy