Is the following a case of static, or dynamic binding? Since I can know, what is the class of object my pointer is pointing to at compile time, my guess is that it is static...
class Base
{
void print() const;
};
class Derived : public Base
{
void print() const;
};
int main()
{
Base base;
Base *basePtr = &base;
basePtr->print();
}
And why we should use dynamic binding over static? I don't understand why we should use virtual functions instead of simply call a function in this way base.print()
(for the example above). This method already "understand" which print()
function call for the right class of the object, without using polymorphism and virtual functions.
Update
// Point class definition represents an x-y coordinate pair.
#ifndef POINT_H
#define POINT_H
class Point {
public:
Point( int = 0, int = 0 ); // default constructor
void setX( int ); // set x in coordinate pair
int getX() const; // return x from coordinate pair
void setY( int ); // set y in coordinate pair
int getY() const; // return y from coordinate pair
virtual void print() const; // output Point object
private:
int x; // x part of coordinate pair
int y; // y part of coordinate pair
}; // end class Point
#endif
// Circle class contains x-y coordinate pair and radius.
#ifndef CIRCLE_H
#define CIRCLE_H
#include "point.h" // Point class definition
class Circle : public Point {
public:
// default constructor
Circle( int = 0, int = 0, double = 0.0 );
void setRadius( double ); // set radius
double getRadius() const; // return radius
double getDiameter() const; // return diameter
double getCircumference() const; // return circumference
double getArea() const; // return area
virtual void print() const; // output Circle object
private:
double radius; // Circle's radius
}; // end class Circle
#endif
// Introducing polymorphism, virtual functions and dynamic
// binding.
#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setprecision;
#include "point.h" // Point class definition
#include "circle.h" // Circle class definition
int main()
{
Point point( 30, 50 );
Point *pointPtr = 0;
Circle circle( 120, 89, 2.7 );
Circle *circlePtr = 0;
// set floating-point numeric formatting
cout << fixed << setprecision( 2 );
// output objects point and circle using static binding
cout << "Invoking print function on point and circle "
<< "\nobjects with static binding "
<< "\n\nPoint: ";
point.print(); // static binding
cout << "\nCircle: ";
circle.print(); // static binding
// output objects point and circle using dynamic binding
cout << "\n\nInvoking print function on point and circle "
<< "\nobjects with dynamic binding";
// aim base-class pointer at base-class object and print
pointPtr = &point;
cout << "\n\nCalling virtual function print with base-class"
<< "\npointer to base-class object"
<< "\ninvokes base-class print function:\n";
pointPtr->print();
// aim derived-class pointer at derived-class
// object and print
circlePtr = &circle;
cout << "\n\nCalling virtual function print with "
<< "\nderived-class pointer to derived-class object "
<< "\ninvokes derived-class print function:\n";
circlePtr->print();
// aim base-class pointer at derived-class object and print
pointPtr = &circle;
cout << "\n\nCalling virtual function print with base-class"
<< "\npointer to derived-class object "
<< "\ninvokes derived-class print function:\n";
pointPtr->print(); // polymorphism: invokes circle's print
cout << endl;
return 0;
} // end main