Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "counter.h"
- using namespace std;
- class LimitedCounter {
- public:
- void setLimit(int newLimit);
- void reset();
- LimitedCounter operator++(int n);
- LimitedCounter& operator++();
- int getCount()const;
- bool operator<(const int i) const; //vaihdettu: int i
- LimitedCounter(int n0, int limitation) : C(n0) {
- limit = limitation;
- }
- private:
- int limit;
- Counter C;
- };
- //kokeile näin
- //Perustelu: (lc < 7) operaattori-< jälkeen on int eikä LimitedCounter olio.
- bool LimitedCounter::operator<(const int i) const {
- return this->C.getCount() < i;
- }
- int LimitedCounter::getCount() const {
- return C.getCount();
- }
- ostream& operator<< (ostream& outs, const LimitedCounter& obj) {
- return outs << obj.getCount();
- }
- void LimitedCounter::reset() {
- C.reset();
- }
- LimitedCounter LimitedCounter::operator++(int) {
- LimitedCounter lc = LimitedCounter(C.getCount(), limit);
- if (C.getCount() < limit) {
- C++;
- }
- return lc;
- }
- LimitedCounter& LimitedCounter::operator++() {
- if (C.getCount() < limit) {
- C++;
- }
- return *this;
- }
- void LimitedCounter::setLimit(int newLimit) {
- limit = newLimit;
- }
- #if 1
- //LAB08
- int main() {
- LimitedCounter lc(3, 5); //initial value 3, upper limit 5
- cout << lc++ << endl; //output should be 3
- cout << ++lc << endl; //output should be 5
- lc.reset();
- for (int i = 0; i < 9; i++) {
- lc++;
- cout << lc << endl; //output is 1 2 3 4 5 5 5 5 5
- }
- cout << lc.getCount() << endl; //output is 5
- cout << (lc < 7);//Testing the comparison operator //output is 1 // Ei toimi
- cout << (lc < 1);//Testing the comparison operator //output is 0 // Ei toimi
- return 0;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement