Help with Constants and enum constants. Newbie learning C++.
Okay, I am learning C++ using the tutorial on this site and also reading learning c++ in 21 days.
I am up to the part of variables and constants. It says that a variable can be defined and initialised from the beginning like this:
Ulong myAge = 23;
But the book also described it as a constant. Can someone please explain the difference. I am not getting this very well.
Also, about enumerated constants, I am having problems understanding this. Can someone explain this to me as well? I tried to do some code like this:
#include <iostream.h>
typedef unsigned short int Uint;
void main()
{
enum Jerky{Uint j=1, Uint o=2, Uint e=3};
int x;
Jerky joey;
joey=Jerky(3);
cout <<joey;
return 0;
}
I am using Visual C++ 6.0 introductory edition. It gives me the following errors:
C:\JOEY\Program Files\Microsoft Visual Studio\VC98\tests\enumsvare.cpp(7) : error C2061: syntax error : identifier 'j'
C:\JOEY\Program Files\Microsoft Visual Studio\VC98\tests\enumsvare.cpp(10) : error C2146: syntax error : missing ';' before identifier 'joey'
C:\JOEY\Program Files\Microsoft Visual Studio\VC98\tests\enumsvare.cpp(10) : error C2501: 'Jerky' : missing storage-class or type specifiers
C:\JOEY\Program Files\Microsoft Visual Studio\VC98\tests\enumsvare.cpp(10) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
enumsvare.obj - 4 error(s), 0 warning(s)
can someone tell me what I did wrong here?
Thank you for your time in advance.
Joey
Re: Help with Constants and enum constants. Newbie learning C++.
Quote:
Originally posted by UnregJDiPerla
[B]I am up to the part of variables and constants. It says that a variable can be defined and initialised from the beginning like this:
But the book also described it as a constant. Can someone please explain the difference. I am not getting this very well.
Hmmm...maybe the book is referring to the 23 as the constant...I don't have my copy of C++ in 21 days with me, but myAge certainly is not a constant as it is defined.
Quote:
Also, about enumerated constants, I am having problems understanding this. Can someone explain this to me as well?
Enumerations basically set up aliases so that you can use them interchangeably with numbers in the code. In other words, as you set it up, wherever the compiler sees "j", it will treat it as if you said "1". Once you have defined an enumeration, the variables in that enumeration are constant, so you can't increment them, assign to them, etc.