Results 1 to 4 of 4

Thread: How to declare const variable in C++

  1. #1
    Join Date
    Jun 2009
    Posts
    3,859

    How to declare const variable in C++

    I have a small code where we are calculating the area of a circular ground. As you can assume this involves "pi" value in your code. Everybody knows that the value of "pi" is constant and that is "3.142". But I don't know how I can declare it as constant variable in C++. I know in C, we declare it above the main() but where is it declared in C++? Can you help me how to declare const variable in C++?

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to declare const variable in C++

    It is quiet common that we want some variables in our code that never changes. To declare a variable as constant, we use const keyword to tell compiler that the object or variable is not modifiable. You can directly declare any variable constant directly in your main(). In C++, you never define variable outside any classes. Everything that is defined remain under some class name and it is the main() that is executed first and thus it is better to declare the constant variable over there.

  3. #3
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to declare const variable in C++

    Things have changed in C++. Here you do not constant variable outside, everything is categorized, even your declaration. Here you do not define the constant variable using "#define", but you use "const" keyword. The const keyword allows you to specify whether or not a variable is modifiable. The const variable is declared as follows:

    Code:
    return_type const variable_name = value;
    OR

    Code:
    const return_type variable_name = value;

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to declare const variable in C++

    Always make sure that you declare the constant variable as "public" and not "private" or "protected" because this will result you a compilation error saying the variable is undefined. This happens because a private or protected member is not accessible in other parts of the code whereas public members are freely accessible to anyone without the need of any object.

Similar Threads

  1. What is the Declare Construct in PHP?
    By Rob Dizzle in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 03:51 AM
  2. Differentiate between const and static readonly
    By KALLIYAN in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 12:52 AM
  3. Problem of const variable and its reference
    By KABIRA16 in forum Software Development
    Replies: 3
    Last Post: 04-09-2009, 04:45 PM
  4. Replies: 2
    Last Post: 28-08-2009, 07:51 PM
  5. const Obj problem in C++
    By Archetype in forum Software Development
    Replies: 2
    Last Post: 25-10-2008, 05:58 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
  •  
Page generated in 1,751,855,412.58269 seconds with 16 queries