C++ assignments
myCodeMateExercise10a05/.DS_Store
__MACOSX/myCodeMateExercise10a05/._.DS_Store
myCodeMateExercise10a05/myCodeMateDirectionsEx10a05.txt
myCodeMate Directions - Exercise 10.05 Define and test a class for a type called CounterType. An object of this type is used to count things, so it records a count that is a nonnegative whole number. Include a default constructor that sets the counter to zero and a constructor with one argument that sets the counter to the value specified by its argument. Include member functions to increase the count by one and to decrease the count by one. Be sure that no member function allows the value of the counter to become negative. Also include a member function that returns the current count value and one that outputs the count to a stream. This last function should have one formal parameter of type ostream. Embed your class definition in a test program.
__MACOSX/myCodeMateExercise10a05/._myCodeMateDirectionsEx10a05.txt
myCodeMateExercise10a05/myCodeMateExercise10a05.cpp
myCodeMateExercise10a05/myCodeMateExercise10a05.cpp
// **************************************************************************
//
// Counter.cpp
//
// Defines and tests class CounterType, which is used to count things.
// CounterType contains both a default constructor and a constructor that
// sets the count to a specified value, plus methods to increment, decrement,
// return, and output the count. The count is always nonnegative.
//
// **************************************************************************
#include
<
iostream
>
#include
<
fstream
>
#include
<
cstdlib
>
using
namespace
std
;
class
CounterType
{
public
:
CounterType
();
//Initializes the count to 0.
CounterType
(
int
initCount
);
//Precondition: initCount holds the initial value for the count.
//Postcondition:
// If initCount > 0,initializes the count to initCount.
// If initCount <= 0,initializes the count to 0.
void
increment
();
//Postcondition:
// The count is one more than it was.
void
decrement
();
//Postcondition:
// If the count was positive, it is now one less than it was.
// If the count was 0, it is still 0
int
getCount
();
void
output
(
ostream
&
outStream
);
//Precondition: outStream is ready to write to
//Postcondition: count has been written to outStream
private
:
int
count
;
};
// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------
// --------------------------------
// --------- END USER CODE --------
// --------------------------------
CounterType
::
CounterType
()
{
count
=
0
;
}
CounterType
::
CounterType
(
int
initCount
)
{
if
(
initCount
>=
0
)
count
=
initCount
;
else
count
=
0
;
}
void
CounterType
::
increment
()
{
count
++
;
}
void
CounterType
::
decrement
()
{
if
(
count
>
0
)
count
--
;
}
int
CounterType
::
getCount
()
{
return
count
;
}
void
CounterType
::
output
(
ostream
&
outStream
)
{
outStream
<<
count
;
}