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

How do i get best Campus / Off Campus Placement?
Companies are looking for Skilled Freshers. So build your technical skills while doing MCA / BTech / BCA / BSc (IT or CS) into below areas- 1. Strong your programming & debugging skills ...

Variables and Storage Classes
Variables and Storage Classes auto default variable type local to a function static retains the values between function calls initialized only once unavailable outside the scope of the function...

Why do pointers have a datatype?
Before we start with pointers you must know what is a variable and a datatype. int a; This is the basic line in every program in 'C' . It means that we are asking the compiler to give us 2 bytes of space...

Programing Languages Learning Tricks
You want to learn that new language or library or framework as soon as possible, right? That’s understandable. Fortunately, there are a handful of tips that can help you to better retain all of that...

Harshal G.

0 0
0

C and C++ programming with memory level debugging
Understanding C and C++ programming by using memory level debugging. Step 1: Understand the Memory map of C executable Step 2: Start memory level debugging using popular IDE Step 3: Find the memory...

Looking for C++ Language Classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
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