Inheritance of overloaded ++ operator issue

Joined
Oct 2, 2011
Messages
1
Reaction score
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;
}
 
Joined
Apr 4, 2015
Messages
1
Reaction score
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top