Thanks for the replies. the code I posted is a simple version of a large free source C++ libary. Every time there is a similar set of functions, they use the method 1. I myself would have used method 2 for the same obvious reasons. But I am aware there were probably reasons why they used method 2.
here is the extract of the code
Code:
//
// daycounter.hpp (extract)
//
#ifndef quantlib_day_counter_hpp
#define quantlib_day_counter_hpp
#include <ql/time/date.hpp>
#include <ql/errors.hpp>
namespace QuantLib {
class DayCounter {
protected:
class Impl {
public:
virtual ~Impl() {}
virtual std::string name() const = 0;
//! to be overloaded by more complex day counters
virtual Time yearFraction(const Date& d1,
const Date& d2,
const Date& refPeriodStart,
const Date& refPeriodEnd) const = 0;
};
boost::shared_ptr<Impl> impl_;
DayCounter(const boost::shared_ptr<Impl>& impl) : impl_(impl) {}
public:
DayCounter() {}
Time yearFraction(const Date&, const Date&,
const Date& refPeriodStart = Date(),
const Date& refPeriodEnd = Date()) const;
};
inline Time DayCounter::yearFraction(const Date& d1, const Date& d2,
const Date& refPeriodStart, const Date& refPeriodEnd) const {
QL_REQUIRE(impl_, "no implementation provided");
return impl_->yearFraction(d1,d2,refPeriodStart,refPeriodEnd);
}
}
#endif // end of file
//
// actual360.hpp (extract)
//
#ifndef quantlib_actual360_day_counter_h
#define quantlib_actual360_day_counter_h
#include <ql/daycounter.hpp>
namespace QuantLib {
//! Actual/360 day count convention
class Actual360 : public DayCounter {
private:
class Impl : public DayCounter::Impl {
public:
Time yearFraction(const Date& d1,
const Date& d2,
const Date&,
const Date&) const {
return dayCount(d1,d2)/360.0;
}
};
public:
Actual360()
: DayCounter(boost::shared_ptr<DayCounter::Impl>(
new Actual360::Impl)) {}
};
}
#endif // end of file