Write a simple but complete class that represents a counter that counts 0, 1, 2, 3, 4, .... The name of the class should be Counter. Make sure it includes one private instance variable representing the value of the counter two instance methods:
increment() adds one to the counter value getValue() returns the current counter value Write a complete definition for the class, Counter
#include <iostream> using namespace std; class Counter{ public: int counterVar; Counter(int c){ counterVar = c; } void increment(){ counterVar++; } void decrement(){ counterVar--; } int getValue(){ return counterVar; } }; int main(){ Counter c(5); cout<<c.getValue()<<endl; c.increment(); c.increment(); cout<<c.getValue()<<endl; c.decrement(); cout<<c.getValue()<<endl; return 0; }
5 7 6
Get Answers For Free
Most questions answered within 1 hours.