Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2005 Curt Hill Constants in C++ Why and How.

Similar presentations


Presentation on theme: "Copyright © 2005 Curt Hill Constants in C++ Why and How."— Presentation transcript:

1 Copyright © 2005 Curt Hill Constants in C++ Why and How

2 Copyright © 2005 Curt Hill Introduction There are three kinds of constants in C++ –Literals –Define named constants –Variables with const prefix We will see the whys and wherefores now

3 Copyright © 2005 Curt Hill Literal constants We have seen the following fours kinds of constants: –-45 (int) –2.3 (float) –1E4 (float) –‘A’ (char) –“Hi” (string) Normally, merely inspecting the constant will tell you its type, as well as its value

4 Copyright © 2005 Curt Hill Literals Continued What do we do to set the constant to something other than a standard int or float? We can use a suffix to tell it the type The L or l indicates that it is long The U or u indicates unsigned Floating point constants are by default double Suffix of F or f makes it a float, L a long double You may also use binary, octal or hexadecimal constants but we do not need to do that here

5 Copyright © 2005 Curt Hill Literal Problems However, literal constants have the following problems that sometimes need to be dealt with We have to retype them each time –This is a problem with high precision constants like PI –Opportunities for typing errors –Just remembering what the 14 digit value is We have to remember what they represent –Small integer constants tend to get confused with one another

6 Copyright © 2005 Curt Hill Example: Suppose that I am a manager and I have 5 people who work 5 days a week and 8 hours a day Thus the number of manhours is: workhours = 5 * 5 * 8; Now we will try something new, four ten hour days per week I use my text editor and change all 5s to 4s and all 8s to 10s We now get workhours = 4 * 4 * 10; The problem with the above expression is that the 5 and 8 have no inherent meaning

7 Copyright © 2005 Curt Hill Alternative It would be better to say: int people = 5, dayperweek = 5, hoursperday = 8;... workhours = people * dayperweek * hoursperday Unfortunately that leads to a problem: –An inadvertent assignment to one of the variables The solution is that we would like to be able to name constants, just like we name variables but without possibility of assignment

8 Copyright © 2005 Curt Hill The define preprocessor directive This is the only C way had to do this Constants are set up using the define directive Format: #define name value Blanks separate the items The value is usually a constant of some sort The name has the format of a C variable

9 Copyright © 2005 Curt Hill Example Some constants #define DAYSPERWEEK 5 #define HOURSPERDAY 8 #define EMPLOYEES 5 hours = DAYSPERWEEK * HOURSPERDAY * EMPLOYEES;

10 Copyright © 2005 Curt Hill Discussion The preprocessor searches the rest of the text and replaces every occurrence of NAME with VALUE The name is not a variable, though it may look like one Examples: #define PI 3.141592653589793 x = PI * r * r; Simply does macro text substitution so that statement becomes: x = 3.141592653589793 * r * r;

11 Copyright © 2005 Curt Hill More Discussion Preprocessor does not know C/C++ It has its own syntax No = or ; is needed So #define PI = 19/6 does not work, it becomes x = = 19/6 * r * r; Even without = the 19/6 would be evaluated as an integer

12 Copyright © 2005 Curt Hill More Discussion String constants are not changed such as: “ The value of PI is ” –This would remain the same #define must occur before use, but customarily comes at front of program Used to parameterize a program There is a convention that constants should be in all caps so a constant is obvious in the text

13 Copyright © 2005 Curt Hill #define problems A problem with the #define statement is that no type is associated with the value All that occurs is macro processing, where one string is found and replaced with another C++ accepts #defined constants but adds a new way to define a constant

14 Copyright © 2005 Curt Hill Const Keyword A feature that is new to C++ is the const qualifier: –const float pi = 3.14159; Any attempt to assign to this will result in an error The float type determines the precision used

15 Copyright © 2005 Curt Hill Discussion The const is a keyword that prefixes the declaration This may be used anywhere a variable may be declared The declaration must have an initialization


Download ppt "Copyright © 2005 Curt Hill Constants in C++ Why and How."

Similar presentations


Ads by Google