Inheritance of overloaded ++ operator issue

Discussion in 'C Programming' started by John Wickerbee, Oct 2, 2011.

  1. John Wickerbee

    John Wickerbee

    Joined:
    Oct 2, 2011
    Messages:
    1
    Likes Received:
    0
    Hi. I have been racking my brain trying to figure out how to use inheritance to add postfix notation for both incrementing and decrementing. The first 2 classes (The base class and the 1st derived class which both use prefix notation) work fine. When I run the program, it seems as if the incrementing postfix cancels out the incrementing prefix, and the decrementing postfix cancels out the decrementing prefix, and the program won't allow for any new objects of the last derived class (PostfixInc) to be instantiated. Here is the code:

    //constructors in derrived class
    #include <iostream>
    using namespace std;
    //////////////////////////////////////////////////////////
    class Counter
    {
    protected: //Note: not private
    unsigned int count; //count
    public:
    Counter() : count() //constructor, no args
    { }
    Counter(int c) : count(c) //constructor, one arg
    { }
    unsigned int get_count() const //return count
    {return count;}
    Counter operator ++ () //increment count (prefix)
    {return Counter(++count);}

    };

    ////////////////////////////////////////////////////////////
    class CountDn : public Counter
    {
    public:
    CountDn() : Counter() // contructor, no args
    { }
    CountDn(int c) : Counter(c) //constructor, one arg
    { }
    CountDn operator -- () // decrement count (prefix)
    {return CountDn(--count);}

    };
    /////////////////////////////////////////////////////////////
    class PostfixDec : public CountDn//this class does everthing it's supposed to except predecrement
    {
    public:
    PostfixDec() : CountDn()
    { }
    PostfixDec(int c) : CountDn(c)
    { }
    PostfixDec operator -- (int)
    {return PostfixDec(count--);}

    };
    /////////////////////////////////////////////////////////////
    class PostfixInc: public PostfixDec
    {
    public:
    PostfixInc() : PostfixDec()
    { }
    PostfixInc(int c) : PostfixDec(c)
    { }
    PostfixInc operator ++ (int)
    {return PostfixInc(count++);}
    };

    int main()
    {
    PostfixInc c1; //class CountDn
    PostfixInc c2(100);

    cout << "c1=" << c1.get_count(); // display
    cout << "\nc2=" << c2.get_count(); // display

    ++c1; ++c1; ++c1; //increment c1
    cout << "\nc1=" << c1.get_count(); //display it

    --c2; --c2; //decrement c2(pre)
    cout << "\nc2=" << c2.get_count(); //display it

    c2--; c2--; //decrement c2(post)
    cout << "\nc2=" << c2.get_count(); //display it

    PostfixInc c3 = --c2; //create c3 from c2
    cout << "\nc3=" << c3.get_count() << endl; //display c3

    system("pause");
    return 0;
    }
     
    John Wickerbee, Oct 2, 2011
    #1
    1. Advertisements

  2. John Wickerbee

    Ahmed Al_gohary

    Joined:
    Apr 4, 2015
    Messages:
    1
    Likes Received:
    0
    ok this code will always use the POSTFIX notation because there is a simple and important rule in inheritance that is ::

    if the derived class contain the same function name of the base class, the function which is executed by the code is the one in the derived class.

    in other words:: the derived class function overload the base class function and cancel's it out
     
    Ahmed Al_gohary, Apr 4, 2015
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DX
    Replies:
    3
    Views:
    519
  2. DX
    Replies:
    0
    Views:
    449
  3. cppsks
    Replies:
    0
    Views:
    1,021
    cppsks
    Oct 27, 2004
  4. Joe P
    Replies:
    1
    Views:
    417
    Pete Becker
    Jun 21, 2005
  5. John Goche
    Replies:
    2
    Views:
    538
    Frederick Gotham
    Sep 4, 2006
  6. Afshin
    Replies:
    0
    Views:
    535
    Afshin
    Aug 12, 2011
  7. Afshin
    Replies:
    0
    Views:
    452
    Afshin
    Aug 12, 2011
  8. Afshin
    Replies:
    7
    Views:
    725
    Victor Bazarov
    Aug 16, 2011
Loading...