Answer to Question #263041 in C++ for SSD

Question #263041

C++ program which demonstrates static binding and dynamic binding.


1
Expert's answer
2021-11-08T17:31:42-0500

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;
}









Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS