Вы находитесь на странице: 1из 8

What are Pointer Variables:

A pointer variable is one that stores an address. We can declare pointers as follows
int* p; .This means that p stores the address of a variable of type int.
x = *y;
y
1776

1775

1776

1776
Memory

25

25
X

Using Pointers in C++:


There are few important operations, which we will do with the pointers very
frequently.
(a) we define a pointer variables
(b) assign the address of a variable to a pointer and
(c) finally access the value at the address available in the pointer variable.
This is done by using unary operator * that returns the value of the variable located
at the address specified by its operand. Following example makes use of these
operations:

Output

Pointer to Object
A pointer can also refer to an object of a class.
The member function of an object can be accessed through pointers by using
symbol
->
The symbol is known as member access operator.
ptr->myClass_func();
Syntax:
ptr-> member
ptr : it is the name of the pointer that reference an object.
> : it is used to access a member of object
member: it is the name of class member to be accessed.

How to

What is polymorphism

Polymorphism means One name many form

Polymorphism is a technique that allows you to set a base object equal to one
or more of its derived objects.

After the assignment, the base acts in different ways, depending on the traits
of the derived object that is currently assigned to it. The base object that acts
in many different ways, hence the name "polymorphism," which translates
literally to "many form."

However, you cannot call one of the derived methods that do not also belong
to the base. The base doesn't know about those methods, so the compiler
won't let you call them. In other words, the base may be able to call some of
the derive functions, but it is still a variable of the base type

If some of the methods in a base class are defined as virtual, each of the
descendants can redefine the implementation of these methods. The key
elements that define a typical case of polymorphism are a base class and the
descendants that inherit a base class's methods. In particular, the fanciest
type of polymorphism involves virtual methods that are inherited from a base
class

Types of Polymorphism diagram

Differnece between Compile time and run time

What is function overloading

All the overloaded functions have the same name

The number of arguments may differ

void Show (float f)


void Show(int a,int b)

The data type of arguments may differ

void Show (float a,float b)


void Show(int a,int b)

The order of arguments may differ

void Show (int a,float b)


void Show(float b, int a)

The return type may differ

int Show(int a)

float Show(float a)

Function Declaration

void Show (int i)


void Show (float f)
void Show(int a,int b)

Function calling

Show(5)

Show(4.0)

Show(2,3)

Merits of Static Polymorphism:

The calls to the overloaded methods are resolved at the compile time. This
helps in creating faster programs.

Demerits of Static Polymorphism

However, since all such calls must be resolved at compile time, it deprives us
from the flexibility of plugging the new code at the runtime

Dynamic Polymorphism
In case of static polymorphism, for the overloaded functions,
the compiler resolves which function to call at the compile
time
In case of dynamic polymorphism, this decision is delayed
until the runtime
C++ supports run-time binding through virtual functions.
Polymorphism is thus implemented by virtual functions and run-time binding
mechanism in C++.

A class is called polymorphic if it contains virtual functions.


A typical scenario of polymorphism in C++:
There is an inheritance hierarchy
The first class that defines a virtual function is the base class of the hierarchy that
uses dynamic binding for that function name and signature.
Each of the drived classes in the hierarchy must have a virtual function with same
name and signature.
There is a pointer of base class type; and this pointer is used to invoke virtual
functions of derived class
They enable the user to execute completely different function by same function call.
Virtual function is defined in the parent class and can be overridden in child classes

Output:
Base
Base
As you can see, the function in the base class is always executed. The compiler ignores
the
contents of the pointer ptr and chooses the member function that matches the type of the
pointer,

This symbol is known as member access operator


Base* ptr; //pointer to base class
ptr = &dv1; //put address of dv1 in pointer
ptr->show(); //execute show()

Output:
Derv1
Derv2

ptr

Base

&Derv1
ptr -> show()

Show()

Derv1
Show()
ptr
&Derv2

Derv2

ptr -> show()


Show()

ptr

Base

&Derv1
ptr -> show()

ptr
&Derv2

Show()

Derv1
Show()

ptr -> show()


Derv2
Show()

Demerits of Dynamic Polymorphism:


In Dynamic polymorphism the call to a particular form of
function is determined at runtime, this process is called late
binding

Merits of Dynamic Polymorphism:


Late binding helps in development of large applications where
code flexibility is desired
By using late binding, code changes can be easily accomplished
without major modifications

Вам также может понравиться