Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Use of Inheritance in C++? Advantage

theLiontheLion Member Posts: 1
The following program creates a derived class for decrementing counter variable. it uses operator overloading

#include
#include

using namespace std;

class Counter
{
protected:
unsigned int count;
public:
Counter () : count(0)
{}

Counter(int c) : count(c)
{}

unsigned int get_count() const
{
return count;
}

Counter operator ++ ()
{
return Counter(++count);
}

};

class CountDn : public Counter
{
public:
Counter operator -- ()
{
return Counter(--count);
}
};

void main()
{
Counter c1;
cout << "
c1=" << c1.get_count();
++c1; ++c1; ++c1;

cout << "
c1=" << c1.get_count();

--c1; --c1;

cout << "
c1=" << c1.get_count();

cout << endl;

getch();
}

I have a question in my mind what is the advantage of such inheritance although i can write the -- overloaded function in my original class and it will give the correct result. so why we have used inheritance. Need Help. Kindly explain it with some other example if anybody has it in his mind. Would be very much grateful.
thankyou.
Sign In or Register to comment.