Comments for a script

Hey guys!

This script has appeared in an exam for a module I have in college. I would really appreciate if you could tell me if I explained it properly and also help me understand some lines that I cannot make sense of which I will mark with "this?"

Thank you in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class PayOffD: public PayO //a class is created that inherits from 
{public: 
	PayOffD(double LowerLevel_, double UpperLevel_); this? - I don't know what the underscore is for
	virtual double op()(double S) const; this? - why is there brackets with nothin in them "()" and then the const at the end
	virtual ~PayOffD(){};//the destructor is used to free the memory occupied by the constructor "PayOffD"
private:
	double LowerLevel;//a variable of type double is created
	double UpperLevel;};//same as above

PayOffD::
PayOffD(double LowerLevel_, double UpperLevel):
LowerLevel(LowerLevel_), UpperLevel(UpperLevel_){} this?
double PayOffD::op()(double spot) const //we implement the function operator()
{if (S<=LowerLevel) return 0; //verifies if the spot is lower than or equal than LowerLevel the function returns 0; 
 if (S=>UpperLevel) return 0;//same but in the different direction
return 1;}//if no condition is satisfied the function returns one. 
> I don't know what the underscore is for
name filling.
variables names can have [A-z0-9_]

> why is there brackets with nothin in them "()"
I suppose that should be virtual double operator()(double S) const;
check out operator overloading, there you are declaring parenthesis.
Then you may do
1
2
PayO &foo = //...
foo(3.14);


> and then the const at the end
that particular member function would not modify the calling object, so it can be used with constant objects.

> the destructor is used to free the memory occupied by the constructor "PayOffD"
The destructor does nothing because nothing needs to be done.
¿where in {} are you freeing memory?

1
2
PayOffD::PayOffD(double LowerLevel_, double UpperLevel):
   LowerLevel(LowerLevel_), UpperLevel(UpperLevel_){}

initialization list.
There you may call the constructor of your member variables and your parent class.

> we implement the function operator()
¿? ¿now you know what it is?

:D Thanks, man!
Topic archived. No new replies allowed.