Thread: Post increment and pre increment help

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    134

    Post increment and pre increment help

    HI, In my code, when I do this Counter c2, Counter c3. c3=c2++ it works fine and does what its supposed to do, but when i do ModCounter c5, ModCounter c6, c6=c5++ it doesnt do it. The same stand for pre and post increment and decrement....how do i solve this?? At the end I have added the the code in a zip file

    main.cpp
    Code:
    #include "Counter.h"
    #include "ModCounter.h"
    #include <iostream>
    using namespace std;
    
    void print(const Counter& ctr)
    {
    	cout << ctr.getCounterValue() << endl;
    }
    
    void playWithCounter(Counter& ctr)
    {
    	ctr++;
    	print (ctr);
    	ctr++;
    	print (ctr);
    	ctr++;
    	print (ctr);
    	ctr++;
    	print (ctr);
    
    	ctr--;
    	print (ctr);
    	ctr--;
    	print (ctr);
    	ctr--;
    	print (ctr);
    
    	ctr.reset();
    	print (ctr);
    	ctr--;
    	print (ctr);
    }
    
    void main()
    {
    	cout << "Working with Counter" << endl;
    	Counter ctr;
    	playWithCounter (ctr);
    
    	cout << "Working with ModCounter" << endl;
    	ModCounter mctr2(2);
    	playWithCounter (mctr2);
    
    	cout << "Working with ModCounter" << endl;
    	ModCounter mctr3(3);
    	playWithCounter (mctr3);
    
    
    	try
    	{
    		Counter ctr2;
    		if (ctr == ctr2)
    			cout << "Same" << endl;
    		else
    			cout << "Different" << endl;
    
    		ModCounter mc2a(2);
    		ModCounter mc2b(2);
    		if (mc2a == mc2b)
    			cout << "Same" << endl;
    		else
    			cout << "Different" << endl;
    
    		if (mctr3 == mc2a)
    			cout << "Same" << endl;
    		else
    			cout << "Different" << endl;
    	}
    	catch(Invalid)
    	{
    		cout << "Exception thrown" << endl;
    	}
    }
    Counter.h
    Code:
    #ifndef COUNTER_H
    #define COUNTER_H
    
    // DO NOT MODIFY THIS FILE
    // DO NOT MODIFY THIS FILE
    // DO NOT MODIFY THIS FILE
    
    // This class models a Counter that holds a value and allows you
    // to increment, decrement and reset.
    
    // An exception class
    class Invalid
    {
    };
    
    class Counter
    {
    public:
    	Counter();
    	// Purpose: To initialize the Counter object
    	// Requirements: -
    	// Promises: Couter's value is 0.
    	// Exception: -
    
    	virtual ~Counter();
    	// Purpose: To properly cleanup the object
    	// Requirements: -
    	// Promises: -
    	// Exception: -
    
    	Counter(const Counter& other);
    	// Purpose: To initialize the Counter object
    	// Requirements: - 
    	// Promises: Counter's value == other's value
    	// Exception: -
    
    	virtual const Counter& operator=(const Counter& other);
    	// Purpose: To copy the value from one counter to another.
    	// Requirements: -
    	// Promises: this object's value == other's value
    	// Exception: May throw an Invalid exception (for future use).
    
    	virtual bool operator==(const Counter& other) const;
    	// Purpose: Checks if two counter objects are equal (by value).
    	// Requirements: -
    	// Promises: Returns a true if value of this object == other's value.
    	// Exception: May throw an Invalid exception (for future use).
    
    	int getCounterValue() const;
    	// Purpose: To query the value of the counter object.
    	// Requirements: - 
    	// Promises: Returns the current value of the counter.
    	// Exception: - 
    
    	virtual Counter operator++();
    	// Purpose: To increment the value of the counter.
    	// Requirements: -
    	// Promises: Note the value may actually be more or less depending on implementation.
    	// Exception: -
    
    	virtual Counter operator++(int);
    	// Purpose: To increment the value of the counter.
    	// Requirements: -
    	// Promises: Note the value may actually be more or less depending on implementation.
    	// Exception: -
    
    	virtual Counter operator--();
    	// Purpose: To decrement the value of the counter.
    	// Requirements: -
    	// Promises: Note the value may actually be more or less depending on implementation.
    	// Exception: -
    
    	virtual Counter operator--(int);
    	// Purpose: To increment the value of the counter.
    	// Requirements: -
    	// Promises: Note the value may actually be more or less depending on implementation.
    	// Exception: -
    
    	virtual void reset(); 
    	// Purpose: To set the value of the counter to 0.
    	// Requirements: - 
    	// Promises: Counter's value is 0.
    	// Exception: -
    private:
    	int value;
    };
    #endif COUNTER_H
    Counter.cpp
    Code:
    // DO NOT MODIFY THIS FILE
    // DO NOT MODIFY THIS FILE
    // DO NOT MODIFY THIS FILE
    
    #include "Counter.h"
    
    Counter::Counter()
    {
    	value = 0;
    }
    
    Counter::~Counter()
    {
    }
    
    Counter::Counter(const Counter& other)
    {
    	value = other.value;
    }
    
    const Counter& Counter::operator=(const Counter& other)
    {
    	if (this != &other)
    	{
    		value = other.value;
    	}
    
    	return *this;
    }
    
    bool Counter::operator==(const Counter& other) const
    {
    	return value == other.value;
    }
    
    int Counter::getCounterValue() const
    {
    	return value;
    }
    
    Counter Counter::operator++()
    {
    	value++;
    	return *this;
    }
    
    Counter Counter::operator++(int)
    {
    	Counter temp = *this;
    	value++;
    	return temp;
    }
    
    Counter Counter::operator--()
    {
    	value--;
    	return *this;
    }
    
    Counter Counter::operator--(int)
    {
    	Counter temp = *this;
    	value--;
    	return temp;
    }
    
    void Counter::reset()
    {
    	value = 0;
    }
    ModCounter.h
    Code:
    #include "Counter.h"
    
    
    #ifndef MODCOUNTER_H
    #define MODCOUNTER_H
    
    
    class ModCounter:public Counter
    {
    public:
    	ModCounter();
    	// Purpose: to initialize a ModCounter
    	// Requirements:  no input is required
    	// Promises: limit will be 1
    	// Exception:
    
    	ModCounter(const int);
    	//Purpose: to initialize a ModCounter 
        //Requirements: it takes an integer 
        //Promises: to set the limit to the value of the parameter
        //Exception:
    
    	ModCounter(const Counter& other);
    	// Purpose:  to assign other to the current object, and determines
    	//           type of object with dynamic_cast.
    	// Requirements: it takes a Counter object
    	// Promises: value will be set to the value of object other
        //           limit will be copied
    	// Exception:
    
    
    	virtual const Counter& operator=(const ModCounter& other);
    	// Purpose: to assign object other to the current object, and determines other type
    	//			via dynamic_cast
    	// Requirements: it takes a Counter object
    	// Promises: value and limit will be set to those of ModCounter
        //           object other
    	// Exception:
    
    
    	virtual bool operator==( const Counter& other) const;
    	// Purpose: to test whether object other and the current hold
        //          same value and limit, uses dynamic_cast to determine &other type
    	// Requirements: it takes a Counter object
    	// Promises: when limits of ModCounter object other and the
        //           current are not equivalent, throw exception; return
        //           true when limits and values of both object are equal
        //           else otherwise 
    	// Exception: if the limits of the two objects are unequal its throws
    	//            invalid exception
    
    
    
    	virtual Counter operator++();
    	// Purpose:  to increment the value of current object by 1
    	// Requirements:  
    	// Promises: increment when value doesn't exceed limit - 1, else
        //           reset the value to zero
    	// Exception:
    
    	virtual Counter operator++(int);
    	// Purpose:  to increment the value of current object by 1
    	// Requirements: takes an int as an argument
    	// Promises: increment when value doesn't exceed limit - 1, else
        //           reset the value to zero
    	// Exception:
    
    
    
    	virtual Counter operator--();
    	// Purpose: to decrement the value of current object by 1
    	// Requirements:  
    	// Promises: set the value to limit - 1 when value is zero, else
        //           decrement the current value by 1
    	// Exception:
    
    	virtual Counter operator--(int);
    	// Purpose: to decrement the value of current object by 1
    	// Requirements: it still doesn't need any parameter; "int" is 
        //               just a flag
    	// Promises: set the value to limit - 1 when value is zero, else
            //           decrement the current value by 1
    	// Exception:
    
    
    	virtual ~ModCounter();
    	// Purpose: to destroy a Modcounter 
    	// Requirements:
    	// Promises:
    	// Exception:
    
    
    private:
    	
    	int limit;
    };
    
    #endif MODCOUNTER_H
    ModCounter.cpp
    Code:
    #include "ModCounter.h"
    #include "Counter.h"
    
    
    
    ModCounter::ModCounter()
    :limit(1)
    {
    }
    
    ModCounter::ModCounter(const int newLimit)
    :limit(newLimit)
    {
    }
    
    ModCounter::~ModCounter()
    {
    }
    
    ModCounter::ModCounter(const Counter& other)
    {
    	int i=0;
    
    	const ModCounter *ptrMod2= dynamic_cast<const ModCounter*>(&other);
    
    	if(ptrMod2!=0)
    	{
    	for(i=0;i<ptrMod2->getCounterValue()-1;i++)Counter::operator ++();
    
    	limit=ptrMod2->limit;
    	}
    		
    
    	
    }
    
    Counter ModCounter::operator ++()
    {
    	if(getCounterValue()<limit-1)Counter::operator ++();
    
    	else reset();
    
    	return *this;
    
    }
    
    Counter ModCounter::operator ++(int)
    {
    	Counter temp=*this;
    	
    	if(getCounterValue() < limit - 1)Counter::operator++();
    	
    	else reset();
    
    	return temp;
    	
    }
    
    Counter ModCounter::operator --()
    {
    	int i=0;
    
    	if(getCounterValue()==0)
    	{
    		for(i=0;i<limit-1;i++)Counter::operator ++();
    	}
    
    	else Counter::operator --();
    	return *this;
    }
    
    
    
    Counter ModCounter::operator --(int)
    {
    	Counter temp1=*this;
    	int i=0;
    	if(getCounterValue() == 0 ) 
    	{
    		for (i = 0; i < limit - 1; i++)Counter::operator++();
    	}
    	else Counter::operator--();
    	return temp1;
    
    }
    
    bool ModCounter::operator ==(const Counter& other) const
    {
    	const ModCounter *ptrMod= dynamic_cast<const ModCounter*>(&other);
    
    	if(ptrMod!=0)
    	{
    	
    	if(limit!=ptrMod->limit)throw Invalid();
    	
    	
    	}
    
    	 return (limit==ptrMod->limit && getCounterValue()==ptrMod->getCounterValue());
    }
    
    const Counter& ModCounter::operator =(const ModCounter& other)
    {
    	const ModCounter *ptrMod1= dynamic_cast<const ModCounter*>(&other);
    
    	if(ptrMod1!=0)
    	{
    	Counter::operator=(other);
    	limit = ptrMod1->limit;
    	}
    	return *ptrMod1;
    }
    Last edited by noob2c; 08-04-2003 at 08:11 PM.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    didnt even look at the code. too much even for a board read thru. attatch your project instead. just the source files. codes much easier to go thru in a compiler/debugger environment.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    Here is the code in zipped format....I was making test code to test my program and the post and pre increment was one thing that i put, and those were the results i got

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    Ive added the code in a zip file.

  5. #5
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Do I smell a bump?
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, it was only 2 hours later.

    Why do the ModCounter incrementers return a normal Counter?

    And further, wouldn't you rather use const references as return type on many functions (like prefix inc/decrement)?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How Does Increment Operators Work...??
    By ajayd in forum C Programming
    Replies: 37
    Last Post: 12-31-2008, 10:01 AM
  2. post vs pre increment
    By xddxogm3 in forum C Programming
    Replies: 13
    Last Post: 03-19-2004, 05:07 PM