CSCI 201 Study Aid: Classes

Objects and Classes

A Class Consists of:

The Class Interface or Definition

The parts of the class definition are:

Simple example

The throttle class---a new data type that manipulates and stores the status of a throttle. A toy example of a class that might be used in a flight simulator. There are 4 throttle functions:

  1. a function to set the throttle to the shutoff position
  2. a function to shift a throttle's position by a given amount
  3. a function that returns the fuel flow (percentage of max flow).
  4. a function to verify that the throttle is currently on.
    
    	class Throttle
    	{
    	public:
    		// Mutator functions
    		void shut_off( );
    		void shift(int amount);
    		// Inspector functions
    		double flow( ) const;
    		bool is_on( ) const;
    	private:
    		int position, max_position;
    	};
    

    The header file and the implementation file

    Using a class

    As with any other data type, you declare variables of that type. These variables are called objects or instances.

    
    	Throttle plane, car;
    
    Calling member functions:
    
    	car.shift(3);
           /   |   \   \
          /    |    \  actual parameter
     object    |     \  
      name   member  member 
             access  function
            function
    

    Implementing member functions

    Constructors

    Exercises

    1. Here is the start of a class declaration:
          class Foo
          {
          public:
             Foo(int num);
             void fun1(Foo f);
             void fun2(const Foo f);
             void fun3(Foo f) const;
             ...
          }
      
      Which of the member functions can alter the PRIVATE member variables of the Foo object that activates the function?

      1. Only Foo can alter the private member variables of the object that activates the function.
      2. Two of the functions can alter the private member variables of the object that activates the function.
      3. Three of the functions can alter the private member variables of the object that activates the function. <==
      4. All of the functions can alter the private member variables of the object that activates the function.

    2. What is the primary purpose of a constructor?
      1. To initialize the data members of an object as it is declared. <==
      2. To allow multiple classes to be used in a single program.
      3. To copy an actual calling argument to a function's parameter.
      4. To free the dynamic memory used by an object.

    3. Which kind of functions can access private member variables of a class?
      1. Friend functions of the class
      2. Private member functions of the class
      3. Public member functions of the class
      4. All of the above can access private member variables <==
      5. None of the above

      solution to 1 through 3

    4. Write the definition of a class that allows you to set and get the time of day in hours, minutes and seconds.

      solution

    5. Modify the class definition given above to include a constructor that has 3 parameters corresponding to the time in hours, minutes and seconds. The constructor should specify default values for these parameters.

      solution

    6. Write the implementation of the constructor describe above.

      solution

    7. Write the definition of a class to represent a rectangle. This class should allow you to both set and get the dimensions of the rectangle. It should also provide facilitators for calculating the area and the perimeter of the rectangle.

      solution

    8. Write the implementation of the class describe above.

      solution