Static and Dynamic binding in C++ with examples (Early and Late binding)
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
π₯ 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.
π 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.
π 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.
π 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
π‘Dynamic Binding
π‘Function Overloading
π‘Function Overriding
π‘Virtual Functions
π‘Compile Time
π‘Runtime
π‘Inheritance
π‘Poll
π‘C++
π‘Early 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
hi everyone i am finally filming a new
video and i am so excited yesterday i
created a poll and i asked you which
topic would you like to see in my next
video and majority of you voted for
static versus dynamic binding in c plus
so that is what i'm going to cover in
this video and i also plan to make more
of these votings in the future so if you
want to participate in the decision
making you can vote here or on my
instagram account as well i take in
consideration both and i am going to
link my instagram in the description of
this video so the topic of static versus
dynamic binding in c plus is actually
very easy to understand if you already
understand some of the topics that we
covered in my previous videos
and i am going to link all of those
videos in the description and i will
link them in the exact order they need
to be watched so that it is easier for
you to learn and so that you can get
that knowledge step by step
so if at any point during this video you
feel like you don't understand something
or you feel like you have a knowledge
gap make sure to visit the description
and there you will find all the help and
all the details that you need so let's
return to explaining differences between
static and dynamic binding in c plus and
the first thing that i want to explain
is what is binding so in order to
understand this you need to be familiar
with functions in c plus and in case
that you are not or if you need a little
reminder i'm going to link a playlist
here
so
what is binding in c plus binding means
associating the call of a function with
the definition of that function now in c
plus we have two different types of
binding static binding and dynamic
binding in static binding all of the
information necessary in order to
perform that association is available at
compile time so that association is
happening at compile time and another
name for static binding in c plus plus
is also compile time binding or early
binding
now on the other hand dynamic binding is
performed during runtime because all of
the information necessary to perform
that binding
is not available at compile time but it
is available at run time so another name
for dynamic binding is late binding or
runtime binding now if you are wondering
about which one is better the answer is
that both have some advantages and
disadvantages in static binding as i
already said the process of associating
the call of a function with its
definition is happening during the
compile time which means that it is not
happening during the runtime which
furthermore will make our program run
faster on the other hand in dynamic
binding that process of association is
happening during the runtime which will
make our program a little bit slower so
now your question might be saldena why
don't we always use static binding then
well the answer is because dynamic
binding also has its own advantages and
the main advantage of dynamic binding is
it is very flexible and it allows us to
decide which function definition we want
to invoke at runtime so we make that
decision at runtime based on the data
and i am going to show you how that
works on the example later in this video
so how can we achieve static binding and
dynamic binding
you need to know that static binding
happens by default so by using a normal
function call or by using function
overloading or operator overloading that
is how we achieve static binding on the
other hand dynamic binding happens when
we use virtual functions or function
overriding now please don't confuse
these two please don't confuse function
overloading and function overriding
function overloading is used with static
binding and function overloading means
creating multiple functions that have
the same name but they have different
parameters that is function overloading
on the other hand function overriding is
used with dynamic binding and it is
achieved when we create inside child
class a newer version of a function that
already exists inside parent class now
if you are confused about any of these
things that i mentioned you can find all
of these topics covered in individual
videos which i will link in the
description and now i'm going to show
you how we can achieve static and
dynamic binding in code so the first one
that i want to demonstrate is static
binding and as i already said it is
achieved by normal function calls or by
using function overloading or operator
overloading so
let's create two functions called sum
numbers and the first function will
receive two numbers and then the second
function will receive three numbers and
let's explain then how static binding
works
so
let's create a function that returns
float
let's call it some numbers
okay
and it will receive two float parameters
so number a
and number
b
and the job of this function will be to
return
the result of a plus
b
okay so that is the first function and
the second function will be very similar
but instead of receiving two parameters
it will receive three parameters so i
will say float a float b and then float
c
and its job will be to return the result
of a plus b
plus
c like this so what i want to do now is
i want to invoke these two functions so
i will say
c out
some
numbers and i will pass number one and
number two
add end line and then in the second line
again i will invoke some numbers but
this time i will pass three parameters
like this so
if i run my program now
you will probably guess correctly what
is going to happen okay so for this
first some numbers function we get the
result of three and then for this second
we get the result of six so let's close
this program and let's explain what is
happening here so even though this
function here and this function here
have the same name it is still possible
to decide at compile time which one of
the two will be invoked at this line
here and then which one will be invoked
at this line here so how can we decide
that at compile time the answer is by
looking at the parameters of these two
functions so the first one receives two
parameters and the second one receives
three parameters so
when we try to invoke some numbers and
we pass two parameters at compile time
we are already associating this function
call here with this definition here and
then in this second line when we try to
invoke a function that is also called
sum numbers but this time we pass three
numbers so three parameters we are again
at compile time associating this
function call with this function
definition and as i already said all of
that is happening at compile time
because all of the information is
already available at compile time so
that is how we achieve static binding in
c plus so let's delete all of this code
i don't need it anymore because i want
to explain how dynamic binding works in
c plus and in order to understand
dynamic binding you need to be familiar
with inheritance in c plus plus and with
virtual functions and i will link those
videos in the description so what i want
to do is i want to create two classes
the first one which will be base class
is called user and then the second one
which will be child class or derived
class is called super user so let's do
that
i will say
class
user
okay
and then as i said the second class will
be super user
like this
okay and this super user will be a child
class of user class that we created here
so i will say that it inherits publicly
from user class like this okay now what
i want to do is i want to create a
method inside this user class and then i
will create a method with the same name
and same parameters inside this super
user class so
i will say public because all of the
members of a class are private by
default and unless we say public
explicitly those members will not be
available outside of that class that is
why i'm putting this access modifier so
let's create a method called get
permissions i will say
void get permissions
like this
and it will not receive any parameters
and inside this method i will just say
see out users can see
limited info
like this
okay now what i want to do is i want to
create this method inside my super user
class
but here i will say that
super users
can see
all the info so we have the same method
here and here and both of these methods
have the same parameter list which is
zero parameters but they have different
implementation this one says user can
see limited info and then this one says
super users can see all of the info now
in order to achieve dynamic binding we
are missing one more thing and that is
virtual keyword here so here i will say
virtual like this and again if you are
not familiar with virtual functions
check out that video in the description
okay
now what i want to do inside my main
function is i want to create a user and
a super user so i will say
user and i will call it you
so we created a user and let's now
create a super user as well so i will
say
super user and let's call it
s
okay what i want to do with these two is
i want to create a list of pointers of
type user and inside that list i will
push both this user and this super user
and that will be possible because super
user deep down is still user because it
inherits from user class so let's create
that list let's say list of user
pointers
like this and in order to be able to use
list collection you need to include it
here you need to say
include
list
like this and as you can see now the
error has disappeared so i am creating a
list of user pointers and i will call it
users
like this and as i already said inside
this users list i will push boot the
address of this user and this super user
so i will say users dot push
back and here i will put the address of
my user and then i will do the same for
my super user and a quick tip if you are
wondering why i'm using this ampersand
symbol here it is because i want to add
the address of my user to this list here
and then the address of a super user to
this list here so why am i doing that
well because this here is a list of user
pointers which means that here we need
to add addresses and if i try to remove
this ampersand symbol from here or from
here as you can see we will get errors
because as i already said this here is a
list of user pointers so here we need to
add addresses so i will return this
ampersand symbol and then here as well
okay so what i want to do now is i want
to iterate through the list of my users
and for each user inside that list
whether it is user or super user i will
invoke a function called get permissions
and it should dynamically bind
appropriate function with that call so
for super user this function here should
be invoked and then for
user this function here should be
invoked so let's demonstrate how that
works
i will create for each loop here so i
will say 4
so for every user pointer which i will
call user ptr
inside my users
list what i want to do is i want to say
user pointer
please invoke function called get
permissions and keep in mind that here i
need to use this symbol instead of dot
because this here is a pointer so let's
return that symbol okay so if i run my
program let's see what is going to
happen
okay
and as you can see it says users can see
limited information and then super users
can see all of the information so for
our user we invoke this implementation
here and then for super user we invoke
this implementation here and that
decision is made at runtime because it
is based on the value that we will find
inside this variable here and that value
that data is not available at compile
time but that data can only be available
at runtime so that is how we achieve
dynamic binding in c plus so i hope that
this video was helpful if it was please
give it a thumbs up for the youtube
algorithm and also comment about the
topics that you would like to see in the
future and i am going to create more
polls where we will decide together
about the topics for my next videos and
you can vote both here on youtube and on
my instagram account as well but if you
want to increase the chances of your
topic appearing in the next video make
sure to vote both on instagram and on
youtube so i believe that is all for
this video thank you very much for
watching and i'm going to see you in
some other video bye
5.0 / 5 (0 votes)