Top Banner
15
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Polymorphism
Page 2: Polymorphism

JUNAID VK

[email protected]

www.facebook.com/junaid.omy

twitter.com/junaid.omy

in.linkedin.com/in/junaidvkomy

9745991390

POLYMORPHISM

Page 3: Polymorphism

Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Page 4: Polymorphism

CONTENTS

POLYMORPHISM• Static Polymorphism

Function OverloadingOperator Overloading

• Dynamic PolymorphismVirtual function

Page 5: Polymorphism

POLYMORPHISM• It means that some

code or operations

or objects behave

differently in different

contexts.

• Poly means many,

morphism means

forms.Two type polymorphism:-

Static polymorphism

Dynamic polymorphism

Page 6: Polymorphism

STATIC POLYMORPHISM

The compiler is able to select the appropriate function for a particular call at compile-time itself.

Two kinds:-

• Function overloading

• Operator overloading

Page 7: Polymorphism

FUNCTION OVERLOADING

Using a single function name to perform different types of tasks is known as function overloading.

Page 8: Polymorphism

Example:#include<iostream>using namespace std;class shape{public:float area(double a){

float circle_area;circle_area=3.14*(a*a);return circle_area;

}int area(int a){

int squire_area;squire_area=a*a;return squire_area;

}int area(int a,int b){

int rectangle_area;rectangle_area=a*b;return rectangle_area;

}};

main(){

double circlee;int squiree,rectanglee;shape object;cout<<"Area of Circle:";circlee=object.area(5.00);cout<<circlee;cout<<"\nArea of squire:";squiree=object.area(4);cout<<squiree;cout<<"\nArea of rectangle:";rectanglee=object.area(5,6);cout<<rectanglee;

}

Page 9: Polymorphism

OPERATOR OVERLOADING

The process of making an operator to exhibit different behaviours in different instances is known as operator overloading.

Page 10: Polymorphism

Example:#include <iostream>using namespace std;class temp{

private:int count;

public:temp(){

count=5;}void operator ++(){count++;}void Display(){

cout<<"Count: "<<count;}

};int main(){

temp t;++t;t.Display();return 0;

}

Page 11: Polymorphism

DYNAMIC POLYMORPHISM

The appropriate member function could be selected while the programming is running.

Page 12: Polymorphism

VIRTUAL FUNCTION• A virtual function is a member function

that is declared within a base class and redefined by a derived class

• Keyword is virtual

Page 13: Polymorphism

EXAMPLE#include <iostream>using namespace std;class grandparent{

public:virtual void display()

{cout<<"INTELLIGENT\n";}};class parent : public grandparent{

public:void display()

{ cout<<"HANDSOM\n"; }

};

class son : public grandparent{

public:void display()

{ cout<<"OPEN MIND\n"; }

};int main(){

grandparent *pointer;parent dparent;son dson;pointer=&dparent;pointer->display(); pointer=&dson;pointer->display();

return 0;}

Page 14: Polymorphism

Follow us @ twitter.com/baabtra

Like us @ facebook.com/baabtra

Subscribe to us @ youtube.com/baabtra

Become a follower @ slideshare.net/BaabtraMentoringPartner

Connect to us @ in.linkedin.com/in/baabtra

Give a feedback @ massbaab.com/baabtra

Thanks in advance

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 15: Polymorphism

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode

Start up VillageEranakulam,Kerala, India.

Email: [email protected]

Contact Us