Dynamic Binding
Explanation
In OOPs Dynamic Binding refers to linking a
procedure call to the code that will be executed only at run time. The code
associated with the procedure in not known until the program is executed, which
is also known as late binding.
Example:
#include
<iostream.h>
int
Square(int x)
{ return
x*x; }
int
Cube(int x)
{ return
x*x*x; }
int
main()
{
int x
=10;
int
choice;
do
{
cout<<
"Enter 0 for square value, 1 for cube value: ";
cin>>
choice;
} while
(choice < 0 || choice > 1);
int
(*ptr) (int);
switch
(choice)
{
case 0:
ptr = Square; break;
case 1:
ptr = Cube; break;
}
cout<<
"The result is: " <<ptr(x) <<endl;
return 0;
}
Result:
Enter 0
for square value, 1 for cube value:0
The
result is:100
In the
above OOPs example the functions "Square" and "Cube" are
called only at runtime based on the value given for "choice". Then a
pointer "ptr" is used to call the appropriate function to get the
result.
• protected (C++)
• protected:
• [member-list]
• protected
base-class
The
protected keyword specifies access to class members in the member-list up to the next access specifier (public or private) or the end of the class definition. Class
members declared as protected can be used only by the following:
· Member functions of the class that originally declared these members. · Friends of the class that originally declared these members.
· Classes derived with public or protected access from the class that
originally declared these members.
· Direct privately derived classes that also have private access to
protected members.
When
preceding the name of a base class, the protected keyword specifies that the
public and protected members of the base class are protected members of its
derived classes. Protected members are not as private as private members, which
are accessible only to members of the class in which they are declared, but
they are not as public as public
members, which are accessible in any function. Protected members that are also
declared as static are accessible to
any friend or member function of a derived class. Protected members that are
not declared as static are
accessible to friends and member functions in a derived class only through a
pointer to, reference to, or object
of the derived class.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2026 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.