Using a class, implement the simulation of tally counter. A tally counter is a m
ID: 3569885 • Letter: U
Question
Using a class, implement the simulation of tally counter. A tally counter is a mechanical device that is used to count people - for example to find out how many people attend a concert or board a bus. Whenever the operator of a tally counter pushes a button, the counter value advances by one and there is a display on the device that shows the current value. There is a separate button to reset the counter back to zero.
This class should be separated into a header and cpp files and make sure you create preprocessor inclusion guards. It should have a private variable to hold the count, and four public functions. The functions should be called AddToCount, GetCount, PrintCount and ResetCount. AddToCount adds one to the count every time it is called. GetCount returns the value of the count and PrintCount outputs the count to the screen using cout (and endl). Finally, there should be a ResetCount function that changes the count to zero.
The class should have two constructors. The default constructor should set count to 0. The working constructor will take a value of the same type as count and set count to that value.
The program's main should be in its own cpp file. The main() function should test your class by creating an instance of the class called tally1 by using the default constructor and calling AddToCount twice. Call PrintCount on the object to verify the count is correct. Call ResetCount on tally1 and verify the count was set to zero by calling PrintCount again.
Next create another instance of the class called tally2 with the working constructor and set it to 10. Call AddToCount once on tally2 and then use the GetCount member function to return the value to main and cout the returned value in main. Call PrintCount on tally2 so you can see if GetCount returned the correct value to main.
Explanation / Answer
Program Code:
//Tally.h
#ifndef Tally
#define Tally
//TallyCounter class that defines a variable tally counter
//variable and its functions
class TallyCounter
{
private:
int counter;
public:
//default constructor
TallyCounter()
{
counter=0;
}
//working or parametrised constructor
TallyCounter(int count)
{
counter=count;
}
//declaration of methods
void AddToCount();
int GetCount();
void PrintCount();
void ResetCount();
};
#endif
-----------------------------------------------------------------------------------------------------
//Tally.cpp
#include "stdafx.h"
#include "Tally.h"
#include <iostream>
using namespace std;
//AddToCount() will increment the counter variable by 1
void TallyCounter:: AddToCount()
{
counter++;
}
//GetCount method will return the counter value
int TallyCounter:: GetCount()
{
return counter;
}
//PrintCount method will print the counter value
void TallyCounter:: PrintCount()
{
cout<<"The count value is: "<<counter<<endl;
}
//ResetCount method will reset the counter to zero
void TallyCounter:: ResetCount()
{
counter=0;
}
-----------------------------------------------------------------------------------
// TallyTest.cpp: Defines the entry point for the console application.
#include "stdafx.h"
#include "Tally.h"
#include <iostream>
using namespace std;
//main function
int main()
{
cout<<"Tally 1st object."<<endl;
//tally1 is object to the TallyCounter
//it calls a default constructor
TallyCounter tally1;
//call the AddToCount method twice
tally1.AddToCount();
tally1.AddToCount();
//call the PrintCount()
tally1.PrintCount();
//call the ResetCount()
tally1.ResetCount();
//call the PrintCount to see whether the
//counter value is set to zero
tally1.PrintCount();
cout<<endl;
cout<<"Tally 2nd object."<<endl;
//tally2 object to the TallyCounter
//working constructor and sets the value to 10
TallyCounter tally2(10);
//call the AddToCount method
tally2.AddToCount();
//print the counter value by calling GetCount method
cout<<"The count value is: "<<tally2.GetCount()<<endl;
system("pause");
return 0;
}
----------------------------------------------------------------------------
Sample output:
Tally 1st object.
The count value is: 2
The count value is: 0
Tally 2nd object.
The count value is: 11
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.