Reusing Code in C++

Reusability
In computer science and Object Oriented Programming, reusability is the likelihood that a segment of source code can be used again to add new functionalities with slight or no modification. Reusable modules and classes reduce implementation time, increase the likelihood that prior testing and use has eliminated bugs and localizes code modifications when a change in implementation is required.
Subroutines or functions are the simplest form of reuse. A chunk of code is regularly organized using modules or namespaces into layers. Proponents claim that objects and software components offer a more advanced form of reusability, although it has been tough to objectively measure and define levels or scores of reusability.
The ability to reuse relies in an essential way on the ability to build larger things from smaller parts, and being able to identify commonalities among those parts. Reusability is often a required characteristic of platform software. Reusability brings several aspects to software development that do not need to be considered when reusability is not required.
Reusability implies some explicit management of build, packaging, distribution, installation, configuration, deployment, maintenance and upgrade issues. If these issues are not considered, software may appear to be reusable from design point of view, but will not be reused in practice.
Software reusability more specifically refers to design features of a software element (or collection of software elements) that enhance its suitability for reuse.
Candidate design features for software reuse include:

* Adaptable
* Brief: small size
* Consistency
* Correctness
* Extensibility
* Fast
* Flexible
* Generic
* Localization of volatile (changeable) design assumptions
* Modularity
* Orthogonality
* Parameterization
* Simple: low complexity
* Stability under changing requirements
Inheritance
Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus producing hierarchical relationships between them.
When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
Inheritance is a concept of linking two or more classes with each other in a hierarchical manner so that their properties and functions can be shared. (One class will extend to another class.) This leads to the biggest advantage of re-usability of the members and avoids redundancy.
The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
When inheritance is done, various links and tables (index, virtual etc) are created which are used to provide the accessibility of the members of the base class in derived class and in other class hierarchy. This means saying “public members are inherited” is better to say as “public members become accessible”.
A derived class inherits every member of a base class except:
* its constructor and its destructor
* its friends
* its operator=() members
Base & Derived Classes:
A class can be derived from more than one classes, which means it can inherit data and functions from multiple base 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 derived-class: access-specifier base-class
Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default. A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.
That gives us 9 combinations: 3 member access specifiers (public, private, and protected), and 3 inheritance types (public, private, and protected).
There are three ways that members can be accessed:
1. A class can always access it’s own members regardless of access specifier.
2. The public accesses the members of a class based on the access specifiers of that class.
3. A derived class accesses inherited members based on the access specifiers of its immediate parent. A derived class can always access it’s own members regardless of access specifier.
Public inheritance
We inherit a base class publicly, all members keep their original access specifications. Private members stay private, protected members stay protected, and public members stay public.
class Base
{
public:
int m_nPublic;
private:
int m_nPrivate;
protected:
int m_nProtected;
};
class Pub: public Base
{
// Public inheritance means:
// m_nPublic stays public
// m_nPrivate stays private
// m_nProtected stays protected
Pub()
{
// The derived class always uses the immediate parent's class access specifications
// Thus, Pub uses Base's access specifiers
m_nPublic = 1; // okay: anybody can access public members
m_nPrivate = 2; // not okay: derived classes can't access private members in the base class!
m_nProtected = 3; // okay: derived classes can access protected members
}
};
int main()
{
// Outside access uses the access specifiers of the class being accessed.
// In this case, the access specifiers of cPub. Because Pub has inherited publicly from Base,
// no access specifiers have been changed.
Pub cPub;
cPub.m_nPublic = 1; // okay: anybody can access public members
cPub.m_nPrivate = 2; // not okay: can not access private members from outside class
cPub.m_nProtected = 3; // not okay: can not access protected members from outside class
}
We cannot access in this condition
1.Derived classes can not directly access private members of the base class.
2.The protected access specifier allows derived classes to directly access members of the base class while not exposing those members to the public.
3.The derived class uses access specifiers from the base class.
4.The outside uses access specifiers from the derived class.
Public inheritance
Base access specifier Derived access specifier Derived class access Public access
Public Public Yes Yes
Private Private No No
Protected Protected Yes No

Private inheritance
With private inheritance, all members from the base class are inherited as private. This means private members stay private, and protected and public members become private.
Note that this does not affect that way that the derived class accesses members inherited from its parent! It only affects the code trying to access those members through the derived class.
class Base
{
public:
int m_nPublic;
private:
int m_nPrivate;
protected:
int m_nProtected;
};
class Pri: private Base
{
// Private inheritance means:
// m_nPublic becomes private
// m_nPrivate stays private
// m_nProtected becomes private Pri()
{
// The derived class always uses the immediate parent's class access specifications
// Thus, Pub uses Base's access specifiers
m_nPublic = 1; // okay: anybody can access public members
m_nPrivate = 2; // not okay: derived classes can't access private members in the base class!
m_nProtected = 3; // okay: derived classes can access protected members
}
};
int main()
{
// Outside access uses the access specifiers of the class being accessed.
// Note that because Pri has inherited privately from Base,
// all members of Base have become private when access through Pri.
Pri cPri;
cPri.m_nPublic = 1; // not okay: m_nPublic is now a private member when accessed through Pri
cPri.m_nPrivate = 2; // not okay: can not access private members from outside class
cPri.m_nProtected = 3; // not okay: m_nProtected is now a private member when accessed through Pri
// However, we can still access Base members as normal through Base:
Base cBase;
cBase.m_nPublic = 1; // okay, m_nPublic is public
cBase.m_nPrivate = 2; // not okay, m_nPrivate is private
cBase.m_nProtected = 3; // not okay, m_nProtected is protected
}
Private inheritance
Base access specifier Derived access specifier Derived class access Public access
Public Private Yes No
Private Private No No
Protected Private Yes No
Protected inheritance
Protected inheritance is the last method of inheritance. It is almost never used, except in very particular cases. With protected inheritance, the public and protected members become protected, and private members stay private.
Protected Inheritance
Base access specifier Derived access specifier Derived class access Public access
Public Private Yes No
Private Private No No
Protected Private Yes No

We can summarize the different access types according to who can access them in the following way: -
 Inheritance (Access Modiifers)
Access Public Protected Private
Same class Yes Yes Yes
Derived classes Yes Yes No
Outside classes Yes No No


A derived class inherits all base class methods with the following exceptions:
1. Constructors, destructors and copy constructors of the base class.
2. Overloaded operators of the base class.
3. The friend functions of the base class.
The Class that is Pre-defined is called as Base or super Class and the class which uses the Existing Code is known as derived or sub class The Various Types of Inheritance those are provided by C++ are as followings:
1. Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one base class.
2. Multiple Inheritance: It is the inheritance hierarchy wherein one derived class inherits from multiple base classes
3. Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple subclasses inherit from one base class.
4. Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for other classes.
5. Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of other four types of inheritance.

Virtual Inheritance
Virtual inheritance is a technique used in object-oriented programming, where a particular base class in an inheritance hierarchy is declared to share its member data instances with any other inclusions of that same base in further derived classes. For example, if class A is normally (non-virtually) derived from class X (assumed to contain data members), and class B likewise, and class C inherits from both classes A and B, it will contain two sets of the data members associated with class X (accessible independently, often with suitable disambiguating qualifiers). But if class A is virtually derived from class X instead, then objects of class C will contain only one set of the data members from class X. The best-known language that implements this feature is C++.
This feature is most useful for multiple inheritance, as it makes the virtual base a common subobject for the deriving class and all classes that are derived from it. This can be used to avoid the problem of ambiguous hierarchy composition (known as the "diamond problem") by clarifying ambiguity over which ancestor class to use, as from the perspective of the deriving class (C in the example above) the virtual base (X) acts as though it were the direct base class of C, not a class derived indirectly through its base (A).
It is used when inheritance represents restriction of a set rather than composition of parts. In C++, a base class intended to be common throughout the hierarchy is ded as virtual with the virtual keyword.
Ex: -
class Animal {
public:
virtual void eat();
};
// Two classes virtually inheriting Animal:
class Mammal : public virtual Animal {
public:
virtual void breathe();
};
class WingedAnimal : public virtual Animal {
public:
virtual void flap();
};
// A bat is still a winged mammal
class Bat : public Mammal, public WingedAnimal {
};


Free Web Hosting