Exposure C Chapter V Variables and Constants The
Exposure C++ Chapter V Variables and Constants
The Limitation of Constants // PROG 0501. CPP // This program demonstrates using four constants. #include <iostream. h> void main() { cout << 2500 << endl; cout << 123. 4567 << endl; cout << 'M' << endl; cout << "Howdy Neighbors" << endl; }
Defining and Assigning Variables Variable Definition Syntax datatype Variable Identifier int Number; char Choice; // optional. but desired comment
// PROG 0502. CPP // This program introduces integer, character and real number // variables. This program also demonstrates the use of an // assignment operator. PROG 0502. CPP OUTPUT #include <iostream. h> 5000 A 3. 14159 void main() { int Var 1; // integer variable char Var 2; // character variable float Var 3; // real number (floating point) variable Var 1 = 5000; // integer assignment Var 2 = 'A'; // character assignment Var 3 = 3. 14159; // real number assignment cout << Var 1 << endl; cout << Var 2 << endl; cout << Var 3 << endl; }
// PROG 0503. CPP // This program demonstrates that C++ is case sensitive. #include <iostream. h> void main() { int Var 1; // integer variable char Var 2; // character variable float Var 3; // real number (floating point) variable Var 1 = 5000; // integer assignment Var 2 = 'A'; // character assignment Var 3 = 3. 14159; // real number assignment cout << var 1 << endl; cout << var 2 << endl; DOES NOT COMPILE AND GIVES cout << var 3 << endl; Compiling PROG 0503. CPP } ERROR MESSAGES LIKE: Error PROG 0503. CPP 18: Undefined symbol 'var 1' Error PROG 0503. CPP 18: Undefined symbol 'var 2' Error PROG 0503. CPP 18: Undefined symbol 'var 3'
Important Warning About Case Sensitivity C++ is case sensitive This means that Gross. Pay and grosspay are two completely different identifiers to a C++ compiler.
The Assignment Operator The two previous program examples included the program statements below. At first glance it may appear like an equation because the equality = sign is used. Var 1 = 5000; Var 2 = 'A'; Var 3 = 3. 14159; // integer assignment // character assignment // real number assignment The program statement Var 1 = 5000; certainly looks like an equation. It is tempting to "Var 1 equals 5000". Saying equals will not really confuse people who understand C++ programming, but it is more correct to say something like: Var 1 gets 5000 Var 1 becomes 5000 is assigned to Var 1
// PROG 0504. CPP // This program demonstrates that the assignment operator can be // used for evaluating mathematical expressions. The program also // shows program output that mixes constants with variables. #include <iostream. h> void main() { int N 1 = 10, N 2 = 20, N 3 = 30; cout << N 1 << " " << N 2 << " " << N 3 << endl; N 1 = 100 + 200; N 2 = 500 + 300; N 3 = -100 + -200; cout << endl; cout << N 1 << " " << N 2 << " " << N 3 << endl; N 1 = N 2 + 400; N 2 = N 1 + 600; N 3 = N 1 + N 2; cout << endl; cout << N 1 << " " << N 2 << " " << N 3 << endl; } PROG 0504. CPP OUTPUT 10 300 20 1200 30 800 1800 -300 3000
Assignment Operator Syntax Variable = Expression Rate = 8. 765; Sum = X + 13; Total = N 1 + N 2 + N 3;
Wrong Assignment Operator Syntax Do not switch the left and right side of an assignment operator. The following examples will not work: 8. 765 = Rate; X + 13 = Sum; N 1 + N 2 + N 3 = Total;
// PROG 0505. CPP // This program demonstrates that the assignment // operator is not an equal sign. // An assignment statement is not an equation. #include <iostream. h> void main() { int Number = 1000; cout << Number << endl; cout << endl; Number = Number + 100; cout << Number << endl; Number = Number + Number; cout << endl; cout << Number << endl; } PROG 0505. CPP OUTPUT 1000 1100 2200
Mixing Data Types The program examples you have seen so far have been logical. Logical in the sense that only integer values were assigned to int variables, characters to char variables, and real numbers to float variables. You might, without realizing it, assign values to the wrong variables. Will the compiler catch such a mistake? If the compiler does not catch it, what is going to happen? These are good questions and program PROG 0506. CPP is designed precisely to answer these questions. This next program example does some pretty bizarre stuff. You will see a letter character assigned to an int variable, a number assigned to a char variable, and a variety of other peculiar assignments that all seem to violate proper assignment practice. Make sure to follow the assignment advise on the next page.
Assignment Advice When in doubt assign like data types, such as: int X, Y; char Letter 1, Letter 2; X = 10; Y = X; X = X + Y; Letter 1 = 'A'; Letter 2 = Letter 1; Only mix data type assignments when this serves a specific purpose, and you know the result of the assignment.
// PROG 0506. CPP // This program demonstrates that it is possible to mix data types. #include <iostream. h> PROG 0506. CPP OUTPUT void main() Int. Var = 'A' { Char. Var = 65 int Int. Var; Float. Var = 25 char Char. Var; Int. Var = 3. 14159 float Float. Var; Char. Var = 'A' + 5 Int. Var = 'A'; Float. Var = 'B' Char. Var = 65; Float. Var = 25; cout << "Int. Var = 'A' " << Int. Var << endl; cout << "Char. Var = 65 " << Char. Var << endl; cout << "Float. Var = 25 " << Float. Var << endl; Int. Var = 3. 14159; Char. Var = 'A' + 5; Float. Var = 'B'; cout << endl; cout << "Int. Var = 3. 14159 " << Int. Var << endl; cout << "Char. Var = 'A' + 5 " << Char. Var << endl; cout << "Float. Var = 'B' " << Float. Var << endl; } 65 A 25 3 F 66
Save Advice Always save your programs before you try to compile/execute. Sooner or later you will find that the computer "locks up" and requires rebooting. Rebooting a computer means the loss of data that is stored temporarily in computer memory, RAM. The programs you write are data that is stored temporarily in memory. Unless you acquire a habit to store your program permanently on a hard drive, diskette or network, you run the risk of losing many hours or computer work.
Initialized Variables In some of the previous programs you have seen program statements like: X = X + 4; This type of statement assumes an initial value for X. The CPU finds the value for X, adds 4 to this value and returns the sum to the memory location of X. Clean and simple, right? True, but what if there is no initial value for X? What will the CPU do?
// PROG 0507. CPP // This program demonstrates how to define and initialize a // variable in the same statement. #include <iostream. h> PROG 0507. CPP OUTPUT void main() 25 50 { # $ int Int. Var 1 = 25; 1025 2050 int Int. Var 2 = 50; 1 2 char Char. Var 1 = '#'; char Char. Var 2 = '$'; cout << Int. Var 1 << " " << Int. Var 2 << endl; cout << Char. Var 1 << " " << Char. Var 2 << endl; Int. Var 1 = Int. Var 1 + 1000; Int. Var 2 = Int. Var 2 + 2000; Char. Var 1 = '1'; Char. Var 2 = '2'; cout << endl; cout << Int. Var 1 << " " << Int. Var 2 << endl; cout << Char. Var 1 << " " << Char. Var 2 << endl; }
Program Sequence How does the computer know what to execute, and when to do the execution of program statements? The answer is sequence. The program is executed in the precise sequence of the program statements created by the programmer.
// PROG 0508. CPP // This program demonstrates that program execution is // controlled by program statement sequence. #include <iostream. h> PROG 0508. CPP ACTUAL OUTPUT Int. Number = 2418 void main() Int. Number = 2500 { Float. Number = 1. 11255 e-11 int Int. Number; Float. Number = 33. 333332 float Float. Number; cout << "Int. Number = " << Int. Number << endl; Int. Number = 2500; cout << "Int. Number = " << Int. Number << endl; Int. Number = 5000; cout << "Float. Number = " << Float. Number << endl; Float. Number = 33. 333333; cout << "Float. Number = " << Float. Number << endl; Float. Number = 66. 666666; }
There certainly is something wrong with the output of PROG 0508. CPP. Perhaps you had expected a program output more along the following lines. PROG 0508. CPP EXPECTED OUTPUT Int. Number = Float. Number 2500 5000 = 33. 333333 = 66. 666666
The problem is best explained by focusing on the following first five program statements extracted from the program: int Int. Number; float Float. Number 1; clrscr(); cout << "Int. Number = " << Int. Number << endl; Int. Number = 2500;
The first three program statement cause no problem. With the very next statement the value of Int. Number is supposed to be displayed. The CPU once again has no problem with that instruction. There exists a memory location for Int. Number and the CPU takes a peek in the memory to determine the value, and then displays the value. Now there will be a value in the memory location for Int. Number. Every BIT in memory has the value of 0 or 1 and combinations of 16 bits are used to store an integer. My computer happened to find 2418, which is surprisingly close to 2500, but the value could be anything. You will probably get a different value on your computer. At this point many students object. You may be one of these students. You object on the ground that the program clearly shows the value 2500 being assigned to Int. Number. You are right, but the problem is that the assignment occurs after the output statement. You see, incorrect program sequence is the problem here.
// PROG 0509. CPP // This is another example that demonstrates why you must use // correct program sequence. This program does not compile. #include <iostream. h> void main() { cout << "Int. Number = " << Int. Number << endl; Int. Number = 2500; cout << "Int. Number = " << Int. Number << endl; Int. Number = 5000; cout << "Float. Number = " << Float. Number << endl; Float. Number = 33. 333333; cout << "Float. Number = " << Float. Number << endl; Float. Number = 66. 666666; int Int. Number; PROG 0509. CPP OUTPUT float Float. Number; } There is no program output. The program will not compile. Some messages indicating that Int. Number and Float. Number are undefined will be your only output.
Identifier Rule All identifiers must be known to C++ before they can be used. C++ checks identifiers three ways: 1. Is the identifier a reserved word? 2. Is the identifier a library function? 3. Is the identifier defined in the program
String Variables apstring Inclusion You must use the statement #include "APSTRING. H" to have access to the special apstring data type. The format "APSTRING. H" is used. Your teacher may use a different include approach, such as "apstring. cpp"
The quotes will appear strange to use. All program examples have used program statements for library functions that use angle brackets, like: #include <iostream. h> #include <conio. h> Now you are expected to use library functions with slightly different syntax, like: #include "apstring. h" or #include "APSTRING. H" What is going on?
When To Use < > And When To Use " " Use <angle brackets> when the file you are including is one of the builtin C++ files that were automatically installed when C++ was installed. These files are located in some obscure directory. The use of <angle brackets> tell the compiler to look for the file in that directory. Use "quotes" when the file you are including is one that you have created or has been given to you. These files are located in the same place as your main. CPP file. This would be either on your disk, or in what ever directory you happen to be working. The use of "quotes" tell the compiler to look for the file in the default directory.
// PROG 0510. CPP // This program introduces the string variable with apstring. #include <iostream. h> #include "APSTRING. H" PROG 0510. CPP OUTPUT Name 1 = Suzie Snodgrass Name 2 = Seymour Snodgrass void main() { apstring Name 1; // 1 st string variable; apstring Name 2; // 2 nd string variable; Name 1 = "Suzie Snodgrass"; Name 2 = "Seymour Schmittlap"; cout << "Name 1 = " << Name 1 << endl; cout << "Name 2 = " << Name 2 << endl; }
// PROG 0511. CPP // This program demonstrates how to initialize a string variable. // The program also shows incorrect value swapping. #include <iostream. h> #include "APSTRING. H" void main() { apstring Name 1 = "Kathy"; // 1 st initialized string variable; apstring Name 2 = "Tommy"; // 2 nd initialized string variable; cout << "Name 1 = " << Name 1 << endl; cout << "Name 2 = " << Name 2 << endl; Name 1 = Name 2; Name 2 = Name 1; cout << endl; cout << "Name 1 = " << Name 1 << endl; cout << "Name 2 = " << Name 2 << endl; } PROG 0511. CPP OUTPUT Name 1 = Kathy Name 2 = Tommy Name 1 = Tommy Name 2 = Tommy
This is the result, in memory, after the two program statements: apstring Name 1 = "Kathy"; apstring Name 2 = "Tommy"; Name 1 Name 2 Kathy Tommy The next assignment statement is crucial. It helps to explain what is happening. Name 1 gets the value from Name 2. The result is that now both string variables contain the exact same value. Name 1 = Name 2; Name 1 Name 2 Tommy The final program statement does not alter anything. It appears that values are swapped, but swapping is not possible, since the value of “Kathy” was lost. Name 2 = Name 1;
// PROG 0512. CPP // This program demonstrates the correct swapping of two variable // values by using an extra variable. #include <iostream. h> #include "APSTRING. H" PROG 0512. CPP OUTPUT void main() Animal 1 = { Animal 2 = apstring Animal 1; apstring Animal 2; Animal 1 = apstring Temp; Animal 2 = Animal 1 = "Tiger"; Animal 2 = "Giraffe"; cout << "Animal 1 = " << Animal 1 << endl; cout << "Animal 2 = " << Animal 2 << endl; Temp = Animal 1; Animal 1 = Animal 2; Animal 2 = Temp; cout << endl; cout << "Animal 1 = " << Animal 1 << endl; cout << "Animal 2 = " << Animal 2 << endl; } Tiger Giraffe Tiger
Swapping Correctly with a Temporary Placeholder Animal 1 = "Tiger"; Animal 2 = "Giraffe"; Animal 1 Animal 2 Temp Tiger Giraffe Unknown We know from previous experience that the value in Animal 1 is likely to be endangered. The next assignment statement preserves the value. Temp = Animal 1; Animal 1 Animal 2 Temp Tiger Giraffe Tiger
With Tiger safely tugged away in Temp, we can copy the Giraffe value to Animal 1 without losing anything. Animal 1 = Animal 2; Animal 1 Animal 2 Temp Giraffe Tiger We now observe that both Animal 1 and Animal 2 store Giraffe. However, this time Tiger is lurking in Temp ready to jump into action. Animal 2 = Temp; Animal 1 Animal 2 Temp Giraffe Tiger
// PROG 0513. CPP // This program demonstrates that string variables can be // defined and initialized two different ways. #include <iostream. h> #include "APSTRING. H" PROG 0513. CPP OUTPUT String 1 = First String 2 = Second String void main() { apstring String 1 = "First String"; apstring String 2("Second String"); cout << "String 1 = " << String 1 << endl; cout << "String 2 = " << String 2 << endl; }
// PROG 0514. CPP // This program demonstrates how to use the // reserved word const. #include <iostream. h> #include "APSTRING. H" PROG 0514. CPP OUTPUT MAX: PI: START: GREETING: const int MAX = 5000; const float PI = 3. 14159; const char START = 'A'; const apstring GREETING = "Good Morning"; 5000 3. 14159 A Good Morning void main() { cout << "MAX: " << MAX << endl; cout << "PI: " << PI << endl; cout << "START: " << START << endl; cout << "GREETING: " << GREETING << endl; }
Syntax for Using const data-type identifier = constant value const int MAX = 5000; It is a common convention, not a requirement, to use all capital letters for a const identifier.
// PROG 0515. CPP // This program demonstrates that it is important to provide the // data type in a const statement. #include <iostream. h> #include "APSTRING. H" PROG 0515. CPP OUTPUT MAX: PI: START: const MAX = 5000; const PI = 3. 14159; const START = 'A'; //const GREETING = "Good Morning"; 5000 3 65 void main() { cout << "MAX: " << MAX << endl; cout << "PI: " << PI << endl; cout << "START: " << START << endl; // cout << "GREETING: " << GREETING << endl; }
// PROG 0516. CPP // This program demonstrates that it is not possible to modify a constant object. #include <iostream. h> #include "APSTRING. H" const int MAX = 5000; const float PI = 3. 14159; const char START = 'A'; const apstring GREETING = "Good Morning"; void main() This program will not compile { MAX = 10000; Error PROG 0516. CPP: Cannot modify a const object PI = 123. 456 START = 'Q'; GREETING = "Good Afternoon"; cout << "MAX: " << MAX << endl; cout << "PI: " << PI << endl; cout << "START: " << START << endl; cout << "GREETING: " << GREETING << endl; }
Literal Constants and Symbolic Constants Examples of literal constants are 2500, 12. 75 and Joe Smith. Number = 2500; Pay. Rate = 12. 75; Name = "Joe Smith"; Examples of symbolic constants are Nbr, PR, and Word. const int Nbr = 10000; const float PR = 9. 25; const apstring Word = "QWERTY"; Symbolic constants cannot be modified.
// PROG 0517. CPP // This program demonstrates that a constant value can be assigned to a variable. #include <iostream. h> #include "APSTRING. H" const int MAX = 5000; const float PI = 3. 14159; const char START = 'A'; const apstring GREETING = "Good Morning"; void main() { PROG 0517. CPP OUTPUT int Int. Var; float Float. Var; Int. Var: 5000 char Char. Var; Float. Var: 3. 14159 apstring String. Var; Char. Var: A Int. Var = MAX; String. Var: Good Morning Float. Var = PI; Char. Var = START; String. Var = GREETING; cout << "Int. Var: " << Int. Var << endl; cout << "Float. Var: " << Float. Var << endl; cout << "Char. Var: " << Char. Var << endl; cout << "String. Var: " << String. Var << endl; }
- Slides: 40