Start with the COUNTEN2 program in this chapter. It can increment or decrement a counter, but only using prefix notation. Using inheritance, add the ability to use postfix notation for both incrementing and decrementing. (See Chapter 8 for a description of postfix notation.)
I was trying to solve this exercise from robert lafore OOPv4 book and when I inherited a class that had pre-increment and decrement and tried to add post-increment and decrement in its derived class it showed this error main.cpp:55:5: error: no match for ‘operator++’ (operand type is ‘CountP’) when I remove either pre or post increment it works fine but gives an error otherwise. I am an absolute beginner so help would be appreciated.
#include <iostream>
#include <stdlib.h>
using namespace std;
class Counter{
protected:
unsigned int count;
public:
Counter() : count(){
}
Counter(int c) : count(c){
}
void display(){
cout<<count<<endl;
}
Counter operator ++ (){ // Pre increment operator
return Counter(++count);
}
};
class CountDn : public Counter{
public:
CountDn() : Counter(){
}
CountDn(int c) : Counter(c){
}
CountDn operator -- (){
return CountDn(--count);
}
};
class CountP : public CountDn{
public:
CountP() : CountDn(){
}
CountP(int c) : CountDn(c){
}
CountP operator ++(int){ // Post increment operator
return CountP(count++);
}
CountP operator --(int){
return CountP(count--);
}
};
int main()
{
CountP cp1(5),cp2;
CountDn cd1(7);
++cp1;
cp1.display();
cp1;
cp1.display();
cp1++;
cp1.display();
return 0;
}