Base And Derived Classes

Defining a Base Class

Like any other class, a base class has data and function members that define its interface and implementation.

Example

// Item sold at an undiscounted price
// derived classes will define various discount strategies
class Item_base 
{
     public:
         Item_base(const std::string &book = "",
                   double sales_price = 0.0):
                          isbn(book), price(sales_price) { }
         std::string book() const { return isbn; }
         // returns total sales price for a specified number of items
         // derived classes will override and apply different discount algorithms
         virtual double net_price(std::size_t n) const
                    { return n * price; }
         virtual ~Item_base() { }
     private:
         std::string isbn;     // identifier for the item
     protected:
         double price;         // normal, undiscounted price
};

Base-Class Member Functions

The Item_base class defines two functions, one of which is preceded by the keyword virtual. The purpose of the virtual keyword is to enable dynamic binding. By default, member functions are nonvirtual. Calls to nonvirtual functions are resolved at compile time. To specify that a function is virtual, we precede its return type by the keyword virtual. Any nonstatic member function, other than a constructor, may be virtual. The virtual keyword appears only on the member-function declaration inside the class. The virtual keyword may not be used on a function definition that appears outside the class body.

A base class usually should define as virtual any function that a derived class will need to redefine.

Access Control and Inheritance

A derived class has the same access as any other part of the program to the public and private members of its base class: It may access the public members and has no access to the private members.
Sometimes a class used as a base class has members that it wants to allow its derived classes to access, while still prohibiting access to those same members by other users. The protected access label is used for such members. A protected member may be accessed by a derived object but may not be accessed by general users of the type.

protected Members

Like private members, protected members are inaccessible to users of the class.
Like public members, the protected members are accessible to classes derived from this class.
Another important property is that a derived object may access the protected members of its base class only through a derived object. The derived class has no special access to the protected members of base type objects.

Defining Derived Classes

To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form
class classname: access-label base-class

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.