• Register
1 vote
50 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,260 points

Related questions

0 votes
1 answer 5 views
5 views
Problem: How to use Java syntax, of static and dynamic binding. I understand the basic concept, that static binding happens at compile-time and dynamic binding happens at runtime, but I can't figure out how they actually work specifically.
asked Mar 23 ummesalma 29.2k points
0 votes
1 answer 23 views
23 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 13k points
0 votes
1 answer 13 views
13 views
Problem Any idea on what is static and dynamic binding in c++
asked Mar 27 Subhendu 4.5k points
0 votes
1 answer 4 views
4 views
Problem: can someone please help for this problem? difference between static and dynamic binding in c++
asked Mar 27 Rohit kr 17.5k points
0 votes
1 answer 25 views
25 views
Problem; what is dynamic binding in c++
asked Feb 6 kaash_kashi 1.2k points
0 votes
1 answer 10 views
10 views
Problem I am not able to figure out What is dynamic binding in java?
asked Mar 8 neeraj 10.5k points
0 votes
0 answers 9 views
9 views
Problem: answer needed from expert? two way binding in angular 2
asked Mar 7 bilal b 420 points
0 votes
0 answers 5 views
5 views
Problem: any one help me in answering this question? two way data binding angular 2
asked Mar 7 bilal b 420 points
0 votes
1 answer 14 views
14 views
Can anyone explain this thing in simple words?
asked Jan 22 Lalit Kumar 1.2k points
0 votes
1 answer 5 views
5 views
Problem: Can anyone the difference between this? If anyone can help me then please answer me.
asked Mar 21 niyatiry 3.9k points