Practice Writing Classes

This lab will help you write classes.

Part 1: Write Classes

1.1. Write a class called my_class with no functions.

[ ]:

1.2. Make an instance of my_class called inst.

[ ]:

1.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 variable count to zero.

    • Arguments:

      • self: The class instance

    • Returns: None

  • plus_one - Add one to the count

    • Arguments:

      • self: The class instance

    • Returns: None

  • minus_one - Subtract one from the count

    • Arguments:

      • 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.

[ ]:

1.4. Update your Counting Class

Update the Counter class to have an __init__ function that takes an argument:

  • __init__ - Set the class variable count to initial_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.

[ ]: