Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

c++ programming

Options
  • 14-04-2009 12:42pm
    #1
    Registered Users Posts: 2,315 ✭✭✭


    I am learning c++ and was just wondering which should be the way I should declare variables and give it a value. The book i'm learning from uses the first & second method method but have seen a lot of online tutorials using the third & fourth method.
    1
    double dnumber1;
    double dnumber2;
    double dnumber3;
    double daverage;

    dnumber1 = 0.0;
    dnumber2 = 0.0;
    dnumber3 = 0.0;
    daverage = 0.0;



    2
    double dnumber1, dnumber2, dnumber3, daverage;

    dnumber1 = 0.0;
    dnumber2 = 0.0;
    dnumber3 = 0.0;
    daverage = 0.0;



    3
    double dnumber1 = 0.0;
    double dnumber2 = 0.0;
    double dnumber3 = 0.0;
    double dnumber4 = 0.0;
    double daverage = 0.0;


    4
    double dnumber1 = 0.0, dnumber2 = 0.0, dnumber3 = 0.0, daverage = 0.0;


Comments

  • Registered Users Posts: 7,182 ✭✭✭Genghiz Cohen


    I use the third, for claritys sake.

    I'd use use the second for very similar variables if i were going to initalise them later.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    There's no right way, just down to personal choice. Don't get caught up on things like this :)


  • Registered Users Posts: 2,534 ✭✭✭FruitLover


    Edit: oops, didn't check that properly! :o

    Here's another style:

    double dnumber1, dnumber2, dnumber3, daverage;

    dnumber1 = dnumber2 = dnumber3 = daverage = 0.0;


  • Registered Users Posts: 981 ✭✭✭fasty


    So what do dnumber1, dnumber2 and dnumber3 get initialised to in that example?


  • Registered Users Posts: 39 Talon.ie


    double dnumber1 = 0.0;
    double dnumber2 = 0.0;
    double dnumber3 = 0.0;
    double dnumber4 = 0.0;
    double daverage = 0.0;

    It signals the intent of the author clearly, isn't overly verbose and isn't subject to different interpretations by other people reading / maintaining the code.


  • Advertisement
  • Registered Users Posts: 3,945 ✭✭✭Anima


    I like the below style because it saves you cluttering up the file with unessesary repetitous data type declarations and its easier to just see just the variable names.

    [PHP]
    double dNumber1 = 0.0,
    dNumber2 = 0.0,
    dNumber3 = 0.0,
    dNumber4 = 0.0,
    dAverage = 0.0;
    [/PHP]


  • Registered Users Posts: 1,922 ✭✭✭fergalr


    Be consistent. Use whichever approach is normally used in the codebase in which your working.

    If I was starting from scratch, I'd probably go with number 3, for the same reasons talon.ie said.

    Things are very clear, and there's no risk of someone later putting in code (eg function call) between declaration and assignment.


    The only thing I don't like is the way it's called 'dnumber1' - I would strongly come down in favour of calling it number1, or even better something that's descriptive of what the number is doing - I don't have the context, but as it's probably not just a number someone decided to make for the hell of it, I'd suggest you try and put the purpose in the name, eg, sampleHeight1, sampleHeight2, sampleHeight3, averageHeight.


  • Closed Accounts Posts: 448 ✭✭ve


    I know some people say you shouldn't get to bogged down with such minor details, and for the most part I would agree, but I think it's important to understand the basics, especially if this is your first programming language which I'm guessing it is.

    With the examples you posted above, yes by the time you get to the end of each one you've essentially ended up at the same point. However it's important to know that
    double dnumber1;
    
    is a declaration, and this is the point in your code where you are merely setting aside memory (or your program is asking the operating system) to allocate enough memory to hold a double. The size of a double will vary from platform to platform, but for the most part you wouldn't need to worry about something like that.
    dnumber1 = 0.0;
    
    is an initialization, and this is the point where you are giving a previously declared variable, a set aside place in memory allocated for use by your program a new value.
    double dnumber1 = 0.0;
    
    This is both a declaration and an initialization done in one line. So when it comes to understanding the impact your code will have on runtime requirements its good to acknowledge the what happens when.

    I would highly encourage you to take this on board at this stage in your programming career, and keep in mind how you "set aside" memory in your code. As you progress you will more likely find much more efficient ways to do things and not take up as much memory in the process as you would have at an earlier stage.

    All the best ;)


  • Registered Users Posts: 2,315 ✭✭✭deceit


    Thanks everyone for your replies and sorry for such a later reply. In relation to the last post I'm taking my time learning it as it is my first language and am not moving past topics until I have a 100% full understanding of them.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Anima wrote: »
    I like the below style because it saves you cluttering up the file with unessesary repetitous...

    I like that one best.

    For job security can you do something like this? :D

    double dnumber1 = 1.0, dnumber2 = 0.0, dnumber3 = 3.0, daverage = 4.0;
    dnumber1 = dnumber3 = daverage = dnumber2;


  • Advertisement
  • Registered Users Posts: 2,315 ✭✭✭deceit


    Taught it better to put another question in here rather than create another thread. Its about signed and unsigned integers.


    Both of these are coming from a book i'm learning from:
    First it states "A signed integer can hold positive and negative value. By default integers are signed.". If this is right how can the comment in the code below be correct also: "int j; // An unsigned short integer".

    #include <iostream>
    using namespace std;

    int main()
    {
    short int i; // A signed short integer short unsigned
    int j; // An unsigned short integer
    j = 60000;
    i = j;
    cout << i << " " << j << endl;


    system("pause");

    return 0;
    }


  • Registered Users Posts: 32,417 ✭✭✭✭watty


    If you EVER have to use Modula-2 or Visual Basic...
    Modula-2 version of this
    1
    double dnumber1;
    double dnumber2;
    double dnumber3;
    double daverage;

    These are all different types
    In M2
    double dnumber1, dnumber2, dnumber3, daverage;
    These are all the SAME type


    IN VB
    double dnumber1, dnumber2, dnumber3, daverage
    (assuming a VB syntax) the first declared variable would be what it says it is. The rest would be variant type

    SO
    I would use
    double dnumber1 = 0.0;
    double dnumber2 = 0.0;
    double dnumber3 = 0.0;
    double dnumber4 = 0.0;
    double daverage = 0.0;

    UNLESS it is something more complex, in which case I declare a type.

    Not all Doubles or Ints are the Same kind of thing (M2 is really good on that concept, C really poor, Java / C++ better and C# maybe better)

    So it it good to
    Group things that may be same Language type today, but not same thing, so one variable might change type. Do the declares in groups and always explicity declare each variable completely.

    If you compilier/language lets you initalise in the declaration, always do it as a common cause of grief.

    So my preferred is

    //The coefficents
    double number1 = 0.0;
    double number2 = 0.0;
    double number3 = 0.0;
    double number4 = 0.0;
    double average = 0.0;

    //thingy that happen to use Doubles but are no-way related to the coefficents
    double thing1 = 0.0;
    double thing2 = 0.0;
    double thing3 = 0.0;

    Meaningful names and no hungarian. Hungarian is for stupid un type safe C with stupid compiler. You might change your mind what type is used to implement. Meaningful name is MORE important than seeing what type you chose.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    First it states "A signed integer can hold positive and negative value. By default integers are signed.". If this is right how can the comment in the code below be correct also: "int j; // An unsigned short integer".

    The comment is a misprint. Its just a normal signed integer, "int j". Nothing is unsigned unless you see the "unsigned" keyword.


  • Registered Users Posts: 2,534 ✭✭✭FruitLover


    deceit wrote: »
    "A signed integer can hold positive and negative value. By default integers are signed.". If this is right how can the comment in the code below be correct also: "int j; // An unsigned short integer".

    It's laid out a bit funny - according to the next part of your post it's:
    deceit wrote: »
    short unsigned
    int j; // An unsigned short integer

    Fix that up a bit and you get:

    short unsigned int j; // An unsigned short integer


  • Registered Users Posts: 2,315 ✭✭✭deceit


    FruitLover wrote: »
    It's laid out a bit funny - according to the next part of your post it's:



    Fix that up a bit and you get:

    short unsigned int j; // An unsigned short integer

    Thanks for everyones help. That makes sense now thanks :). Its from "C++ A Beginner’s Guide by Herbert Schildt" and I think theres quite a few miss prints now am going back over it and i'm only on the second chapter. Could anyone recommend me a better book that I could pick up?


  • Closed Accounts Posts: 448 ✭✭ve


    This may be of use to you as a free learning resource....CProgramming.com

    All the best. Fair play btw for taking the time to learn things properly instead of rushing ahead. C/C++ are great languages to start off with.


  • Registered Users Posts: 2,315 ✭✭✭deceit


    ve wrote: »
    This may be of use to you as a free learning resource....CProgramming.com

    All the best. Fair play btw for taking the time to learn things properly instead of rushing ahead. C/C++ are great languages to start off with.

    Thanks for the link. Seems to be tonnes of info on the site. Will try get the site downloaded onto a disk as alot of the time i'm learning is when get a free moment in work.


  • Closed Accounts Posts: 5,857 ✭✭✭professore


    Sounds like you'll make a good programmer ;)

    Good choice of language too - it'll give you a great foundation.


  • Registered Users Posts: 2,534 ✭✭✭FruitLover


    It sounds like you might find assembly language interesting. I'd recommend having a look at High-level assembly if you have some free time. It's not 'true' assembly, but it'll teach you a lot about how computer processors and memory work at a low level (which is really all people learn assembly for at all these days).


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    FruitLover wrote: »
    It sounds like you might find assembly language interesting. I'd recommend having a look at High-level assembly if you have some free time. It's not 'true' assembly, but it'll teach you a lot about how computer processors and memory work at a low level (which is really all people learn assembly for at all these days).
    +1, nice book.


  • Advertisement
  • Registered Users Posts: 2,315 ✭✭✭deceit


    FruitLover wrote: »
    It sounds like you might find assembly language interesting. I'd recommend having a look at High-level assembly if you have some free time. It's not 'true' assembly, but it'll teach you a lot about how computer processors and memory work at a low level (which is really all people learn assembly for at all these days).
    Just got a chance to look at the website. Looks really interesting will definiately look into it, but think is best I fully understand c++ first which am really enjoying at the moment :)


Advertisement