Access specifiers c++

Access specifiers c++

Inheritance is probably the most powerful feature of object-oriented programming, after classes themselves. The derived class inherits all the capabilities of the base class but can add embellishments and refinements of its own.

Derived Class and Base Class AndroWep-Tutorials
Derived Class and Base Class AndroWep-Tutorials

Inheritance is an essential part of OOP. Its big payoff is that it permits code reusability. Once a base class is written and debugged, it need not be touched again, but, using inheritance, can nevertheless be adapted to work in different situations. Reusing existing code saves time and money and increases a program’s reliability. Inheritance can also help in the original conceptualization of a programming problem, and in the overall design of the program.

Derived Class and Base Class

Let’s suppose that we have worked long and hard to make the Counter class operate just the way we want, and we’re pleased with the results, except for one thing. We really need a way to decrement the count.

Perhaps we’re counting people entering a bank, and we want to increment the count when they come in and decrement it when they go out, so that the count represents the number of people in the bank at any moment

We could insert a decrement routine directly into the source code of the Counter class. However, there are several reasons that we might not want to do this. First, the Counter class works very well and has undergone many hours of testing and debugging.

// counten.cpp
// inheritance with Counter class
#include <iostream>

using namespace std;
////////////////////////////////////////////////////////////////
class Counter //base class
{
    protected: //NOTE: not private
        unsigned int count; //count
    public:
        Counter(): count(0) //no-arg constructor
    {}
    Counter(int c): count(c) //1-arg constructor
    {}
    unsigned int get_count() const //return count
    {
        return count;
    }
    Counter operator++() //incr count (prefix)
    {
        return Counter(++count);
    }
};
////////////////////////////////////////////////////////////////
class CountDn: public Counter //derived class
{
    public: Counter operator--() //decr count (prefix)
    {
        return Counter(--count);
    }
};
////////////////////////////////////////////////////////////////
int main() {
    CountDn c1; //c1 of class CountDn
    cout << “\nc1 = ” << c1.get_count(); //display c1
    ++c1;
    ++c1;
    ++c1; //increment c1, 3 times
    cout << “\nc1 = ” << c1.get_count(); //display it
    --c1;
    --c1; //decrement c1, twice
    cout << “\nc1 = ” << c1.get_count(); //display it
    cout << endl;
    return 0;
}

The listing starts off with the Counter class, which (with one small exception, which we’ll look at later) has not changed since its appearance in COUNTPP3. Notice that, for simplicity, we haven’t modeled this program on the POSTFIX program, which incorporated the second overloaded ++ operator to provide postfix notation.

Specifying the Derived Class

Following the Counter class in the listing is the specification for a new class, CountDn. This class incorporates a new function, operator–(), which decrements the count. However—and here’s the key point—the new CountDn class inherits all the features of the Counter class. CountDn doesn’t need a constructor or the get_count() or operator++() functions, because these already exist in Counter.

class CountDn : public Counter

Accessing Base Class Members

An important topic in inheritance is knowing when a member function in the base class can be used by objects of the derived class. Let’s see how the compiler handles the accessibility issue in the COUNTEN example.

The protected Access Specifier

We have increased the functionality of a class without modifying it. Well, almost without modifying it. Let’s look at the single change we made to the Counter class. What does this do?

Let’s first review what we know about the access specifiers private and public. A member function of a class can always access class members, whether they are public or private. But an object declared externally can only invoke (using the dot operator, for example) public members of the class.

Access specifiers without inheritance AndroWep-Tutorials
Access specifiers without inheritance AndroWep-Tutorials

Inheritance and Accessibility

Inheritance and Accessibility AndroWep-Tutorials
Inheritance and Accessibility AndroWep-Tutorials

The moral is that if you are writing a class that you suspect might be used, at any point in the future, as a base class for other classes, then any member data that the derived classes might need to access should be made protected rather than private. This ensures that the class is “inheritance ready.”