Professional Documents
Culture Documents
Virtual Function and Pointer To Object
Virtual Function and Pointer To Object
pointer to 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.
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
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();
}
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
#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;
}
#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;
}
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
}