Advertisement
Syndragonic

Lab 4 Code 2 CS311

Sep 22nd, 2019
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. class CounterType
  5. {
  6. public:
  7. CounterType();
  8. void setCount(int );
  9. void upCount();
  10. void downCount();
  11. int getCount();
  12. void printCount();
  13. private:
  14. int count;
  15. };
  16. int main(){
  17. CounterType counter;
  18. int i;
  19. for(i=0;i<10;i++)
  20. {counter.upCount();
  21. counter.printCount();
  22. }
  23. for(i=0;i<5;i++)
  24. {counter.downCount();
  25. counter.printCount();
  26. }
  27. system("pause");
  28. return 0;
  29. }
  30. CounterType::CounterType(){
  31. count=0;
  32. }
  33.  
  34. void CounterType::setCount(int c){
  35. if(c>=0)
  36. count=c;
  37. else
  38. {count=0;
  39. cout<<"Invalid count initialization,\n will set to 0\n";
  40. }
  41. }
  42.  
  43. void CounterType::upCount(){
  44. count++;
  45. }
  46.  
  47. void CounterType::downCount(){
  48. if(count>0)
  49. count--;
  50. else
  51. cout<<"This will set count < 0\nothing is being done\n";
  52. }
  53.  
  54. int CounterType::getCount(){
  55. return count;
  56. }
  57.  
  58. void CounterType::printCount(){
  59. cout<<"count="<<count<<endl;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement