00:00
00:00
Newgrounds Background Image Theme

ParallaxDraw just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

C++ Main: Constants

2,048 Views | 4 Replies
New Topic Respond to this Topic

C++ Main: Constants 2006-05-14 07:51:41


CONSTANTS TUTORIAL

In this tutorial I will be explaining another way to hold data with C++ other than variables.

There are two ways to hold data in C++... Variables and Constants....

So why then, is there two methods?
------------------------------------------
--------------
The difference between the two is that variables hold data that can be changed at any time at any place in your code, as you guessed constants are constant. That is, you cannot alter the values of a constant than that you can with a variable. The program has no choice what so ever in the value of a constant than it does with a variable.

C++ Has to type of constants; Literal constants and Symbolic contsants.

Literal constants
-------------------------

A literal constant is a value typed directly into your program wherever it may be needed, E.g;

int tutCons = 1;

tutCons is an interger variable, the value contained inside of the variable ( 1 ) is a constant, you cannot assign a value to this constant and it's value cannot be changed.

Symbolic Constants
-------------------------------

A symbolic constant is represent by a name, similar to a variable, whereas unlike a variable, after the constant has been initalized, it's value cannot be changed.

Defining Constants
--------------------------

There are two methods of defining a constant; with the #define statement and the const statement.

Using the #define statement
------------------------------------------
-------

To define a constant using the old fashioned way, you would write the code ;

#define tutConst 1

Note that tutConst is of no particular type (int, char and so on). #define does a simple text substitution. Every time the preprocessor sees the word tutConst, it puts the value 1 in the text.

Because your preprocessor runs before your compiler, your program never sees the constant statement, it just sees the value 1.

Defining constants with the const Statement
------------------------------------------
----------------------------------

The define statement works fine, only, a new and better statement has been made to define constants;

const unsigned short int tutConst = 1

This is longer to type, but in better in so many ways, it makes it easier to limit the constant to one type of value according to it's type registered, it is just like defining a variable but adding a const statement to the code tells the compiler that the variable cannot be changed by the program.

What you have learned.
---------------------------------

In this tutorial, you have learned;

What constants are...

What the difference is between variables and constants...

How to define a constant using the #define statement...

and

How to define a constant using the const statement...

I hope you use this knowledge for future reference...

FlashStation

Note that

Response to C++ Main: Constants 2006-05-14 08:49:10


Good tutorial :)


BBS Signature

Response to C++ Main: Constants 2006-05-14 15:43:04


Not a bat tutorial, but I have a question: What exactly is the point of using a Constant instead of a variable? It seems to me to make more sense to be able to change the value later if needed.
Also you forgot a C++: Main link.

Response to C++ Main: Constants 2006-05-14 15:56:51


At 5/14/06 03:43 PM, Naois wrote: Not a bat tutorial,

hahaha, no, it is a c++ tutorial!

lol sorry

Response to C++ Main: Constants 2006-05-14 16:19:00


At 5/14/06 03:43 PM, Naois wrote: Not a bat tutorial,

...I meant "Not a bad tutorial"