Qustion about (random function) ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Warly girl
    New Member
    • Nov 2007
    • 25

    #1

    Qustion about (random function) ?

    Hi ...
    I want to create Student class that including their id, which is an automatic number issued by the system ..

    do I need to use random method ? or is there another simple way to make the id generated by the system -not input by the user - ?

    i hope that you undarstand what do i mean .....

    thanks !
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by Warly girl
    Hi ...
    I want to create Student class that including their id, which is an automatic number issued by the system ..

    do I need to use random method ? or is there another simple way to make the id generated by the system -not input by the user - ?

    i hope that you undarstand what do i mean .....

    thanks !
    Why you want to use random number? It is better if you increment a number every time new student is created! What is your criteria regarding student id?

    Comment

    • Warly girl
      New Member
      • Nov 2007
      • 25

      #3
      you mean it is enough to Increase the id in the constructer
      Code:
      id++;

      Comment

      • Meetee
        Recognized Expert Contributor
        • Dec 2006
        • 928

        #4
        Originally posted by Warly girl
        you mean it is enough to Increase the id in the constructer
        Code:
        id++;
        Yeah, but that depends on your criteria. Why you want to take random numbers?

        Comment

        • Warly girl
          New Member
          • Nov 2007
          • 25

          #5
          it just an idea because i think that increasing the number is not enough to make that !
          thanks alot ..

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Warly girl
            it just an idea because i think that increasing the number is not enough to make that !
            thanks alot ..
            Have a static member of your class, say 'id' and increment and assign it to your
            student id every time you create one; so in the Student constructor you'd do:

            [code=cpp]
            myId= id++;
            [/code]

            Note that this approach is not thread safe and when you restart your program,
            the static id variable will be reset again.

            kind regards,

            Jos

            Comment

            • Warly girl
              New Member
              • Nov 2007
              • 25

              #7
              Originally posted by JosAH
              Have a static member of your class, say 'id' and increment and assign it to your
              student id every time you create one; so in the Student constructor you'd do:

              [code=cpp]
              myId= id++;
              [/code]

              Note that this approach is not thread safe and when you restart your program,
              the static id variable will be reset again.

              kind regards,

              Jos
              thanks alot , i undarstand your explanation
              so , i will declare "id" as static member
              what about "myId"
              can i declare it as const ?!!

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by Warly girl
                thanks alot , i undarstand your explanation
                so , i will declare "id" as static member
                what about "myId"
                can i declare it as const ?!!
                No; the variable 'myId' should be an ordinary (private?) member variable for all
                the Student objects; it represents the unique id of a student.

                kind regards,

                Jos

                Comment

                • Warly girl
                  New Member
                  • Nov 2007
                  • 25

                  #9
                  it is clear
                  Thank you Jos ..

                  Comment

                  Working...