Cases of static and dynamic binding in C

0 votes

The following code has four classes: Base1, Derived1 (derived from Base1), Base2, Derived2 (derived from Base2) (derived from Base2). 

The integer data1 and display data() functions are available in both base classes. 

Data1 and data2 are integers in both derived classes, as are the display data() functions.

In my code, I tried four different cases, which can be seen in the main function. 

I'm not sure which of these is a static binding case and which is a dynamic binding case. 

I need some assistance here. 

I'd also like to know which of these scenarios qualifies as "function overriding."

#include <iostream>
using namespace std;

class Base1{
protected:
    int data1;

public:
    Base1(int idata1 = 0) {
        data1 = idata1;
    }

    void display_data() {
        cout << "Base1: " << data1 << endl;
    }
};

class Derived1 : public Base1 {
protected:
    int data2;

public:
    Derived1(int idata1 = 0, int idata2 = 0) {
        data1 = idata1;
        data2 = idata2;
    }

    void display_data() {
        cout << "Derived1: " << data1 << ' ' << data2 << endl;
    }
};


class Base2 {
protected:
    int data1;

public:
    Base2(int idata1 = 0) {
        data1 = idata1;
    }

    virtual void display_data() {
        cout << "Base2: " << data1 << endl;
    }
};

class Derived2 : public Base2 {
protected:
    int data2;

public:
    Derived2(int idata1 = 0, int idata2 = 0) {
        data1 = idata1;
        data2 = idata2;
    }

    void display_data() {
        cout << "Derived2: " << data1 << ' ' << data2 << endl;
    }
};

int main()
{
    // case 1
    Derived1 d1(1, 10);
    d1.display_data();

    // case 2
    Base1* d2 = new Derived1(2, 20);
    d2->display_data();

    // case 3
    Derived2 d3(3, 30);
    d3.display_data();

    // case 4
    Base2* d4 = new Derived2(4, 40);
    d4->display_data();

    return 0;
}

OUPUT:

Derived1: 1 10
Base1: 2
Derived2: 3 30
Derived2: 4 40
Jun 2, 2022 in C++ by Nicholas
• 7,760 points
470 views

1 answer to this question.

0 votes

When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class).

When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime).

Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class.

Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding).

Derived1 d1(1, 10);
    d1.display_data();

You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method ().

The first condition is incorrect: d1 is neither a pointer nor a reference.

The second condition isn't acceptable: 

There is no virtual Derived1::display data.

The second call is for

 Base1* d2 = new Derived1(2, 20);
    d2->display_data();

We can see that the variable d2's declared type is Base1, but the instance is Derived1 (it is correct because of inheritance thus Derived1 is a Base1 class). 

However, you have no idea whether the display data method it will call is from Base1::display data or Derived1::display data.

Because we have the d2 of type pointer Base1*, the first condition is satisfied.

Because Base1::display data is not virtual, the second condition is incorrect. 

Because it is still a static binding, the function with the declared type will be called, so the code will call Base1::display data.

The third call is for

// case 3
    Derived2 d3(3, 30);
    d3.display_data();

This will result in a static binding, which will then be used to call the Derived3:: method. 

display data

The first requirement is not met: d3 is neither a pointer nor a reference.

The second condition is acceptable: 

It is virtual to use Derived2::display data.

This is the fourth call.

This time it's a dynamic binding: 

Base2* d4 = new Derived2(4, 40);
    d4->display_data();

The first requirement is satisfied: d4 is a pointer.

The second condition is acceptable: 

It is virtual to use Derived2::display data. 

At runtime, instead of calling the method from the declared type base2, the method will be called from the declared instance. 

Derived2::display data

answered Jun 7, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

Use of min and max functions in C++

Are std::min and std::max better than fmin ...READ MORE

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
332 views
0 votes
0 answers

Static vs dynamic type checking in C++

I'm curious in the differences between static ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
301 views
0 votes
0 answers

Purpose of Unions in C and C++

I had previously used unions with ease, but when I read this post and learned that this code union ARGB { uint32_t colour; ...READ MORE

Jul 13, 2022 in C++ by Nicholas
• 7,760 points
180 views
0 votes
0 answers

Purpose of Unions in C and C++

I had previously utilised unions with ease; however, I was startled when I read this post and discovered that this code union ARGB { uint32_t colour; ...READ MORE

Jul 13, 2022 in C++ by Nicholas
• 7,760 points
222 views
0 votes
1 answer

Why we actually need runtime polymorphism?

One of the most significant elements of ...READ MORE

answered Jun 10, 2022 in C++ by Damon
• 4,960 points
932 views
0 votes
0 answers

Is hiding implementation detail Encapsulation or Abstraction?

I have seen some people defining abstraction ...READ MORE

May 6, 2022 in Java by narikkadan
• 63,420 points
2,493 views
0 votes
1 answer

Why do we need virtual functions in C++?

A virtual function is a base class member function that we expect to redefine in derived classes. In the base class, a virtual function is used to ensure that the function is overridden.  This is especially true when a pointer from a base class points to an object from a derived class. For example, consider the code below: class Base ...READ MORE

answered May 27, 2022 in Others by Damon
• 4,960 points
708 views
0 votes
1 answer

When to use virtual destructors?

When you want to delete an instance of a derived class using a pointer to the base class, virtual destructors come in handy: class Base { // ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
435 views
0 votes
1 answer

Use of min and max functions in C++

The functions fmin and fmax are designed ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
9,384 views
0 votes
1 answer

The static keyword and its various uses in C++

Static variables exist during the "lifetime" of the translation unit in which they are declared, and: It cannot be accessible from any other translation unit if it is in a namespace scope (i.e. outside of functions and classes).  This is referred to as "internal linking" or "static storage lifetime."  (Except for constexpr, do not do this in headers; otherwise, you would wind up with a different variable in each translation unit, which is really confusing.) If it is a variable in a function, it, like any other local variable, cannot be accessed from outside the function.  (This is the mentioned local) Class members have no limited scope owing to static, but they may be referenced from both the class and an instance (like std::string::npos). locations as code: static std::string namespaceScope = "Hello"; void ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
293 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP