UrbanPro
true

Learn C++ Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Polymorphism In C++

Ajit Deshmukh
07/02/2018 0 0

Basically polymorphism represents poly means many and morph means forms that many forms.

In which we are passing same message to different objets but every object will work for that message in their own manner. Means message is same but implementaion is different.

Take an real life example:

Suppose I have set of 50 LED TV's. There are two cases.

Case 1: Each tv has seperate remote but it is very difficult for me to manage each tve remote.

Case 2: I can use common remote to manage all LED Tv's. So it is very esay for me to manage all tv's with the help of common remote.

So second case represents polymorphism concept. We are providing common message but its implementaion is different.

Before going to polymorphism we will understand what is mean by binding.Binding represents association of object with function. There are two types of binding:

1. Compile time binding (Early binding or static binding): At compile time, binding takes place that will be representd as compilr time binding.

2. Run time binding (Late binding or dynamic binding): At run time,  binding takes place that will be represented as run time biding.

So we have to understood the concept of run time binding before that we need to understand two concepts.

1. Function overloading: Same function name but different signature represents function overloading. It will always takes place into same classes. Signature means may be no of parameters,sequence and its return type may be different.

2. Function overriding: All things of function(Function name,signature, return type) are same then it will be represented as function overriding. But function overriding always takes place in to different classes.

For example:

class A

{

       public:

                void show()

                {

                         cout<<"In class A"<<endl;

                 }

};

 

Class B : public A

{

                void show()

                {

                          cout<<"In class B";

                }

}

 

So In above example void show() function represents Function overrding concept. Function overriding always takes place into different classes when we are using inheritance concept.

Now let's see polymorphism concept:

For example:

class A

{

       public:

                void show()

                {

                         cout<<"In class A"<<endl;

                 }

};

 

class B: public A

{

                void show()

                {

                          cout<<"In class B";

                }

}

 

 

void main()

{

          A  *p;                   //base class pointer

          B  bObj;              //object of derived class

          p = &bObj;         //assigned address of class B object to base class pointer p.

          p->show();        //Trying to call derived class function withe the help of base class pointer.

}

 

In above example I am trying to call function of dereived class B. But here compile time binding takes place. Here compiler calls base class function because the compilre will check type of pointer. So we have declared pointer is type of Class A. Here compilre is not going to consider what address stored within pointer. It will just consider what type of pointer we have created and it will call base class function.

So how to resolve this problem. So for that we need to apply run time polymeophsim. In C++ run time polymeorphism is achieved by using virtual keyword. Here we have to just make base class function as  virtual. No need to use virtual keyword for each overrided function. Need to make base class function as virtual. Here all ovrrided function automatically made virtual because we using inheritance concept.

If we are using function overriding then and then your derived classes functins become virtual.

So just consider following change in above same example.We are just making void show() function of base class A as a virtual.

For example:

class A

{

       public:

                virtual  void show()

                {

                         cout<<"In class A"<<endl;

                 }

};

 

class B : public A

{

                void show()

                {

                          cout<<"In class B";

                }

}

void main()

{

          A  *p;                   //base class pointer

          B  bObj;              //object of derived class

          p = &bObj;         //assigned address of class B object to base class pointer p.

          p->show();        //It will call appropriate function of class B.

}

 

So now we will get expected output because we have applied run time polymorphism. We have just made base class function as a virtual. Now compiler going to check at run time what type of address we have stored within this base class pointer. So it will call appropriate function of derived class.

So in this way we have achieved polymorphism with the help of virtual keyword. So we are calling here derived classes function with the help of base class pointer.

0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

Contents Of C++ Programming
1. History of CPP 2. Concepts of OOP's 3. Introduction and Features of CPP (OOP's) 4. Characteristics of OOP's 5. CPP Keywords 6. Major Components of CPP: Encapsulation Inheritance Polymorphism 7....

Advantages of C++ Language
Advantages of C++ - C++ is a profoundly convenient dialect and is frequently the dialect of decision for multi-gadget, multi-stage application advancement. - C++ is a protest situated programming dialect...

What Would Be Life Cycle Of A Fresher After Campus In An IT Company?
1. Basic Technical Training: Since freshers are not subject matter experts so gone through 3 - 6 months basic technical training within Organization. 2. Technical Assessment: HR sends freshers to various...

Do You Know How Is Size Of Structure Defined?
Size of the structure is defined based on multiplies of bigger data type member in the structure. Example: If a structure contains integer, char, short data type, then size of the Structure will be multiples...

C++ Program-Factorial
//Header files #include<iostream.h>#include<conio.h> //Main function void main(){ int num,i; long factorial=1; //Function for clearing screen clrscr(); cout<< "Enter a number:";...
X

Looking for C++ Language Classes?

The best tutors for C++ Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C++ Language with the Best Tutors

The best Tutors for C++ Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more