Polymorphism-Virtual Fun

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Polymorphism

Pointers to Objects
• 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.
• Consider following example
class item
{
int code;
float price;
public:
void getdata(int a, float b)
{
code =a;
price=b; }
void show(void)
{
cout<<“Code= “<<code<<endl;
cout<<“Price= “<<price<<endl; } };
Pointers to Objects
• item x;
item *ptr = &x // Pointer ptr is initialized with the address of
x

• Member function can be accessed in two ways


1) x.getdata(02, 500.00); // Using object
x.show();

2) ptr ->getdata(02, 500.00) // Using pointer


ptr -> show();

• Object can also be created dynamically using pointer and new


operator.
– item *ptr = new item;

• If a class has a constructor with arguments and does not include an


empty constructor then we must supply the arguments when the
object is created.
Pointers to derived classes
• Base class pointer can be made to point to
derived class pointer also.

• However using base class pointer we can


access only those members which are
inherited from base class and not the
members that are originally belong to derived
class.
Polymorphism
Polymorphism

Compile time Run time


polymorphism polymorphism
Static binding Dynamic binding
Early binding Late binding

Eg: function overloading eg: virtual function


operator overloading
Polymorphism
• It means ‘one name, multiple forms’. C++ supports two
types of Polymorphism.

1. Compile time polymorphism also known as Static


binding. Function overloading and Operator
overloading are the examples of compile time
polymorphism.

2. Run time Polymorphism also known as Dynamic


binding. It is achieved using virtual function and
using object pointer of type base class.
Run time Polymorphism
• class A
{ int x;
public:
void show() {--------}
};
class B : public A
{
int y;
public :
void show() {--------}
};

• In the situation when the function name and prototype is the


same in both the base class and derived classes, the function
cannot be overloaded and thus static binding does not apply.
Run time Polymorphism
• Then in such situation an appropriate member function can
be selected while program is running by using mechanism
known as virtual function.
• Selecting appropriate function at run time is known as run
time polymorphism.
• At runtime when it is known what class objects are under
consideration the appropriate version of the function is
invoked
• It is also known as dynamic binding because the selection of
the appropriate function is done dynamically at run time.
• Since the function is linked with a particular class much later
after the compilation this process is termed as late binding
also.
• Virtual function and object pointer are used to achieve this.
Virtual function
• When we use same function name in both base and
derived classes, then the function in the base class is
declared virtual. When a function is made virtual, C++
determines at run time which function is to be used.
At run time it is known which object is under
consideration and then appropriate function is called.

• Though the prototype of the function is same in both


derived class and base class, the function is not
overloaded. The appropriate member functions can be
selected at run time and this is called as run time
polymorphism. C++ supports the concept of run time
polymorphism by using virtual functions.
Virtual function contd..….
class base void main()
{ public: { Display base
void display () Show base
base B, *bptr; Display base
{ cout<<“display base”;
} derived D; Show derived

virtual void show() cout<<“ptr points to base”;


{cout<<“show base”; bptr = &B;
}}; bptr-> display();
class derived : public base
bptr-> show();
{ public:
void display () cout<<“ptr points to derived”;
{ cout<<“display derived”; bptr = &D;
} bptr-> display();
void show() bptr-> show();
{ cout<<“show derived”; }
getch();
};
}
Virtual function contd….

NOTE :

Base class pointer can point to base class objects


and derived class objects.

But derived class pointers cannot point to base


class objects.
Virtual function contd….

NOTE :

When the function in base class and derived


class have same prototype.

base class function must be made virtual.


Constructors in derived classes
• If base class contains default constructor with no
argument derived class need not have a constructor
function.

• However if any base class contains constructor with


argument/s then it is compulsory for the derived class
to have a constructor and pass the arguments to the
base class constructor.

• When both base and derived class contain


constructors then when the object of derived class is
created then the base constructor is executed first and
then derived constructor is executed.
• In case of multiple inheritance the base
classes are constructed in the order in which
they appear in the declaration

• In case of multilevel inheritance the


constructors will be executed in the order of
inheritance.
Example
class A class D : public A, public B
{ int x; { int m, n;
public: public:
A(int i)
D(int a,float b, int c, int d) : A(a),
B(b)
{x=i; {
cout<< “A initialized”; m=c; n=d;
} cout << “D initialized”;
}; }
class B };
{ float y; void main()
public: {
D d1(5, 10.2, 50,100);
B(float j)
----
{ y=j;
-----
cout <<“ B initialized”; }
} // x=5, y=10.2, m=50, n=100
};
Initializing class objects (another method)
Syntax:
constructor (arg. list) : initialization section
{
assignment section
}
e.g.
class xyz
{
int a,b; xyz(int i, int j) : a(i), b(2*j) { }
public : void main()
xyz(int i, int j) : a(i), b(j){ } { xyz x(2,3); } //a=2 and b=6
};
void main()
{ xyz x(2,3); } // a=2 and b=3

You might also like