-
Virtual function
I have a virtual function:
virtual void set_num(double number = 56.00);
I also have a function get_num().
In the virtual function, if a value is greater than 100, I have to set it to 100.
Code:
set_num(double number)
{
if (number > 100.0)
{
number1 = 100.0; // number1 is a private instance method
}
}
If I dont specify a value, I dont get the default 56.00 printed, and if I put a number over 100, it doesnt output 100.
Thanks
-
I fixed one problem, but another one came up.
I have a default value of 56.00, and when I call that function with no parameter, it doesnt set the instance varialbe number1 to 56.00. How can I accomplish that?
Thanks
-
Is this in the base class or a derived class? Can you show us something compilable that demonstrates the problem?
-Prelude
-
This is in the base class.
I have a function
virtual void set_num(double number = 56.00);
I have an instance variable number1;
A funvtion get_num(), returns the instacne variable number1.
So, if I use set_num(), with no argument, wanting the 56 defaulted, it wont set numbe1 to 56.00.
-
Do you have another set_num() declaration (one with no parameters). Although your compile would warn you of something like that I can't think of any other reason why you are having trouble. Also, make sure you are putting the default in the actual declaration of the class i.e.
Code:
class myclass {
void set_num(double=56.0);
};
If you only put it where you are defining the function then the compiler won't know what to do with the default value.