Sie sind auf Seite 1von 12

Virtual Base

Class
Virtual Base Class
 Consider a situation where all the three kinds
of inheritance, namely, multilevel, multiple
and hierarchical inheritance are involved.

 The ‘child’ has two direct base classes


‘parent1’ and ‘parent2’ which themselves
have a common base class ‘grandparent’.

 The ‘child’ inherits the traits of ‘grandparent’


via two separate paths. It can also inherit
directly as shown in the broken line. The
‘grandparent’ is referred as indirect base
class.
All the public and protected
members of ‘grandparent’ are
inherited into ‘child’ twice, first via
‘parent1’ and again via ‘parent2’.
This means, ‘child’ would have
duplicate sets of the members
inherited from ‘grandparent’. This
introduces ambiguity and should be
avoided.
The duplication of inherited
members due to these multiple
paths can be avoided by making
the common base class as virtual
base class while declaring the direct
or intermediate base classes.
class A // grandparent
{
-------------
-------------
};
class B1 : virtual public A //
parent1
{
-----------------
-----------------
};
class B2 : public virtual A //parent 2
{
-----------------
-----------------
};
class C: public B1,public B2 // child
{
---------------- // only one
copy of A
---------------- // will be
inherited
};
 When a class is made a virtual base
class, C++ takes necessary cares to see
that only one copy of that class is
inherited, regardless of how many
inheritance paths exist between the
virtual base class and a derived class.
Abstract Class :
 An abstract class is one that is not used
to create objects. An abstract class is
designed only to act as a base class.
Defining constructor in the derived class:
 When the constructor is defined in the
derived class, the base class constructor has to
be explicitly called. Consider the following
example :
class A
{
int a;
public : A( ) // default
constructor
{
A = 5;
}
};
class B
{
int b;
public : B( ) : A( )
{
B = 6;
}
};
Containership (Nesting of Classes) :

A class can contain a collection of objects of other classes.


When an object is created inside another class, it is known as
containership.

Example:

class alpha

----------------

----------------

};

class beta

----------------

----------------

};
class gamma

Alpha a;

Beta b;

--------------

};

All objects of gamma will contain the objects


of ‘a’ and ‘b’. These objects are created in two
stages : first , the members of the objects are
created using their respective constructors
and then the ordinary members are created.
 In the above example, the constructors of
the objects ‘a’ and ‘b’ are executed first. Then
the constructor of gamma object is executed.
 The constructor for the gamma class will be
given as follows:
class gamma
{
alpha a;
beta b;
--------------
  public : gamma(arglist):a(arglist1):b(arglist2)
{
//Body of constructor
}
};

Das könnte Ihnen auch gefallen