You are on page 1of 28

Virtual function and

pointer to object

Pointer to derived class


object

Class base1
{
Public:
Void display()
{
Cout<<base class displayed;
}
};
Class der1:public base1
{
Public:
Void display()
{cout<<derived class displayed;}
};

Void main()
{
base1 *ptr;
der1 d;
ptr=&d;
ptrdisplay();
getch()
}
Output
Base class displayed

Memory management
The basic idea of new and delete is simple: new
creates an object of a given type and gives a
pointer to it, and delete destroys an object
created by new, given a pointer to it.
The reason that new and delete exist in the
language is that code often does not know when
it is compiled exactly which objects it will need to
create at runtime, or how many of them.
Thus new and delete expressions allow for
"dynamic" allocation of objects.

int main()
{
int * p = new int(3);
int * q = p;
delete q; // the same as delete p
return 0;
}

VIRTUAL FUNCTIONS

Virtual function
It is the member function declared in
the base class using the key word
virtual. whose functionality redefined
in the derived class.

Definition
Virtual means existing in appearance
but not in reality.
Virtual functions are used in late
binding or dynamic binding.
The concept of pointers play important
role in the virtual functions.
To make member function virtual,
keyword VIRTUAL is being used.

Static Binding
Binding means linking.
In c++, programs choosing the
function in a normal way during the
compilation time is called the static
binding or static linking or compile
time binding or early binding.
In this, compiler determines which
function is to be used based upon the
parameters passed to the function.

Late Binding or Dynamic


Binding
virtual function is an example of late
binding or dynamic binding/run time
polymorphism.
The linking of the functions at run
time is called run time binding.

Class base1
{
Public:
Virtual Void display()
{
Cout<<base class displayed;
}
};
Class der1:public base1
{
Public:
Void display()
{cout<<derived class displayed;}
};

Void main()
{
base1 *ptr;
der1 d;
ptr=&d;
ptrdisplay();
getch()
}
Output
derived class displayed

Pure Virtual Function


A pure virtual function is a virtual
function which has no body i.e no
arguments, no variables and no
expressions or statements inside it.
It has only declaration but no
definition.
Pure virtual functions can be
declared inside and outside the class.

Why we use Pure virtual


function??
To make the code simpler.
To access the members of derived
class through the pointer of base
class, we need virtual functions.
But if the virtual function is of no use,
because it will not be called then we
make it as pure virtual function.

Abstract base class


The class in which at least one pure
virtual function declared is called an
abstract base class.
We can not create object of the
abstract class, but we can create
pointer to that class.
A class that does not have a pure
virtual function is called a concrete
class.

Pure Virtual Function

Is a function without a body


Is created by adding the notation
=0 to the virtual
function
declaration
Example:
virtual int
calc_net_salary()=0;

class shape
{
protected:
int a,b;
public:
void read()
{
cin>>a>>b;
}
virtual void cal_area()=0;
};

class rectangle:public
shape
{
void cal_area()
{
doubles area=a*b;
cout<<area;
}
};

class triangle:public sh
{
void cal_area()
{
doubles area=(a*b)/2;
cout<<area;
}
};

int main()
{
shape *ptr[2];
rectangle r1;
cout<<enter leng n bredth;
r1.read();
triangle t1;
cout<<enter base n perpendicular;
t1.read();
ptr[0]=&r1;
ptr[1]=&t1;
for(int i=0;i<2;i++)
ptr[i]->cal_area();
}

Enter leng and breadth 10 20


Enter base and perpendicular 5 20
Area of rectangle=200
Area of trinagle=50

Virtual destructor
"A destructor is a member function of a class,
which gets called when the object goes out of
scope". This means all clean ups and final steps
of class destruction are to be done in destructor.
A virtual function is something which helps a
derived class in overriding the implementation of
a functionality of a base class.
The order of execution of destructor in an
inherited class during a clean up is like this.
1. Derived class destructor
2. Base class destructor

A difference between a destructor


(of course also the constructor) and
other member functions is that, if a
regular member function has a body
at the derived class, only the version
at Derived class gets executed.
Whereas in case of destructors, both
derived as well as base class
versions get executed.

#include <iostream.h>
class Base
{
public:
Base(){ cout<<"Constructor: Base"<<endl;}
~Base(){ cout<<"Destructor : Base"<<endl;}
};
class Derived: public Base
{
//Doing a lot of jobs by extending the functionality
public:
Derived(){ cout<<"Constructor: Derived"<<endl;}
~Derived(){ cout<<"Destructor : Derived"<<endl;}
> };
void main()
{
Base *Var = new Derived();
delete Var;
}

the constructors are getting called in


the proper order. But to the dread of a
programmer of a large project, the
destructor of the derived class was not
called at all.
This is where the virtual mechanism
comes into our rescue. By making the
Base class Destructor virtual, both the
destructors will be called in order. The
following is the corrected sample.

#include <iostream.h>
class Base
{
public:
Base(){ cout<<"Constructor: Base"<<endl;}
virtual ~Base(){ cout<<"Destructor : Base"<<endl;}
};
class Derived: public Base
{
//Doing a lot of jobs by extending the functionality
public:
Derived(){ cout<<"Constructor: Derived"<<endl;}
~Derived(){ cout<<"Destructor : Derived"<<endl;}
};
void main()
{
Base *Var = new Derived();
delete Var;
}

If the destructor in the base class is


not made virtual, then an object that
might have been declared of type base
class and instance of child class would
simply call the base class destructor
without calling the derived class
destructor.
Hence, by making the destructor in the
base class virtual, we ensure that the
derived class destructor gets called
before the base class destructor.

Object Slicing
When a Derived Class object is assigned to Base
class, the base class' contents in the derived object
are copied to the base class leaving behind the
derived class specific contents. This is referred as
Object Slicing.
That is, the base class object can access only the
base class members. This also implies the
separation of base class members from derived
class members has happened.
"Slicing" is where you assign an object of a derived
class to an instance of a base class, thereby losing
part of the information - some of it is "sliced" away.

Class Base
{
public:
int i;
Base()
{
i=10;
}};
class Derived : public Base
{
public:
int j;
Derived()
{
i=20;
j=30;
}};
int main()
{
Base B1;
Derived D1;
B1 = D1; //only i is copied to B1
}

You might also like