- 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;
}
//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;
}