Class constructor (c++ newb)

This is a discussion on Class constructor (c++ newb) within the C++ Programming forums, part of the General Programming Boards category; Looking at the tutorial on classes, at the constructor part: Following part in public class: Computer(); //Constructor Member function: Computer::Computer() ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    26

    Class constructor (c++ newb)

    Looking at the tutorial on classes, at the constructor part:

    Following part in public class:
    Computer();
    //Constructor
    Member function:
    Computer::Computer()
    { //Constructors can accept arguments, but this one does not
    processorspeed = 0;
    //Initializes it to zero
    }
    My problem lies that, say I have 5 variables in the private section of the class. I need to initialize those values to 0 if there isn't already anything in there, but do I have to make 6 different member functions, accomodating the fact that they can either send in no variable, one, two, three, etc, or is there a way to make one and have it determine if anything has already been initialized.... I don't know if this makes much sense reading it, but help is appreciated.

  2. #2
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    if there isn't already anything in there
    do you mean, if the variables havn't been initialized?

    that they can either send in no variable, one, two, three, etc
    sorry, i have no clue what you mean here..

    but..from what i can gather from you question, your asking if there is a way not to initalize the private data to 0 if it already has a value. Well, remember that the constructor runs automatically whenever their is an instance of an object, so when an object is created, there would be no point in checking if the data already contains a value, because the constructor is the first function that executes. Once it executes it doesn't run again, so, your safe from the constructor accidently overwriting data.
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    26
    Er.. heres what I'm trying to mean in code by the 'sending in one, two three, etc'

    Code:
    Payroll employee1(5,4,3,2,1); Payroll employee2(5,4,3,2); Payroll employee3 (5,4,3)

    And keep continuing that until you get down to where its not sending a variable. Do I need to make six member functions to cover the amount of combinations which can be sent in?

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Code:
    class Computer
    {
    public:
    	Computer(int ps = 0, int mem = 0, int ds = 0) : 
    		processorspeed(ps), memory(mem), drivespace(ds)
    	{ }
    private:
    	int processorspeed;
    	int memory;
    	int drivespace;
    };
    This line:
    Code:
     Computer(int ps = 0, int mem = 0, int ds = 0)
    is what you should focus on to have a constructor that takes different values. Just like any other function, the parameters with " = 0" are optional, meaning they will be 0 if the user doesn't specify them. The downside is that if the user wants to indicate memory but not processorspeed, they must add the 0 for processor speed also (like in this example):
    Code:
    Computer myComputer(0, 128);
    which creates a computer with memory at 128 and processorspeed and drivespace at 0. Note that the default doesn't have to be 0, I just chose that.
    Last edited by jlou; 04-09-2004 at 01:04 PM.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    26
    Thats exactly what I needed to know

    *Edit*

    Except now I just realized I'm lost when you did this:

    Code:
    processorspeed(ps), memory(0), drivespace(ds)
    Last edited by OttoDestruct; 04-08-2004 at 09:18 PM.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    That's called an initializer list. You don't have to use it if you don't want to. In general, before the body of your constructor (the stuff inside the braces) is run, all the member variables of the class are created. Then, inside the body of your constructor you set them to initial values that you want. For integers, that is not really a big deal. But when you get into more advanced programming, you will have other classes as members, and sometimes it will be inefficient to have those objects be created once, and then have them set their initial value later. By putting these things into the initializer list, they get constructed with those initial values immediately.

    So in general, when you feel comfortable with it, it is a good idea to use the initializer list to set the initial values of your member variables. The syntax is like that of a constructor, which is why there are parentheses with the initial value inside.

    [Edit] - Ohh, and i just realized I made a typo! It should be:
    Code:
    processorspeed(ps), memory(mem), drivespace(ds)
    Oops!

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    26
    I'm scared .... that all made sense to me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 02:09 PM
  3. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  4. New silly newb questoin. Returning vector from a class
    By Gatt9 in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2005, 08:50 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

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