I don't understand the part in red at all. Okay. CountDn is a derived class. But this part as it stands
[COLOR=Red][B]Counter operator -- ()
{ return Counter(--count); }[/B][/COLOR] simply means that it is a member function of base class Counter but is defined within the derived class. Isn't it so? In other words, if we had defined this function in the class Counter we would have used the same listing as we have used for it in the CountDn class. Please help me with it. Thanks.
#include <iostream>
#include <cstdlib>
using namespace std;
////////////////////////////////////////////////////////////////
class Counter
{
protected:
int count;
public:
Counter() : count(0)
{ }
Counter(int c) : count(c)
{ }
int get_count() const
{ return count; }
Counter operator ++ ()
{ return Counter(++count); }
};
////////////////////////////////////////////////////////////////
[COLOR=Red][COLOR=Black]class CountDn : public Counter
{
public:[/COLOR][B]
Counter operator -- ()
{ return Counter(--count); }
};[/B][/COLOR]
////////////////////////////////////////////////////////////////
int main()
{
CountDn c1;
cout << "\nc1=" << c1.get_count();
++c1; ++c1; ++c1;
cout << "\nc1=" << c1.get_count();
--c1; --c1;
cout << "\nc1=" << c1.get_count();
cout << endl;
return 0;
}


Sign In
Create Account


Back to top









