+ Reply to Thread
Results 1 to 6 of 6

Thread: constants (const) type

  1. #1
    genux's Avatar
    genux is offline Learning Programmer
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    79
    Rep Power
    8

    constants (const) type

    Here is my first tutorial, not sure how good it will be for this site.

    Const (constants) type restrict the usage of that variable so that it value for example cannot be altered which is good for values of pi, you do not want that changed from 3.142 to 5, because then your circle will just not look the same!!.

    There are a couple of places to put a const type in a variable declaration.
    Code:
    int normalInt = 10;
    const int constInt = 30;
    the first normalInt value is 10 and can be altered but the const int cannot not.

    For pointers it is more fun,
    Code:
    const int * pConst = &constInt;
    Here the pConst is pointing to a const int value, but its pointing to place (the memory location of the variable) can change.

    Code:
    int * const pConst = &normalInt;
    Slightly different the value that is pointed to can change, since it is a normal int value, but the value within the pConst cannot e.g. the memory location to where it is pointing, so it will always point to the same variable.

    Code:
    const int * const pConstConst = &constInt;
    Here is more fun, it is a const int (cannot change the value) const pointer (cannot change where pointing to) pointer!!.

    You can also place const within the functions definition

    Code:
    const int returnValue(const int value1) const;
    Here in order
    1. Cannot change the return'd value, unless you assign it to a variable.
    2. Cannot change the parameters value.
    3. Cannot change the "this" class variables.

    For example for the first one,
    Code:
    int returnedValue = returnValue(10);
    The returnedValue can be changed but if you did
    Code:
    returnValue(10)++;
    would produce an error because you are trying to increment a const int.

    The second one, the parameter const is similar in that you cannot alter the value passed
    Code:
    const int returnValue(const int value1) 
    {
        value1= value1+10;
        return value1;
    }
    Cannot do this, because value1 cannot be altered.

    The last is more fun, where you cannot alter the values within "this" class. For example
    Code:
    class AClass
    {
        private :
          int var1;
        public:
          AClass() { var1 = 10};
          int returnValue(int value1) const;
    };
    
    int AClass::returnValue(int value1) const
    {
       // cannot do this !!. var1 is part of "this class"
        var1 = var + 1;
        return var1;
    }
    I do really like to have any feedback regarding any tutorial, just reply or PM me.. glad to help, better to share knowledge.
    Code:
    int coffeePerDay = 10; // need to cut down!!!
    Codingfriends

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    outsid3r's Avatar
    outsid3r is offline Programming God
    Join Date
    Jul 2008
    Posts
    621
    Rep Power
    18

    Re: constants (const) type

    Nice. rep+

  4. #3
    Join Date
    Nov 2009
    Location
    I seem to be lost...
    Posts
    1,566
    Blog Entries
    1
    Rep Power
    22

    Re: constants (const) type

    Nice first tut. +rep
    Knowledge: Intermediate C#, Beginner AS3, HTML, CSS, Binary, Hex, Octal.
    Science is only an educated theory, which we cannot disprove.

  5. #4
    exicute's Avatar
    exicute is offline Programming Expert
    Join Date
    Jan 2010
    Location
    Ohio
    Posts
    398
    Rep Power
    10

    Re: constants (const) type

    Great tutorial, very neat and organized. rep+

    My Tutorials|Build A Computer|Cat 5E|

  6. #5
    genux's Avatar
    genux is offline Learning Programmer
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    79
    Rep Power
    8

    Re: constants (const) type

    Cheers

    going to do one on dynamic, const, interpret and static casting.
    Code:
    int coffeePerDay = 10; // need to cut down!!!
    Codingfriends

  7. #6
    whitey6993's Avatar
    whitey6993 is offline Programming Expert
    Join Date
    Dec 2008
    Posts
    435
    Rep Power
    15

    Re: constants (const) type

    Good first tutorial. Looking forward to your casting tut. +rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Scheme Functions and Constants
    By chili5 in forum Scheme Tutorials
    Replies: 4
    Last Post: 08-30-2011, 08:28 AM
  2. Constants - what's the point?
    By justanothernoob in forum C# Programming
    Replies: 9
    Last Post: 04-09-2011, 01:29 AM
  3. 'Could not convert variant of type (String) into type (Double)'
    By Ilikestring in forum Pascal and Delphi
    Replies: 3
    Last Post: 03-26-2010, 05:35 PM
  4. Constants
    By clookid in forum PHP Tutorials
    Replies: 1
    Last Post: 01-11-2007, 06:39 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts