Menu

Explanation of Syntax for Class Functions

2008-08-16
2012-09-26
  • Chris Hammond

    Chris Hammond - 2008-08-16

    In the header file "PayOff1.h"(pasted at the bottom of the message) the PayOff class is created. In the file "PayOff1.cpp," the PayOff function is actually defined.

    I understand that "PayOff::PayOff(...)" starts the definition, but why is there a colon after that instead of a parenthesis? I have read most of O'REilly's "Practical C++ Programming," and I cannot seem to find an explanation for what is going on here.

    PayOff::PayOff(double Strike_, OptionType TheOptionsType_)
    :
    Strike(Strike_), TheOptionsType(TheOptionsType_)
    {
    }

    double PayOff::operator ()(double spot) const
    {
    switch (TheOptionsType)
    {
    case call :
    return max(spot-Strike,0.0);

    case put:
        return max(Strike-spot,0.0);
    
    default: 
        throw("unknown option type found.");
    
    }
    

    }

    // PayOff1.h

    ifndef PAYOFF_H

    define PAYOFF_H

    class PayOff
    {

    public:

    enum  OptionType {call, put};
    PayOff(double Strike_, OptionType TheOptionsType_);
    double operator()(double Spot) const;
    

    private:

    double Strike;
    OptionType TheOptionsType;
    

    };

    endif

     
    • cpns

      cpns - 2008-08-16

      It is an example of a "member initialization list". Look that or just "initializer list" or similar. If you book uses English spelling it will be an "initialisation list".

      Alternatively it the following links explain it:

      http://msdn.microsoft.com/en-us/magazine/cc301399.aspx
      http://www.cprogramming.com/tutorial/initialization-lists-c++.html

      You can google for many more examples now you know what it is called.

       
    • Chris Hammond

      Chris Hammond - 2008-08-16

      Thanks again. That was very helpful.

       
      • cpns

        cpns - 2008-08-16

        ... despite the fact that I typed it in something only approximating English. Mutt be tired.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.