Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #pragma once
  2. #include "counter.h"
  3. using namespace std;
  4.  
  5. class LimitedCounter {
  6. public:
  7.     void setLimit(int newLimit);
  8.     void reset();
  9.     LimitedCounter operator++(int n);
  10.     LimitedCounter& operator++();
  11.     int getCount()const;
  12.     bool operator<(const int i) const; //vaihdettu: int i
  13.  
  14.     LimitedCounter(int n0, int limitation) : C(n0) {
  15.         limit = limitation;
  16.     }
  17.  
  18.  
  19. private:
  20.     int limit;
  21.     Counter C;
  22. };
  23.  
  24. //kokeile näin
  25. //Perustelu: (lc < 7) operaattori-< jälkeen on int eikä LimitedCounter olio.
  26. bool LimitedCounter::operator<(const int i) const {
  27.     return this->C.getCount() < i;
  28. }
  29.  
  30. int LimitedCounter::getCount() const {
  31.     return C.getCount();
  32. }
  33.  
  34. ostream& operator<< (ostream& outs, const LimitedCounter& obj) {
  35.     return outs << obj.getCount();
  36. }
  37.  
  38.  
  39. void LimitedCounter::reset() {
  40.     C.reset();
  41. }
  42.  
  43. LimitedCounter LimitedCounter::operator++(int) {
  44.     LimitedCounter lc = LimitedCounter(C.getCount(), limit);
  45.     if (C.getCount() < limit) {
  46.         C++;
  47.     }
  48.     return lc;
  49. }
  50.  
  51. LimitedCounter& LimitedCounter::operator++() {
  52.     if (C.getCount() < limit) {
  53.         C++;
  54.     }
  55.     return *this;
  56. }
  57.  
  58. void LimitedCounter::setLimit(int newLimit) {
  59.     limit = newLimit;
  60. }
  61.  
  62. #if 1
  63. //LAB08
  64. int main() {
  65.     LimitedCounter lc(3, 5); //initial value 3, upper limit 5
  66.     cout << lc++ << endl; //output should be 3
  67.     cout << ++lc << endl; //output should be 5
  68.     lc.reset();
  69.     for (int i = 0; i < 9; i++) {
  70.         lc++;
  71.         cout << lc << endl; //output is 1 2 3 4 5 5 5 5 5
  72.     }
  73.     cout << lc.getCount() << endl; //output is 5
  74.     cout << (lc < 7);//Testing the comparison operator //output is 1 // Ei toimi
  75.     cout << (lc < 1);//Testing the comparison operator //output is 0 // Ei toimi
  76.     return 0;
  77. }
  78. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement