C++ program which demonstrates static binding and dynamic binding.
Static Binding
#include <iostream>
using namespace std;
class Calculate
{
public:
int mult(int a, int b ){
return a * b;
}
int mult(int a, int b, int c) {
return a * b * c;
}
};
int main()
{
Calculate c1;
cout << "Product is " << c1.mult(10, 20) << endl;
cout << "Product is " << c1.mult(10, 20, 30) << endl;
return 0;
}
Dynamic binding
#include <iostream>
using namespace std;
class Dynamic
{
public:
virtual void output() {
cout << "The base class function is called.\n";
}
};
class A: public Dynamic
{
public:
void output() {
cout << "The derived class function is called.\n";
}
};
int main()
{
Dynamic base;
A derived;
Dynamic *basePtr = &base;
basePtr->output();
basePtr = &derived;
basePtr->output();
return 0;
}
Comments
Leave a comment