I started writing a c++ program but am at a loss! Any Help would be highly appreciated! My program asks for this:
implement a class that models a tally counter, a mechanical device that is used to count people. whenever the operator pushes a button, the counter value advances by one. Model this operation with a count member function. a physical counter has a display to show the current value. in your class, use a get_value member function instead.
1) Start by making a class
2) Write a member function 'count' that increments a counter
3) Write a member function 'get_value' that returns the counter
I dont need to have (int a, a++) for adding one to the value every time? Im confused with the use a get_value member function instead. Also the class should have a default construct and a constructor to initialize the count. it should also have a set_value () function that sets the value to the integer passed to it. How do I put this in my program?
Have you read the tutorial about classes[1] (I think part 1 is enough for)? @MidnightRyder RL did most of the work for you. Just take the class he wrote and make the minor changes needed.
My Program looks like this but I dont understand why it isnt compiling and what the errors are.
#include<iostream>
#include<cmath>
using namespace std;
class TallyCounter
{
private:
int a;
public:
void count_up(){
a++;
}
int get_value()
{
return a;
}
void set_value(int x)
{
a = x;
}
TallyCounter();
TallyCounter(int c);
};
TallyCounter::TallyCounter(){
a = 0;
}
TallyCounter::TallyCounter(){
a = c;
}
int main(){
int class TallyCounter()
cout<<"sum"<<sum<<endl;
return 0;
//my errors are as follows:
prog7.cpp:37: error: redefinition of ‘TallyCounter::TallyCounter()’
prog7.cpp:34: error: ‘TallyCounter::TallyCounter()’ previously defined here
prog7.cpp: In function ‘int main()’:
prog7.cpp:42: error: expected primary-expression before ‘int’
prog7.cpp:42: error: expected ‘;’ before ‘int’
I fixed the problem but now I can only get it to put out Tally_Count(). How do I get it to count and give me a number?
#include<iostream>
#include<cmath>