Unit IV
Inheritance – public, private, and protected derivations – multiple
inheritance – virtual base class – abstract class – composite objects Runtime
polymorphism – virtual functions – pure virtual functions – RTTI – typeid –
dynamic casting – RTTI and templates – cross casting – down casting .
2 MARKS
1. State Inheritance.
classification and provides the idea of reusability. The class which is inherited is
known as the base or super class and class which is newly derived is known as
· Single inheritance
· Multi-level inheritance
· Multiple inheritance
· Hierarchical inheritance
· Hybrid inheritance
The syntax of deriving a new class from an already existing class is given
by,
class.
Here, class B is derived from class A and class C is further derived from
class.
base class.
inheritance.
two copies of the properties of class A. This can be avoided by declaring classes B
A class that serves only as a base class from which derived classes are
derived. No objects of an abstract base class are created. A base class that
function for a particular call at the compile time itself. It can be achieved
for a particular call at the run time only. It can be achieved using virtual
functions.
function. For e.g., the function call a. show () will set the pointer ‘this’ to the
use at run time based on the type of object pointed to by the base pointer, rather
definition relative to the base class. In such cases, the compiler requires each
function. A class containing pure virtual functions cannot be used to declare any
· They cannot be static members and they are accessed by using object
pointers
· Prototypes of base class version of a virtual function and all the derived
class
The typeid operator provides a program with the ability to retrieve the
operator, along with the dynamic cast operator, is provided for runtime type
Example:
The dynamic cast operator performs type conversions at run time. The
void. If T is a pointer and the dynamic cast operator fails, the operator returns a
null pointer of type T. If T is a reference and the dynamic cast operator fails, the
operator throws the exception std::bad_cast. You can find this class in the
option.
Down casting is the term used in C++ for casting a pointer or reference to a
base class to a derived class. C++ provides a special explicit cast called dynamic
cast that performs this conversion. Down casting is the opposite of the basic
Cross casting only occurs when using multiple inheritances. Here a type
can be derived from two or more base classes. A pointer or reference to such a
type can be passed around as a pointer or reference to any of these base types. A
cross cast is where we have a pointer or reference to an object as one of its bases
one base to another is a cross cast, because we are going across the inheritance
(Or)
9. (a) Write a C++ program for RTTI and templates for typeid?
(b) Write a C++ program for RTTI and templates for dynamic casting?
10. Elaborate cross casting and down casting in run time polymorphism?
**********