UNIT-IV
C++
Virtual Base Classes
An ambiguity can arise when several paths exist
to a class from the same base class. This means
that a child class could have duplicate sets of
members inherited from a single base class.
- C++ solves this issue by introducing a virtual
base class. When a class is made virtual,
necessary care is taken so that the duplication
is avoided regardless of the number of paths
that exist to the child class.
Contd….
When two or more objects are derived from a
common base class, we can prevent multiple
copies of the base class being present in an
object derived from those objects by
declaring the base class as virtual when it is
being inherited. Such a base class is known
as virtual base class. This can be achieved by
preceding the base class’ name with the word
virtual.
Example
Friend Function
A friend function of a class is defined outside
that class' scope but it has the right to access
all private and protected members of the
class. Even though the prototypes for friend
functions appear in the class definition,
friends are not member functions.
To declare a function as a friend of a class,
precede the function prototype in the class
definition with keyword friend
Bullets…..
Declaration of friend function in C++
Demo of friend function
example
Difference between virtual function and
pure virtual function
Virtual Function in C++
A virtual function a member function which is
declared within base class and is re-defined
(Override n) by derived class . When you
refer to a derived class object using a pointer
or a reference to the base class, you can call
a virtual function for that object and execute
the derived class’s version of the function.
They are mainly used to achieve
Runtime polymorphism
Functions are declared with a virtual
keyword in base class.
The resolving of function call is done at Run-
time.
Rules for Virtual Functions
They Must be declared in public section of class.
Virtual functions cannot be static and also cannot be
a friend function of another class.
Virtual functions should be accessed using pointer or
reference of base class type to achieve run time
polymorphism.
The prototype of virtual functions should be same in
base as well as derived class.
They are always defined in base class and overridden
in derived class. It is not mandatory for derived class
to override (or re-define the virtual function), in that
case base class version of function is used.
example
Pure Virtual Function
A pure virtual function (or abstract function)
in C++ is a virtual function for which we
don’t have implementation, we only declare
it. It is because Sometimes implementation of
all function cannot be provided in a base
class because we don’t know the
implementation
OR
Contd..
A pure virtual function is a function which has
no definition in the base class. Its definition
lies only in the derived class i.e it is
compulsory for the derived class to provide
definition of a pure virtual function. Since
there is no definition in the base class, these
functions can be equated to zero.
The general form of pure virtual function is :
virtual type func-name(parameter-list) = 0;
example
Contd….
The virtual function and pure virtual function
both are the concepts of run time
polymorphism.
The main difference between ‘virtual
function’ and ‘pure virtual function’ is that
‘virtual function’ has its definition in the base
class and also the inheriting derived classes
redefine it. The pure virtual function has no
definition in the base class, and all the
inheriting derived classes has to redefine it.
Abstract classes
An abstract class is a class that is designed to
be specifically used as a base class. An
abstract class contains at least one pure
virtual function. You declare a pure virtual
function by using a pure specifier (= 0) in the
declaration of a virtual member function in
the class declaration.
Sometimes implementation of all function
cannot be provided in a base class because
we don’t know the implementation. Such a
class is called abstract class.
Example
Contd…
Because an abstract class contains one or more
functions for which there is no definition (that is, a
pure virtual function), no objects of an abstract
class may be created. Instead, an abstract class
constitutes an incomplete type that is used as a
foundation for derived classes.
Although you cannot create objects of an abstract
class, you can create pointers and references to an
abstract class. This allows abstract classes to
support run-time polymorphism, which relies upon
base-class pointers and references to select the
proper virtual function.
Exception Handling
A Program execution many times returns an
abnormal, unexpected answer at runtime. It
may due to violation of the rules of the
language or constraints of the C++ execution
environment. It should be noted that
exceptions are different from syntax and
logical errors which are detected at run time.
Example of Exception
Out of Bound Array Subscript (trying to
access a non-existent array element)
Arithmetic overflow ( a value outside the
reprentable range of values)
Division by zero (trying to divide a number by
0);
Memory exhaustion.
Contd…
C++ exception handling is built upon three keywords:
try, catch, and throw.
throw − A program throws an exception when a
problem shows up. This is done using a throw
keyword.
catch − A program catches an exception with an
exception handler at the place in a program where
you want to handle the problem. The catch keyword
indicates the catching of an exception.
try − A try block identifies a block of code for which
particular exceptions will be activated. It's followed
by one or more catch blocks.
Catching Exception
The code that is likely to generate an
exception is enclosed in a try block. This Try
block is immediately followed by one or more
catch blocks. Each Catch block specifies the
following:
a)type of exception it can handle
b) the exception handler
A try block must be accompanied by at least
one catch block
General Form of Exception handling
try
{
/*statements likely to cause exception
}
catch (ExceptionType 1 obj)
{
Exception handler for exception type1
}
catch (ExceptionType2 obj)
{
Exception handler for exception type2
}
EXAMPLE
try {
int age = 15;
if (age >= 18) {
cout << "Access granted - you are old enough.";
} else {
throw (age);
}
}
catch (int myNum) {
cout << "Access denied - You must be at least 18
years old.\n";
cout << "Age is: " << myNum;
}
Summary Exception Handling
An exception is a problem that arises during
the execution of a program. A C++ exception
is a response to an exceptional circumstance
that arises while a program is running, such
as an attempt to divide by zero.
Exceptions provide a way to transfer control
from one part of a program to another. C++
exception handling is built upon three
keywords: try, catch, and throw.
Template Class in C++
Template class, as the name suggests, is a
Template for classes. C++ provides us with a
way where we can create a class that will
serve as a blueprint/template for future
classes. A template class will have generic
variables and methods of type “T”, which can
later be customized to be used with different
data types as per the requirement.
Class Template Declaration
EXAMPLE
NULL POINTER
The Null Pointer is the pointer that does not point
to any location but NULL
A null pointer has a reserved value that is called a
null pointer constant for indicating that the
pointer does not point to any valid object or
function. You can use null pointers in the following
cases: Initialize pointers. Represent conditions
such as the end of a list of unknown length
Often used to signal that a pointer doesn't
currently point to anything or to represent errors
or the end of a data structure
example