Thread: Virtual function

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    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
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    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
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Is this in the base class or a derived class? Can you show us something compilable that demonstrates the problem?

    -Prelude
    My best code is written with the delete key.

  4. #4
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    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.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Stroustrup Talk on C++0x
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 07-20-2007, 02:02 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM