• Register
1 vote
42 views

Problem:

Well, I found two new terms in my C++ textbook. They are Static Binding and Dynamic Binding. Could you guys please elaborate those terms in simple words for me? My textbook explained it in a difficult way. I am looking forward to having your explanation.

Thanks for your help.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes
 
Best answer

Solution:

Binding mentions to the process of converting identifiers into addresses. Binding takes place either in run time or at compile time. There are two types of binding in C++. One is Static/Early binding and the other one is dynamic/late binding.

Static Binding: Static/Early binding says by its own name. In this binding, the compiler directly associates an address to the function call. It substitutes the call with a machine language guidance that tells the mainframe to jump to the address directly of the function. Let’s have an example of Static Binding

#include<iostream> 
using namespace std; 
    
class BaseClass{
    public: 
    void show(){
        cout<<" Displaying in BaseClass \n";
    } 
}; 
    
class Derived: public BaseClass{
    public: 
    void show(){
        cout<<"Displaying in DerivedClass \n";
    } 
};
    
int main(void){
    BaseClass *bc = new Derived;
    bc->show();   
    return 0; 
}

Output should be

Displaying in BaseClass

 

Dynamic Binding: Dynamic Binding is also known as runtime polymorphism. Late binding let the compiler identifies the kind of object at runtime then meets the call with the right function definition. This type of binding can be achieved by declaring a virtual function.

#include<iostream> 
using namespace std; 
    
class BaseClass{
    public:
    virtual void show(){
        cout<<"Displaying in BaseClass \n";
    } 
};
    
class Derived: public BaseClass{
    public: 
    void show(){
        cout<<"Displaying in Derived \n";
    } 
}; 

int main(void){
    BaseClass *bc = new Derived; 
    bc->show(); 
    return 0; 
}

In this case, the output should be 

Displaying in Derived

This might help you.

13 9 6
94,240 points

Related questions

0 votes
1 answer 18 views
18 views
Problem: Association of method call to the method body is known as binding. There are two types of binding: Static Binding that happens at compile time and Dynamic Binding that happens at runtime. Before I explain static and dynamic binding in java, lets see few terms that will help you understand this concept better.
asked Dec 5, 2020 Mashhoodch 10.2k points
0 votes
1 answer 14 views
14 views
Problem; what is dynamic binding in c++
asked Feb 6 kaash_kashi 1.2k points
0 votes
1 answer 11 views
11 views
Can anyone explain this thing in simple words?
asked Jan 22 Lalit Kumar 1.2k points
0 votes
1 answer 3 views
3 views asked Feb 25 Akhil k 1.3k points
0 votes
1 answer 7 views
0 votes
1 answer 10 views
10 views
Static binding or dynamic binding? What kind of polymorphism does this show?
asked Feb 8 TeamScript 13.5k points
0 votes
1 answer 8 views
0 votes
1 answer 14 views
14 views
Problem: Each element of the array is a Number object. Is it with triple pointer? (***)
asked Jan 25 sasha 13.2k points
0 votes
1 answer 10 views
10 views
Problem: I have been making a code that allows me to use an integer pointer, which stores all the integers that I want until I type the word "end" and then print them, I did it in the following way: #include <iostream> #include <string> using namespace std; int main () ... why does that happen, and how could I correct it so that it allows me to implement the 2 codes at once? Thanks for your help.
asked Dec 25, 2020 sasha 13.2k points
0 votes
1 answer 107 views
107 views
Problem: I want to initialize my variables dynamically. I have very basic knowledge of C++. Please help.
asked Oct 13, 2020 Code Learner 6.5k points