Polymorphism
Polymorphism
Polymorphism
Polymorphism – An Introduction
The ability for objects of different classes related by
inheritance to respond differently to the same message.
Polymorphism is implemented in C++ via inheritance and
virtual functions.
This is also called as dynamic polymorphism. i.e. when a
request is made through a base class pointer to use a
virtual function, C++ chooses the correct overridden
function in the appropriate derived class associated with
the object
Other types of polymorphism are obtained through
function or operator overloading are called static
polymorphism.
This powerful feature of C++ helps in reducing complexity
of the system.
Static Binding
void show()
{
cout<<x;
}
};
4
Pointer to Derived Class
NOTE
B
There is a problem in using the
pointer of the base class to
access the public members of
D derived class. Using base class
pointer we can access only the
members which are inherited
from the base class and not
the members that originally
belong to the derived class.
B *ptr; In case a member of the
derived class has the same
B ob1; name as one of the members
ptr = &ob1; of the base class, then any
reference to that members by
the pointer will always access
the base class member.
5
Example
6
Dynamic Binding
class Car
{
public:
virtual void brake()=0;
virtual void steering()=0;
virtual void regNo()=0;
};
14
class Santro:public Car
{
int regn_no;
public:
Santro(int reg)
{
regn_no=reg;
}
void brake()
{
cout<<"AIR BRAKE"<<endl;
}
void steering()
{
cout<<"POWER STEERING"<<endl;
}
void regNo()
{
cout<<"REGISTRATION:::\t"<<regn_no<<endl;
}
};
15
class Hyundai: public Car
{
int regn_no;
public:
Hyundai(int reg)
{
regn_no=reg;
}
void regNo()
{
cout<<"REGISTRATION:::"<<regn_no<<endl;
}
void brake()
{
cout<<"NORMAL BRAKE"<<endl;
}
void steering()
{
cout<<"ORDINARY STEERING"<<endl;
}
};
16
int main()
{
Car *ptr;
Santro s(121);
Hyundai h(234);
ptr=&s;
ptr->regNo();
ptr->brake();
ptr->steering();
cout<<endl;
ptr=&h;
ptr->regNo();
ptr->brake();
ptr->steering();
return 0;
}
17
“this” pointer
Within a member function, the this keyword is a pointer to the
current object, i.e. the object through which the function was
called
C++ passes a hidden this pointer whenever a member function is
called
Within a member function definition, there is an implicit use of this
pointer for references to data members
CStr object
(*this)
“this” Pointer
Every object has access to its own address
through a pointer called ‘this’
The this pointer is passed as an implicit first
argument on every non-static member function
call for an object.
The this pointer is implicitly used to refer both
the data and function members of an object
It can also be used explicitly;
Example: (*this).x=5; or this->x=5;
EXAMPLE OF this POINTER
void set()
class A
{
{
cout<<"Value of i:"<<i<<endl;
int i;
cout<<"Value of f:"<<f;
float f;
}
};
public:
int main()
A(int i, float f)
{
{
A obj(10,20);
this->i=i;
obj.set();
this->f=f;
}
}
20
Example
Shape
Circle Rectangle
Manager Officer
designation designation
float getSalary();
float getSalary();
ASSIGNMENT
Vehicle
WRPM
WH_CIRCUM
virtual float getSpeed() =0;
TwoWheeler ThreeWheeler
TechBook NonTechBook