Tuesday, November 15, 2011

Inheritance: Derived Class Declaration

The derived class inherits all the features of its parent class and adds its own new features too. The declaration of derived class specifies its relationship with the base class along with its own typical futures than the base class. During the derived class declaration we specify the access specifier to the base class to specify access privilege to its members. Like base class, memory is not allocated during the definition of derived class but memory is allocated when it is instantiated (while creating an object). The syntax for the declaration of derived class is as follows:
class derived_class : [access_to_base]base_class
{
    //members of derived class
};
The colon(:) after derived_class indicates that this new class is derived from the class base_class. The access_to_base within the square bracket can be public, protected or private. The access_to_base specifies the access specifier for the access to base class members to derived class after the derivation. The access_to_base within square bracket indicates that it is optional. If not specified the access specifier for base class is private in class and public in struct declaration.
The visibility of base class members in derived class in different derivations are given in table below:

Base class Member
access specifiers
public derivation protected derivation private derivation
private invisible invisible invisible
protected protected protected private
public public protected private

No comments:

Post a Comment