Constructors + Derived class

This is a discussion on Constructors + Derived class within the C++ Programming forums, part of the General Programming Boards category; Im having a hard time figuring out how you call a base class constructor from the dervied class. Code: //Setting ...

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

    Constructors + Derived class

    Im having a hard time figuring out how you call a base class constructor from the dervied class.

    Code:
    //Setting instance variables in base class constructor
    player(int num, char *player_name)
    {
                number = num;
    	name = new char[20];
                strcpy(name, player_name);
    }
    
    //Constructor in derived class
    hplayer(int number, double salary = 500000.00, char *name = "Bill");
    
    //scoped into class
    hplayer::hplayer(int number, double salary, char *name):call here()
    {
    	//set instance variable
                sal = salary
    }
    The problem is I dont know how to call the base class constructor

    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
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    hplayer::hplayer(int number, double salary, char *name) : player(number, name) {
    	//set instance variable
                sal = salary
    }

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Im only setting one instance variable in the derived class, a nwe instance variable that I added in the dervied class. It gives me warnings, of not using the other parameters in the the derived class construcotr, and when I print those out, I get wierd characters. Why?
    -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

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    //Setting instance variables in base class constructor
    player(int num, const string &player_name) : number(num), name(player_name)
    { };
    
    //Constructor in derived class
    hplayer(int number, double salary = 500000.00, const string &name = "Bill"); // I really have no idea if this const is legal...
    //scoped into class
    hplayer::hplayer(int number, double salary, const string &name) : player(number, name), sal(salary) 
    { } ;
    Try that...
    You should prefix member variables with an m_ or postfix a _ to avoid ambiguity, such the above code.
    If you're wondering why I've changed your code from assignment to initilization lists, read this: http://www.parashift.com/c++-faq-lit....html#faq-10.6 I changed your char * to string refrences, as you need to allocate memory for your char *, then use strcpy to copy the characters... this can't be used in an initilization list. I think this is a better method, but you can change it back if you please.
    Last edited by Eibro; 11-10-2002 at 03:56 PM.

  5. #5
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    You have made the name a const, what if you have a char array and allocate room for it, could you do that in the code, and use the : number(num) next to the constructor still?
    -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

  6. #6
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Sorry; I was editing the above post when you posted. I changed more stuff, look at it again. I'm currently testing the legality of that const identifier.

    Edit: Yes, it's legal... my compiler allowed it anyway.
    Last edited by Eibro; 11-10-2002 at 04:02 PM.

  7. #7
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    I ended up doing the allocation of the char inside the constructor and did the :number(num) next to it, and I am getting the correct results printed.

    Thanks for the help.
    -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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. derived class
    By BKurosawa in forum C++ Programming
    Replies: 9
    Last Post: 08-09-2007, 02:18 PM
  2. C++ Newbie - Derived Class Issue
    By Gipionocheiyort in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2007, 12:20 AM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  5. templatizing a derived class
    By the Wookie in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2003, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21