Documente Academic
Documente Profesional
Documente Cultură
Polymorphism
One name multiple forms
Concept of polymorphism is implemented using
overloaded functions and operators.
The overloaded member functions are selected for
invoking by matching the arguments, both type and
number.
This information is known to the compiler at
compile time.
Compiler is able to select the appropriate function at
compile time hence called early binding or static
binding or compile time polymorphism.
Polymorphism
Example
Pointers To Objects
Object Pointers
Object pointers are useful in creating objects at
run time.
We can also use an object pointer to access the
public members of an object.
Example
Object Pointers
Declare an item variable x and a pointer to x
as follows:
Output
This Pointer
C++ uses a unique keyword called this to represent an object
that invokes a member function.
this is a pointer that points to the object for which this
function was called.
The starting address is the same as the address of the first
variable in the class structure.
This unique pointer is automatically passed to a member
function when it is called.
This Pointer
The this pointer is an implicit parameter to all
member functions.
Therefore, inside a member function, this may
be used to refer to the invoking object.
Friend functions do not have a this pointer,
because friends are not members of a class.
Only member functions have a this pointer.
Output
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1
Output