C++

Discussion in 'Scripting & Programming' started by Mof, Jul 10, 2008.

  1. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    This is what you are told when you are a student. In real life you are modelling the solution to a problem when you code.

    I had used 'enum Charge{one_quid, two_quid};' to differentiate from 'enum Charge{NEGATIVE,POSITIVE};' in order to clearly point out the effect of scope as applied to the problem at hand. Why you have chosen to write paragraphs on the topic I don't know. Mof hasn't got there yet!

    Note to 'Mof': When you code you use the features of the language that are necessary. For the code that you are playing around with, the default/standard namespace (std) that you are already using suffices and there is no need to introduce another. Secondly, this is a topic that should not be introduced to you net, not until you have covered functions and parameters, files and linkage, pointers, then general object oriented theory, etc. Without knowledge of these first the above will mean very little to you. :)

    Why can't game devs and business app devs just get along... :rolleyes:
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  2. Mof

    Mof Megabyte Poster

    526
    2
    49
    Im assuming because the globle enum was differant it would need to be declared as a local one ie in the main code to be declared.


    As a foot not Mathmatix is right about the level Im at here is a list of the next few topics in the book.
    Doing arithmetic \\ now modulus has been explained Im ok with this
    assigning Values \\ yep can work this out
    comparing values \\ need to work on this a bit
    assessing logic \\ well I will need help with this
    Examining conditions \\not even looked at it yet, but as with all of them I dont realy know how they work in a program, I suppope im not meant to yet just getting use to writing code.
     
    WIP: C++ and A+
  3. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Not quite. When you commented out

    Code:
    enum Charge{NEGATIVE,POSITIVE};
    
    the compiler assumed that

    Code:
    enum Charge{one_quid, two_quid};
    
    was the definition for the enum 'Charge'. Now when you got to the code

    Code:
        Charge neutral_wire = NEGATIVE;
        Charge live_wire = POSITIVE;
    
    both 'NEGATIVE' and 'POSITIVE' were undefined, so the compiler pointed out this section of code as the problem.

    With C++ it is very important that you take your time and fully understand everything as you go along. There is no hurry. :)

    Looking good. The 'examining conditions' will heavily depend on 'assessing logic', and it will be a longer road than all of the stuff that you've learned thus far. But you're doing well! :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  4. Mof

    Mof Megabyte Poster

    526
    2
    49
    ok that make sence, but was wondering would the compiler know the differance between the two enum charge I know this is going off on a tangent.
     
    WIP: C++ and A+
  5. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    It's to do with the scope of an object that we mentioned earlier.

    Code:
    enum Charge{one_quid, two_quid}; 
    
    Has global scope, but

    Code:
    enum Charge{NEGATIVE,POSITIVE}; 
    
    Has the same scope as

    Code:
        Charge neutral_wire = NEGATIVE;
        Charge live_wire = POSITIVE;
    
    which means that they are more tightly coupled. Basically, the compiler will assume that definitions defined in the same scope are intended to be interdependant over those from an outer scope. If a definition for an object does not exist in local scope, then the level up (the scope that encloses the current scope) will be investigated for a definition, and so forth up the tree of existing scopes. Failing that, the default scope will be searched and if definitions are found they will be used from that level. Only when a matching definition is not found in any scopes would the compiler report that no definition exists for an object. At most, only one definition for an object must exist within the scope in which it was found.
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  6. Mof

    Mof Megabyte Poster

    526
    2
    49
    this is my understanding of Assigning Values:
    for some reason the endl has moved .
    ps have no idear where the smilie face come from smilie face should read 8
     
    WIP: C++ and A+
  7. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Holy smoke! Which book are you learning from again? :blink

    All 'endl' does is cause a carriage return (move the cursor to the start of the line), move it down a line, and flush the steam buffer (the bit of memory containing the text string).

    I would be inclined to write, for your own learning purposes

    Code:
    a+=b
    
    as

    Code:
    a = a + b
    
    Were you aware that they are the same? If not you haven't yet grasped assignments. In layman's terms an assignment simply means to give a variable, constant, or any other object a value. This value may be derived from a literal (like '1' or the character 'n' for example) or maybe from another variable containing a value, as in 'a = b' - or the result of a calculation or logical expression.

    I noticed that when you explain you tend to not use proper terminology, for example

    when you should be learning to use language like

    This last statement conveys the type of 'a' (as in it is an integer), also that the value that 'a' contains may be modified later in the code (because you clearly stated that it was a 'variable' rather than a constant), and that after the assignment has taken place the variable 'a' will contain the integer value 8.

    If you revise your code and comments as per my recommendations above, I'll be conviced that you understand assignments. :)
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  8. Mof

    Mof Megabyte Poster

    526
    2
    49
    yes I did know they were equivalent:
    and quite correct i shoud use proper terminology.

    a*=b is the same as a=(a*b) // integer value a is multiplied by integer value b and assigned to variable a and stored.
    and the endl returns cursor to new line,like the enter key on the keyboard would, well Ill be a monkeys uncle. yes the book does say carrage return but it just didnt regester.

    Mathmatix and Dmarsh26
    I think its time to open a new thread this ones getting a bit long. how about I open one called c++ Assigning values and when your happy I grasped the topic we will open a new thread "Comparing Values".
     
    WIP: C++ and A+
  9. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    I have no problem with that! :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.