Jump to content

c++ constant questions

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
i just finished a simple code and i started wondering about the constant expression like double, literal, int, and ect.

so here is the code
// This is a simple c++ program

// Just playing around with my variables

// Practice your c++ code son!


# include <iostream>

# include <string>

using namespace std;


# define NEWLINE '\n'

# define PI 3.14159


int main ()

    {

         // first we declare variables, assigning a type to each

         int a, b;

         double result;

         double circum;

         double rc;

         

         // next, we show what is each variable

         a = 2;

         b = 3;

         result = (a + b)*2;

         circum = PI * result;

         

         // next, we have to assign another process for 'rc' caclcuation

         // now we assign a value with b values and then re-create the value again

         a = b;

         b = 4;

         rc = a + b * a;

         

         // now, let us completeit, we will echo the results

         cout << result;

         cout << NEWLINE;

         cout << circum;

         cout << NEWLINE;

         cout << rc;

         cout << NEWLINE;

         cin.get(); // asks for a pasue

         

         // let's end the program now. if everything works out, it will show in console

         return 0;

    }

    


Constants
i always have problems with the concepts of constants, even when i learned about php....

let say in the code above, if i assign result and circum as double
how can i see the difference if i assign them with int instead?

in what cases, will i see the difference, and in what condition, will i use these constant?

thanks for any reply any help

#2
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
The C++ 'const' Declaration: Why & How

this site may help you, it explains it very well
Posted Image

#3
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
thanks, but i guess it's still not easy to understand by a newbie like me
the pointer thing is already a difficult thing to understand
i guess i should first concentrate on the basic first
the constant is okay now, but not from the article you gave me....

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
In C++, a const variable is a variable that must be initialized when it is declared, and cannot have its value reassigned. In C, people usually do something like
#define PI 3.1415926
to declare constant values. Unfortunately, that isn't type-safe and can cause problems with C++. To get around that, C++ allows the following:
const double PI=3.1415926;

This has the advantage of allowing type-checking when passing parameters and doesn't risk having conflicts between different definitions of PI in included files.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
In C++, a const variable is a variable that must be initialized when it is declared, and cannot have its value reassigned. In C, people usually do something like

#define PI 3.1415926

to declare constant values. Unfortunately, that isn't type-safe and can cause problems with C++. To get around that, C++ allows the following:

const double PI=3.1415926;


This has the advantage of allowing type-checking when passing parameters and doesn't risk having conflicts between different definitions of PI in included files.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
i see, so instead you would recommended to use const double pi = xxxx; method, not define method?

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Yes. One of the design goals of C++ was to make #define almost completely unnecessary. It's preserved for backwards compatibility with C, but is not recommended.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
Thanks, I am re-reading all the basics things now
trying to understand the basics, and keep practicing the basics
i guess i need to test different things around, and see how they will work
i am high school senior, and thanks for all the great helps, codecall forum
you know i wish one day i can be someone like you guys, helping the community out ^^

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
All you have to know to help out is one more thing than the person asking the question :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog