0

I new to C++ and am learning it for financial applications. In the book that I am reading (C++ for Financial Mathematics), there is a section in which the same function is initialised as public and private within the same class:

class BlackScholesModel {
    public: ...other members of BlackScholesModel...
    std::vector<double> generateRiskNeutralPricePath( 
    double toDate,
    int nSteps)const;
    }; 

and now it introduces the function generateRiskNeutralPricePath as a private function (with the additional drift parameter):

class BlackScholesModel {
    ...other members of BlackScholesModel...
    std::vector<double> generateRiskNeutralPricePath(
    double toDate,
    int nSteps,
    double drift) const;
    }

So, I wanted to know, doesn't the fact that I am introducing the same function twice confuse the compiler? I understand that when I declare the functions, it will be able to differentiate between the two functions (from their parameters). But, is that even a good practice to do? I'd like to think not.

  • 3
    I suggest you [get a few generic C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read first, and learn the language. If you then read about finance and math separately, you could afterward pool your knowledge. – Some programmer dude Oct 24 '18 at 19:00
  • 3
    If the parameters are different, you have two different functions. –  Oct 24 '18 at 19:01
  • 1
    Note the number of parameters or their types have to be different. Names aren't good enough `void X(int a, int b)` and `void X(int c, int d)` look the same to the compiler. – user4581301 Oct 24 '18 at 19:15
  • 2
    The key term here is “function overloading”. Those are two different functions, even though they have the same name. – Pete Becker Oct 24 '18 at 20:06
  • @Someprogrammerdude Thank you for telling me this. I followed your recommendation and picked up C++ Primer from the link you mentioned. I am glad I followed your advice. I am just 2 chapters into the book and I feel I have learnt more than the 8 chapters I studied in the other book. –  Nov 07 '18 at 12:27
  • 1
    Good books can make a big difference from bad books. I'm glad it helped. Good luck! :) – Some programmer dude Nov 07 '18 at 12:31

2 Answers2

0

An overloaded function in C++ while it might have the same name are actually not the same function. So while I think it's an odd construct, the compiler shouldn't complain.

Eric Yang
  • 2,678
  • 1
  • 12
  • 18
0

These two functions have different function signatures.

std::vector<double> generateRiskNeutralPricePath(double toDate, int nSteps);
std::vector<double> generateRiskNeutralPricePath(double toDate, int nSteps, double drift);

C++ will treat these as if they were two different functions. They could both be public or both be private, and the compiler will still accept it. You could treat it like

std::vector<double> generateRiskNeutralPricePath(double toDate, int nSteps);
std::vector<double> generateRiskNeutralPricePathWithDrift(double toDate, int nSteps, double drift);

Is it good practice? If done correctly, yes. Overloaded functions will generally have almost-identical functionalities. In this case, the additional drift probably just adjusts the return value slightly, while the overall functionality is the same.

A common example is the square root function.

     double sqrt (double x);
      float sqrt (float x);
long double sqrt (long double x);
     double sqrt (T x);           // additional overloads for integral types

There are specific overloading rules for what is and isn't allowed, exactly to prevent compilers from being confused. In this case, these functions have different number of arguments, so their function signatures are different.

(For those moving from C, note that this is one of the crucial differences between C and C++, as C doesn't have function overloads.)

Bonus: For an example of when the compiler does get confused
How do I specify a pointer to an overloaded function?

ChilliDoughnuts
  • 367
  • 2
  • 13