Lab: Writing Classes#
This lab will help you write classes.
Part 1: Write Classes#
1. Empty Class#
Write a class called my_class
with no functions.
[ ]:
2. Instance#
Make an instance of my_class
called inst
.
[ ]:
3. Write a Counting Class#
Write a class called Counter
. The class should have the following member variables:
count
: an integer
The class should have the following member functions:
__init__
- Set the class variablecount
to zero.Arguments:
self
: The class instance
Returns: None
plus_one
- Add one to the countArguments:
self
: The class instance
Returns: None
minus_one
- Subtract one from the countArguments:
self
: The class instance
Returns: None
print_count
- Print the current count value.Arguments:
self
: The class instance
Returns: None
Test your class by creating an instance of it and using all member functions.
[ ]:
4. Update your Counting Class#
Update the Counter
class to have an __init__
function that takes an argument:
__init__
- Set the class variablecount
toinitial_count
Arguments:
self
: The class instance.initial_count
: The initial value of the count variable.
Test your class by creating an instance of it and using both member functions.
[ ]: