--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD - IT)
Mobile No : 7600044051
Email : rupareleducation@gmail.com
Chapter No: 8
Que. This Pointer
Ans. Normally the member function of a class is invoked by some
object of the class.
It
has an implicit call of the function.
C++
has a keyword this pointer to represent an object that invoke a member
function.
So, member
function of every object has access to a pointer name this. Which point to an
object it self. It is represent as * (this pointer).
*
this can be treated like any other pointer an object.
Using
*this any member function can find out the address of the object of which it is
a member.
Que. Virtual function
Ans. When an object of different
class in a class hierarchy has the member function with same signature then
which version of member function should be invoke at run time.
It’s based on the pointer which
point to the specific object of the
class.
Normally pointer of the base class
is created to refer all the derived object.
But,
even when pointer point to derived class object it always execute the function
of the base class.
C++ provide a solution to invoke the
except version of member function which has to be decided at run time using
virtual function.
They are the
mean by which function of the base class can be overwrite by the function.
The keyword
virtual provide a mechanism for defining the virtual function.
When declaring the member function in the base class the keyword virtual preceding with those function which are to be bound dynamically.
When declaring the member function in the base class the keyword virtual preceding with those function which are to be bound dynamically.
Que. Pure virtual function
Ans. Usually, a function is a declare
as a virtual function in the base class and re-define it in the derived class.
Sometimes the member function just only declare inside a base class as a
virtual with NULL BODY.
It serve as a
framework for future design of the class hierarchy. Such function is call pure
virtual function.
Que. Rules for virtual function.
Ans. When
a virtual function in a base class is created there must be define virtual
function with a virtual keyword. The virtual function must be a member of some
class. They can not be static members. They can not be friend function to
another class. They are access using object pointers. The class can not have
virtual constructor but can contain virtual destructure. It is also possible to
have virtual operator overload. They should be declare in the public section of
a class.
Que. Run-Time Type Identification, OR what is
RTTI?
Ans. Run-Time
Type identification isi the feature that use while a program is running to find
out type information. That could not determine at compile time. It provide some
information about object at run time.
Such as the name of its type.
C++ provide type id() function for
getting information about object type. Its argument is an expression or type
main. It return a constant reference to a type info object. This type of
information does not help to find out the relation of object and does not solve
the problem arise in application.