CS12, Lab 3

Week of January 21-25

 

 

Topics: 

·        Memberwise copy

·        Constructors

·        Functions with default parameters

·        Overloading (two functions with the same name)

·        Private methods

·        Methods calling other methods.

 

 

You will be modifying the files from Lab 2.  

 

To do: 

·      &nbsC; Copy the files counter.h and counter.cpp from Lab 2 into a new folder.

·        Add a private mutator method setCount(int value)that sets the value of count to value. 

·        Change the constructor (or add a constructor) so that a Counter object can be initialized with or without a starting value.  The default start value for the counter, if none was specified, should be zero.  When setting the value of the counter, the constructor should use the private method setCount(int).

·        In your main program (main.cpp),

·        Create a Counter object with some initial value other than zero. 

·        Create a second Counter object with no initial value specified.

·        Verify the values of the Counters (print them out).

·        Assign to one of your objects the value of the other.

·        Compare the values of the two counters to see that they are indeed the same.  Do private data members get copied on direct assignment?

 

Extra credit

Experiment:

  1. What happens when you compile a program with two constructors, one that has no parameters and another with one parameter with a default value?  Use an empty main().  Does your program compile?
  2. If the program does compile, try declaring in main() an object with no parameters.  Does your program compile now?
  3. If the program compiles and runs, how can you check which constructor gets called if no argument is specified?