+ Reply to Thread
Results 1 to 6 of 6

Thread: constants (const) type

  1. #1
    Learning Programmer genux will become famous soon enough genux's Avatar
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    57

    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. #2
    Programming God outsid3r has a spectacular aura about outsid3r has a spectacular aura about outsid3r's Avatar
    Join Date
    Jul 2008
    Location
    Portugal
    Posts
    564

    Re: constants (const) type

    Nice. rep+

  3. #3
    Programming God thegamemaker has a spectacular aura about thegamemaker has a spectacular aura about thegamemaker's Avatar
    Join Date
    Nov 2009
    Location
    At the end of the rainbow.
    Age
    14
    Posts
    619
    Blog Entries
    1

    Re: constants (const) type

    Nice first tut. +rep
    My Web Site

  4. #4
    Programming Expert exicute is on a distinguished road exicute's Avatar
    Join Date
    Jan 2010
    Location
    Ohio
    Age
    18
    Posts
    411

    Re: constants (const) type

    Great tutorial, very neat and organized. rep+

    My Tutorials|Build A Computer|Cat 5E|

  5. #5
    Learning Programmer genux will become famous soon enough genux's Avatar
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    57

    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

  6. #6
    Programming Expert whitey6993 has a spectacular aura about whitey6993 has a spectacular aura about whitey6993's Avatar
    Join Date
    Dec 2008
    Location
    In front of my Computer
    Posts
    408

    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. Help to create NotePad in c++
    By mindblaster in forum C and C++
    Replies: 21
    Last Post: 08-20-2009, 01:30 PM
  2. How to add My program to Startup ?
    By kresh7 in forum Visual Basic Programming
    Replies: 52
    Last Post: 08-18-2009, 08:29 PM
  3. Replies: 0
    Last Post: 04-27-2009, 01:40 PM
  4. [ C++ ] Drawing Program.
    By Max_Payne in forum C and C++
    Replies: 6
    Last Post: 01-23-2008, 09:18 PM
  5. Help with SendMessage API
    By MrNobody in forum Visual Basic Programming
    Replies: 8
    Last Post: 11-22-2006, 12:01 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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