Static and Dynamic binding in C++ with examples (Early and Late binding)

CodeBeauty
15 Jul 202115:24

Summary

TLDRIn this informative video, the creator addresses a popular request from their audience by explaining the concepts of static and dynamic binding in C++. The video begins with an introduction to the topic, which was chosen through a community poll, and promises future engagement through similar voting. The script provides a clear definition of binding and distinguishes between static and dynamic binding, highlighting their respective advantages and disadvantages. Static binding, occurring at compile time, is noted for its efficiency, while dynamic binding, decided at runtime, offers flexibility. The creator demonstrates both concepts with practical examples, including function overloading for static binding and the use of virtual functions for dynamic binding. The video concludes with an invitation for viewers to participate in future topic selection and to provide feedback.

Takeaways

  • 🎬 The video is about explaining static versus dynamic binding in C++ based on a poll result.
  • πŸ“Š The majority voted for the topic of static versus dynamic binding, which the video covers.
  • πŸ”— The presenter plans to conduct more polls for future video topics and encourages participation.
  • πŸ”— Links to previous videos and related topics are provided in the description for context.
  • πŸ“š Static binding in C++ occurs at compile time, also known as early or compile-time binding.
  • πŸ“š Dynamic binding happens at runtime and is also called late or runtime binding.
  • ⚑ Static binding makes programs run faster because the function association is done at compile time.
  • πŸ”„ Dynamic binding, while slower, offers flexibility by allowing function selection at runtime based on data.
  • πŸ“ Static binding is achieved by default with normal function calls, function overloading, or operator overloading.
  • πŸ“ Dynamic binding is achieved using virtual functions or function overriding in a derived class.
  • πŸ€” Function overloading (for static binding) should not be confused with function overriding (for dynamic binding).
  • πŸ’‘ The video provides a code example demonstrating static binding with function overloading.
  • πŸ’» A second code example illustrates dynamic binding using inheritance, virtual functions, and a list of user pointers.
  • πŸ“£ The presenter asks for a thumbs up and comments for future topic suggestions and mentions more polls for topic selection.

Q & A

  • What topic was voted for by the majority in the poll for the next video?

    -The majority voted for a video on static versus dynamic binding in C++.

  • How can viewers participate in the decision-making for future video topics?

    -Viewers can participate by voting in the polls, which are available both on the YouTube channel and the creator's Instagram account.

  • What does 'binding' refer to in the context of C++ functions?

    -Binding in C++ refers to associating the call of a function with its definition.

  • What are the two types of binding in C++?

    -The two types of binding in C++ are static binding and dynamic binding.

  • At what time is static binding resolved?

    -Static binding is resolved at compile time.

  • What is another name for static binding in C++?

    -Static binding is also known as compile time binding or early binding.

  • How does dynamic binding differ from static binding in terms of when it is resolved?

    -Dynamic binding is resolved during runtime, as opposed to static binding which is resolved at compile time.

  • What is the main advantage of dynamic binding over static binding?

    -The main advantage of dynamic binding is its flexibility, allowing decisions on which function definition to invoke to be made at runtime.

  • How is static binding achieved in C++?

    -Static binding is achieved by default through normal function calls, function overloading, or operator overloading.

  • How is dynamic binding achieved in C++?

    -Dynamic binding is achieved through the use of virtual functions or function overriding.

  • What is the difference between function overloading and function overriding?

    -Function overloading is used with static binding and involves creating multiple functions with the same name but different parameters. Function overriding is used with dynamic binding and occurs when a function in a child class overrides a function in the parent class.

  • How does the video demonstrate static binding?

    -The video demonstrates static binding by showing a function called 'sumNumbers' being called with different numbers of parameters, with the appropriate function being determined at compile time based on the parameters passed.

  • In the video, what is used to demonstrate dynamic binding?

    -The video uses a base class called 'User' and a derived class called 'SuperUser', along with virtual functions, to demonstrate dynamic binding.

  • How does the video show that dynamic binding is decided at runtime?

    -The video shows that dynamic binding is decided at runtime by creating a list of user pointers that includes both 'User' and 'SuperUser' objects, and then invoking a method called 'getPermissions', which is resolved differently for each object at runtime.

  • What does the video suggest for viewers who want to see their suggested topics covered in future videos?

    -The video suggests that viewers should vote on both the YouTube and Instagram polls to increase the chances of their suggested topics being covered in future videos.

Outlines

00:00

πŸŽ₯ Introduction to Static vs. Dynamic Binding in C++

The video starts with the host expressing excitement about filming a new video, which will cover the topic of static versus dynamic binding in C++. This topic was chosen based on a poll the host created, where the majority voted for it. The host plans to involve the audience in future topic selections through voting on their YouTube channel and Instagram account. The video aims to explain the concept of binding in C++, which involves associating a function call with its definition. The host will differentiate between static binding, which occurs at compile time, and dynamic binding, which happens at runtime. They will also provide links to previous videos that cover related topics in the correct order for viewers to follow along and understand the material step by step.

05:01

πŸ“š Demonstrating Static Binding with Function Overloading

In this section, the host demonstrates static binding in C++ through function overloading. They create two functions named 'sumNumbers' with different numbers of parameters: one taking two floats and the other taking three. The host explains how static binding allows the compiler to determine which function to call based on the parameters provided at compile time. By invoking these functions with different numbers of arguments, the host illustrates that the compiler can distinguish between the two overloaded functions and bind the correct function call to its definition without any ambiguity.

10:04

πŸ”„ Exploring Dynamic Binding with Inheritance and Virtual Functions

The host moves on to dynamic binding, which requires an understanding of inheritance and virtual functions in C++. They create a base class 'User' and a derived class 'SuperUser'. Within these classes, they define a method 'getPermissions' with the same name but different implementations. To enable dynamic binding, the host declares the 'getPermissions' method in the base class as 'virtual'. The host then creates a list of 'User' pointers, adding both a 'User' and a 'SuperUser' object to it. Using a for-each loop, they iterate through the list and invoke the 'getPermissions' method on each object. The host explains that the decision on which 'getPermissions' method to invoke is made at runtime based on the object's actual type, demonstrating the flexibility and power of dynamic binding.

15:06

πŸ‘‹ Conclusion and Future Engagement

To conclude the video, the host thanks the viewers for watching and encourages them to like the video and comment with topics they would like to see in future videos. The host also mentions that they will be creating more polls to involve the audience in deciding the content of upcoming videos. They remind viewers to vote on both YouTube and Instagram to increase the chances of their preferred topics being selected. The host signs off with a friendly 'bye', indicating the end of the video.

Mindmap

Keywords

πŸ’‘Static Binding

Static Binding, also known as early binding or compile-time binding, refers to the process where the association between a function call and its definition is resolved at compile time. In the context of the video, static binding is achieved by default through normal function calls, function overloading, or operator overloading. An example from the script is the 'sumNumbers' function, which is called with a specific number of parameters, allowing the compiler to determine which function to invoke without any ambiguity.

πŸ’‘Dynamic Binding

Dynamic Binding, also known as late binding or runtime binding, is the process where the association between a function call and its definition is resolved at runtime. This is in contrast to static binding and is typically used with virtual functions or function overriding in object-oriented programming. The video demonstrates dynamic binding through a 'getUserPermissions' method that behaves differently when called on a 'User' object versus a 'SuperUser' object, showcasing the flexibility of choosing function implementations at runtime.

πŸ’‘Function Overloading

Function Overloading is a feature in C++ that allows the creation of multiple functions with the same name but different parameter lists. It is used in the context of static binding. In the script, the presenter discusses creating two 'sumNumbers' functions with different numbers of parameters, which is an example of function overloading. This enables static binding because the compiler can determine which function to call based on the number of arguments passed at compile time.

πŸ’‘Function Overriding

Function Overriding occurs when a derived class provides a new implementation of a function that already exists in the base class. It is used in the context of dynamic binding. The video script includes an example where the 'SuperUser' class overrides the 'getUserPermissions' method from the 'User' class, allowing for different behavior when the method is called on an object of type 'SuperUser'.

πŸ’‘Virtual Functions

Virtual Functions are a key concept in C++ that enable dynamic binding. They are declared with the 'virtual' keyword in the base class and can be overridden in derived classes. In the video, the 'getUserPermissions' method is declared as virtual, which allows for dynamic binding to occur. When this method is called on a pointer or reference to the base class type, the correct function is invoked based on the actual object type at runtime.

πŸ’‘Compile Time

Compile Time refers to the time when the source code is translated into machine code by the compiler. In the video, the presenter explains that static binding occurs at compile time, as all necessary information to associate a function call with its definition is available at this stage. An example from the script is the resolution of which 'sumNumbers' function to call based on the number of parameters passed during the function call.

πŸ’‘Runtime

Runtime is the time when a program is executed by the computer. In the context of the video, dynamic binding takes place during runtime because the necessary information to associate a function call with its definition is only available when the program is running. The script demonstrates this with the 'getUserPermissions' method, where the decision about which function implementation to invoke is made based on the object type at runtime.

πŸ’‘Inheritance

Inheritance is a fundamental concept in object-oriented programming that allows a class (child class or derived class) to inherit attributes and methods from another class (base class or parent class). The video script uses inheritance to demonstrate dynamic binding, with the 'SuperUser' class inheriting from the 'User' class and providing a new implementation of the 'getUserPermissions' method.

πŸ’‘Poll

A Poll is a survey used to gather opinions or preferences, often used for decision-making. In the video script, the presenter mentions creating a poll to decide on the topic for the next video, allowing the audience to vote and participate in the content creation process. This method of audience engagement is used to determine the topic of 'static versus dynamic binding in C++' for the current video.

πŸ’‘C++

C++ is a high-performance, general-purpose programming language known for its object-oriented features and efficiency. The entire video is centered around explaining concepts in C++, specifically focusing on static and dynamic binding, function overloading, function overriding, and the use of virtual functions. These are all core features of the C++ language that are essential for understanding advanced programming concepts.

πŸ’‘Early Binding

Early Binding is a term synonymous with static binding, referring to the process where the function call is associated with its definition at compile time. In the video, the presenter discusses early binding in the context of function calls that are resolved before the program is run, which is beneficial for performance but less flexible compared to dynamic binding.

Highlights

Introduction of a new video on static versus dynamic binding in C++ based on a viewer poll.

Future plans to involve viewers in decision-making through voting for video topics.

Explanation of binding in C++ as associating function calls with their definitions.

Static binding occurs at compile time, also known as early binding.

Dynamic binding happens during runtime, also called late binding.

Advantages of static binding include faster program execution.

Dynamic binding offers flexibility by allowing function selection at runtime.

Static binding is achieved by default through normal function calls and function overloading.

Dynamic binding is achieved using virtual functions and function overriding.

Clarification on the difference between function overloading and function overriding.

Demonstration of static binding with function overloading in code.

Explanation of dynamic binding using inheritance and virtual functions.

Code example illustrating dynamic binding with a user and super user class.

How to use a list of pointers to demonstrate dynamic binding in C++.

Runtime decision-making in dynamic binding based on the object's data.

Viewer engagement request for likes, comments, and participation in future polls.

Closing remarks and a thank you for watching the video.

Transcripts

play00:00

hi everyone i am finally filming a new

play00:02

video and i am so excited yesterday i

play00:05

created a poll and i asked you which

play00:08

topic would you like to see in my next

play00:10

video and majority of you voted for

play00:13

static versus dynamic binding in c plus

play00:17

so that is what i'm going to cover in

play00:19

this video and i also plan to make more

play00:21

of these votings in the future so if you

play00:24

want to participate in the decision

play00:26

making you can vote here or on my

play00:28

instagram account as well i take in

play00:31

consideration both and i am going to

play00:33

link my instagram in the description of

play00:35

this video so the topic of static versus

play00:37

dynamic binding in c plus is actually

play00:40

very easy to understand if you already

play00:42

understand some of the topics that we

play00:44

covered in my previous videos

play00:46

and i am going to link all of those

play00:48

videos in the description and i will

play00:50

link them in the exact order they need

play00:53

to be watched so that it is easier for

play00:55

you to learn and so that you can get

play00:57

that knowledge step by step

play01:00

so if at any point during this video you

play01:03

feel like you don't understand something

play01:05

or you feel like you have a knowledge

play01:07

gap make sure to visit the description

play01:09

and there you will find all the help and

play01:11

all the details that you need so let's

play01:14

return to explaining differences between

play01:16

static and dynamic binding in c plus and

play01:19

the first thing that i want to explain

play01:21

is what is binding so in order to

play01:24

understand this you need to be familiar

play01:26

with functions in c plus and in case

play01:28

that you are not or if you need a little

play01:31

reminder i'm going to link a playlist

play01:33

here

play01:34

so

play01:35

what is binding in c plus binding means

play01:39

associating the call of a function with

play01:42

the definition of that function now in c

play01:46

plus we have two different types of

play01:48

binding static binding and dynamic

play01:51

binding in static binding all of the

play01:54

information necessary in order to

play01:56

perform that association is available at

play02:00

compile time so that association is

play02:02

happening at compile time and another

play02:05

name for static binding in c plus plus

play02:07

is also compile time binding or early

play02:11

binding

play02:12

now on the other hand dynamic binding is

play02:15

performed during runtime because all of

play02:19

the information necessary to perform

play02:21

that binding

play02:22

is not available at compile time but it

play02:25

is available at run time so another name

play02:28

for dynamic binding is late binding or

play02:31

runtime binding now if you are wondering

play02:34

about which one is better the answer is

play02:37

that both have some advantages and

play02:39

disadvantages in static binding as i

play02:42

already said the process of associating

play02:45

the call of a function with its

play02:47

definition is happening during the

play02:49

compile time which means that it is not

play02:51

happening during the runtime which

play02:53

furthermore will make our program run

play02:56

faster on the other hand in dynamic

play02:59

binding that process of association is

play03:01

happening during the runtime which will

play03:04

make our program a little bit slower so

play03:07

now your question might be saldena why

play03:09

don't we always use static binding then

play03:12

well the answer is because dynamic

play03:15

binding also has its own advantages and

play03:19

the main advantage of dynamic binding is

play03:22

it is very flexible and it allows us to

play03:25

decide which function definition we want

play03:28

to invoke at runtime so we make that

play03:31

decision at runtime based on the data

play03:34

and i am going to show you how that

play03:36

works on the example later in this video

play03:40

so how can we achieve static binding and

play03:42

dynamic binding

play03:44

you need to know that static binding

play03:46

happens by default so by using a normal

play03:49

function call or by using function

play03:51

overloading or operator overloading that

play03:54

is how we achieve static binding on the

play03:57

other hand dynamic binding happens when

play04:00

we use virtual functions or function

play04:03

overriding now please don't confuse

play04:06

these two please don't confuse function

play04:08

overloading and function overriding

play04:11

function overloading is used with static

play04:14

binding and function overloading means

play04:17

creating multiple functions that have

play04:19

the same name but they have different

play04:22

parameters that is function overloading

play04:25

on the other hand function overriding is

play04:27

used with dynamic binding and it is

play04:30

achieved when we create inside child

play04:33

class a newer version of a function that

play04:36

already exists inside parent class now

play04:40

if you are confused about any of these

play04:42

things that i mentioned you can find all

play04:44

of these topics covered in individual

play04:46

videos which i will link in the

play04:48

description and now i'm going to show

play04:51

you how we can achieve static and

play04:53

dynamic binding in code so the first one

play04:56

that i want to demonstrate is static

play04:58

binding and as i already said it is

play05:01

achieved by normal function calls or by

play05:04

using function overloading or operator

play05:06

overloading so

play05:08

let's create two functions called sum

play05:12

numbers and the first function will

play05:13

receive two numbers and then the second

play05:16

function will receive three numbers and

play05:18

let's explain then how static binding

play05:20

works

play05:21

so

play05:22

let's create a function that returns

play05:25

float

play05:26

let's call it some numbers

play05:30

okay

play05:31

and it will receive two float parameters

play05:34

so number a

play05:36

and number

play05:37

b

play05:38

and the job of this function will be to

play05:41

return

play05:42

the result of a plus

play05:44

b

play05:45

okay so that is the first function and

play05:48

the second function will be very similar

play05:50

but instead of receiving two parameters

play05:53

it will receive three parameters so i

play05:55

will say float a float b and then float

play05:59

c

play05:59

and its job will be to return the result

play06:02

of a plus b

play06:04

plus

play06:05

c like this so what i want to do now is

play06:08

i want to invoke these two functions so

play06:10

i will say

play06:12

c out

play06:13

some

play06:14

numbers and i will pass number one and

play06:17

number two

play06:19

add end line and then in the second line

play06:22

again i will invoke some numbers but

play06:25

this time i will pass three parameters

play06:27

like this so

play06:29

if i run my program now

play06:32

you will probably guess correctly what

play06:33

is going to happen okay so for this

play06:36

first some numbers function we get the

play06:38

result of three and then for this second

play06:40

we get the result of six so let's close

play06:43

this program and let's explain what is

play06:45

happening here so even though this

play06:47

function here and this function here

play06:50

have the same name it is still possible

play06:52

to decide at compile time which one of

play06:56

the two will be invoked at this line

play06:58

here and then which one will be invoked

play07:01

at this line here so how can we decide

play07:04

that at compile time the answer is by

play07:06

looking at the parameters of these two

play07:09

functions so the first one receives two

play07:12

parameters and the second one receives

play07:14

three parameters so

play07:16

when we try to invoke some numbers and

play07:19

we pass two parameters at compile time

play07:22

we are already associating this function

play07:25

call here with this definition here and

play07:28

then in this second line when we try to

play07:31

invoke a function that is also called

play07:33

sum numbers but this time we pass three

play07:36

numbers so three parameters we are again

play07:39

at compile time associating this

play07:42

function call with this function

play07:45

definition and as i already said all of

play07:47

that is happening at compile time

play07:50

because all of the information is

play07:51

already available at compile time so

play07:54

that is how we achieve static binding in

play07:57

c plus so let's delete all of this code

play08:00

i don't need it anymore because i want

play08:02

to explain how dynamic binding works in

play08:06

c plus and in order to understand

play08:09

dynamic binding you need to be familiar

play08:11

with inheritance in c plus plus and with

play08:13

virtual functions and i will link those

play08:16

videos in the description so what i want

play08:19

to do is i want to create two classes

play08:22

the first one which will be base class

play08:25

is called user and then the second one

play08:27

which will be child class or derived

play08:30

class is called super user so let's do

play08:33

that

play08:34

i will say

play08:35

class

play08:37

user

play08:38

okay

play08:39

and then as i said the second class will

play08:42

be super user

play08:46

like this

play08:47

okay and this super user will be a child

play08:51

class of user class that we created here

play08:54

so i will say that it inherits publicly

play08:58

from user class like this okay now what

play09:02

i want to do is i want to create a

play09:05

method inside this user class and then i

play09:08

will create a method with the same name

play09:10

and same parameters inside this super

play09:13

user class so

play09:15

i will say public because all of the

play09:18

members of a class are private by

play09:21

default and unless we say public

play09:24

explicitly those members will not be

play09:26

available outside of that class that is

play09:29

why i'm putting this access modifier so

play09:32

let's create a method called get

play09:34

permissions i will say

play09:36

void get permissions

play09:42

like this

play09:43

and it will not receive any parameters

play09:46

and inside this method i will just say

play09:48

see out users can see

play09:52

limited info

play09:54

like this

play09:57

okay now what i want to do is i want to

play10:00

create this method inside my super user

play10:04

class

play10:05

but here i will say that

play10:07

super users

play10:09

can see

play10:12

all the info so we have the same method

play10:16

here and here and both of these methods

play10:19

have the same parameter list which is

play10:21

zero parameters but they have different

play10:25

implementation this one says user can

play10:27

see limited info and then this one says

play10:29

super users can see all of the info now

play10:33

in order to achieve dynamic binding we

play10:35

are missing one more thing and that is

play10:38

virtual keyword here so here i will say

play10:41

virtual like this and again if you are

play10:44

not familiar with virtual functions

play10:46

check out that video in the description

play10:49

okay

play10:50

now what i want to do inside my main

play10:52

function is i want to create a user and

play10:55

a super user so i will say

play10:58

user and i will call it you

play11:01

so we created a user and let's now

play11:03

create a super user as well so i will

play11:05

say

play11:06

super user and let's call it

play11:08

s

play11:09

okay what i want to do with these two is

play11:12

i want to create a list of pointers of

play11:15

type user and inside that list i will

play11:19

push both this user and this super user

play11:23

and that will be possible because super

play11:25

user deep down is still user because it

play11:29

inherits from user class so let's create

play11:33

that list let's say list of user

play11:37

pointers

play11:38

like this and in order to be able to use

play11:41

list collection you need to include it

play11:43

here you need to say

play11:45

include

play11:46

list

play11:48

like this and as you can see now the

play11:50

error has disappeared so i am creating a

play11:53

list of user pointers and i will call it

play11:57

users

play11:58

like this and as i already said inside

play12:01

this users list i will push boot the

play12:04

address of this user and this super user

play12:08

so i will say users dot push

play12:12

back and here i will put the address of

play12:15

my user and then i will do the same for

play12:18

my super user and a quick tip if you are

play12:22

wondering why i'm using this ampersand

play12:24

symbol here it is because i want to add

play12:27

the address of my user to this list here

play12:30

and then the address of a super user to

play12:33

this list here so why am i doing that

play12:36

well because this here is a list of user

play12:39

pointers which means that here we need

play12:41

to add addresses and if i try to remove

play12:45

this ampersand symbol from here or from

play12:47

here as you can see we will get errors

play12:50

because as i already said this here is a

play12:53

list of user pointers so here we need to

play12:56

add addresses so i will return this

play12:59

ampersand symbol and then here as well

play13:02

okay so what i want to do now is i want

play13:05

to iterate through the list of my users

play13:08

and for each user inside that list

play13:10

whether it is user or super user i will

play13:13

invoke a function called get permissions

play13:17

and it should dynamically bind

play13:20

appropriate function with that call so

play13:24

for super user this function here should

play13:26

be invoked and then for

play13:27

user this function here should be

play13:30

invoked so let's demonstrate how that

play13:32

works

play13:32

i will create for each loop here so i

play13:36

will say 4

play13:37

so for every user pointer which i will

play13:40

call user ptr

play13:43

inside my users

play13:45

list what i want to do is i want to say

play13:49

user pointer

play13:51

please invoke function called get

play13:54

permissions and keep in mind that here i

play13:57

need to use this symbol instead of dot

play14:00

because this here is a pointer so let's

play14:05

return that symbol okay so if i run my

play14:08

program let's see what is going to

play14:10

happen

play14:11

okay

play14:12

and as you can see it says users can see

play14:15

limited information and then super users

play14:17

can see all of the information so for

play14:20

our user we invoke this implementation

play14:23

here and then for super user we invoke

play14:26

this implementation here and that

play14:29

decision is made at runtime because it

play14:32

is based on the value that we will find

play14:35

inside this variable here and that value

play14:39

that data is not available at compile

play14:41

time but that data can only be available

play14:44

at runtime so that is how we achieve

play14:47

dynamic binding in c plus so i hope that

play14:50

this video was helpful if it was please

play14:52

give it a thumbs up for the youtube

play14:54

algorithm and also comment about the

play14:56

topics that you would like to see in the

play14:58

future and i am going to create more

play15:01

polls where we will decide together

play15:03

about the topics for my next videos and

play15:05

you can vote both here on youtube and on

play15:08

my instagram account as well but if you

play15:10

want to increase the chances of your

play15:12

topic appearing in the next video make

play15:14

sure to vote both on instagram and on

play15:17

youtube so i believe that is all for

play15:19

this video thank you very much for

play15:21

watching and i'm going to see you in

play15:22

some other video bye

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
C++ ProgrammingStatic BindingDynamic BindingFunction OverloadingFunction OverridingInheritanceVirtual FunctionsCompile TimeRuntimeEducational Content