Exposure C++ Chapter V Variables and Constants

40
Exposure C++ Chapter V Variables and Constants

description

Exposure C++ Chapter V Variables and Constants. The Limitation of Constants. // PROG0501.CPP // This program demonstrates using four constants. #include void main() { cout

Transcript of Exposure C++ Chapter V Variables and Constants

Page 1: Exposure C++ Chapter V Variables and Constants

Exposure C++

Chapter V

Variables and Constants

Page 2: Exposure C++ Chapter V Variables and Constants

The Limitation of Constants

// PROG0501.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;}

Page 3: Exposure C++ Chapter V Variables and Constants

Defining and Assigning Variables

Variable Definition Syntax

datatype Variable Identifier // optional. but desired comment

int Number;

char Choice;

Page 4: Exposure C++ Chapter V Variables and Constants

// PROG0502.CPP// This program introduces integer, character and real number// variables. This program also demonstrates the use of an// assignment operator.

#include <iostream.h>

void main(){ int Var1; // integer variable char Var2; // character variable float Var3; // real number (floating point) variable Var1 = 5000; // integer assignment Var2 = 'A'; // character assignment Var3 = 3.14159; // real number assignment cout << Var1 << endl; cout << Var2 << endl; cout << Var3 << endl;}

PROG0502.CPP OUTPUT

5000A3.14159

Page 5: Exposure C++ Chapter V Variables and Constants

// PROG0503.CPP// This program demonstrates that C++ is case sensitive.

#include <iostream.h>

void main(){ int Var1; // integer variable char Var2; // character variable float Var3; // real number (floating point) variable Var1 = 5000; // integer assignment Var2 = 'A'; // character assignment Var3 = 3.14159; // real number assignment cout << var1 << endl; cout << var2 << endl; cout << var3 << endl;}

DOES NOT COMPILE AND GIVES ERROR MESSAGES LIKE:

Compiling PROG0503.CPPError PROG0503.CPP 18: Undefined symbol 'var1'Error PROG0503.CPP 18: Undefined symbol 'var2'Error PROG0503.CPP 18: Undefined symbol 'var3'

Page 6: Exposure C++ Chapter V Variables and Constants

Important Warning About Case Sensitivity

C++ is case sensitive

This means that GrossPay and grosspay are two completely different identifiers to a C++ compiler.

Page 7: Exposure C++ Chapter V Variables and Constants

The Assignment OperatorThe two previous program examples included the program statements below. At first glance it may appear like an equation because the equality = sign is used.

The program statement Var1 = 5000; certainly looks like an equation. It is tempting to "Var1 equals 5000". Saying equals will not really confuse people who understand C++ programming, but it is more correct to say something like:

Var1 gets 5000

Var1 becomes 5000

5000 is assigned to Var1

Var1 = 5000; // integer assignmentVar2 = 'A'; // character assignmentVar3 = 3.14159; // real number assignment

Page 8: Exposure C++ Chapter V Variables and Constants

// PROG0504.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 N1 = 10, N2 = 20, N3 = 30; cout << N1 << " " << N2 << " " << N3 << endl; N1 = 100 + 200; N2 = 500 + 300; N3 = -100 + -200; cout << endl; cout << N1 << " " << N2 << " " << N3 << endl; N1 = N2 + 400; N2 = N1 + 600; N3 = N1 + N2; cout << endl; cout << N1 << " " << N2 << " " << N3 << endl;}

PROG0504.CPP OUTPUT

10 20 30

300 800 -300

1200 1800 3000

Page 9: Exposure C++ Chapter V Variables and Constants

Assignment Operator Syntax

Variable = Expression

Rate = 8.765;

Sum = X + 13;

Total = N1 + N2 + N3;

Page 10: Exposure C++ Chapter V Variables and Constants

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;

N1 + N2 + N3 = Total;

Page 11: Exposure C++ Chapter V Variables and Constants

// PROG0505.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;}

PROG0505.CPP OUTPUT

1000

1100

2200

Page 12: Exposure C++ Chapter V Variables and Constants

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 PROG0506.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.

Mixing Data Types

Page 13: Exposure C++ Chapter V Variables and Constants

When in doubt assign like data types, such as:

int X, Y;char Letter1, Letter2;X = 10;Y = X;X = X + Y;Letter1 = 'A';Letter2 = Letter1;

Only mix data type assignments when this serves a specific purpose, and you know the result of the assignment.

Assignment Advice

Page 14: Exposure C++ Chapter V Variables and Constants

// PROG0506.CPP// This program demonstrates that it is possible to mix data types.

#include <iostream.h>

void main(){ int IntVar; char CharVar; float FloatVar; IntVar = 'A'; CharVar = 65; FloatVar = 25; cout << "IntVar = 'A' " << IntVar << endl; cout << "CharVar = 65 " << CharVar << endl; cout << "FloatVar = 25 " << FloatVar << endl; IntVar = 3.14159; CharVar = 'A' + 5; FloatVar = 'B'; cout << endl; cout << "IntVar = 3.14159 " << IntVar << endl; cout << "CharVar = 'A' + 5 " << CharVar << endl; cout << "FloatVar = 'B' " << FloatVar << endl;}

PROG0506.CPP OUTPUT

IntVar = 'A' 65CharVar = 65 AFloatVar = 25 25

IntVar = 3.14159 3CharVar = 'A' + 5 FFloatVar = 'B' 66

Page 15: Exposure C++ Chapter V Variables and Constants

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.

Page 16: Exposure C++ Chapter V Variables and Constants

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?

Page 17: Exposure C++ Chapter V Variables and Constants

// PROG0507.CPP// This program demonstrates how to define and initialize a// variable in the same statement.

#include <iostream.h>

void main(){ int IntVar1 = 25; int IntVar2 = 50; char CharVar1 = '#'; char CharVar2 = '$'; cout << IntVar1 << " " << IntVar2 << endl; cout << CharVar1 << " " << CharVar2 << endl; IntVar1 = IntVar1 + 1000; IntVar2 = IntVar2 + 2000; CharVar1 = '1'; CharVar2 = '2'; cout << endl << endl; cout << IntVar1 << " " << IntVar2 << endl; cout << CharVar1 << " " << CharVar2 << endl;}

PROG0507.CPP OUTPUT

25 50# $1025 20501 2

Page 18: Exposure C++ Chapter V Variables and Constants

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.

Page 19: Exposure C++ Chapter V Variables and Constants

// PROG0508.CPP// This program demonstrates that program execution is // controlled by program statement sequence.

#include <iostream.h>

void main(){ int IntNumber; float FloatNumber; cout << "IntNumber = " << IntNumber << endl; IntNumber = 2500; cout << "IntNumber = " << IntNumber << endl; IntNumber = 5000; cout << "FloatNumber = " << FloatNumber << endl; FloatNumber = 33.333333; cout << "FloatNumber = " << FloatNumber << endl; FloatNumber = 66.666666;}

PROG0508.CPP ACTUAL OUTPUT

IntNumber = 2418IntNumber = 2500FloatNumber = 1.11255e-11FloatNumber = 33.333332

Page 20: Exposure C++ Chapter V Variables and Constants

There certainly is something wrong with the output of PROG0508.CPP. Perhaps you had expected a program output more along the following lines.

PROG0508.CPP EXPECTED OUTPUT

IntNumber = 2500IntNumber = 5000FloatNumber = 33.333333FloatNumber = 66.666666

Page 21: Exposure C++ Chapter V Variables and Constants

The problem is best explained by focusing on the following first five program statements extracted from the program:

int IntNumber;float FloatNumber1;clrscr();cout << "IntNumber = " << IntNumber << endl;IntNumber = 2500;

Page 22: Exposure C++ Chapter V Variables and Constants

The first three program statement cause no problem.

With the very next statement the value of IntNumber is supposed to be displayed. The CPU once again has no problem with that instruction. There exists a memory location for IntNumber 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 IntNumber. 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 IntNumber. You are right, but the problem is that the assignment occurs after the output statement. You see, incorrect program sequence is the problem here.

Page 23: Exposure C++ Chapter V Variables and Constants

// PROG0509.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 << "IntNumber = " << IntNumber << endl; IntNumber = 2500; cout << "IntNumber = " << IntNumber << endl; IntNumber = 5000; cout << "FloatNumber = " << FloatNumber << endl; FloatNumber = 33.333333; cout << "FloatNumber = " << FloatNumber << endl; FloatNumber = 66.666666; int IntNumber; float FloatNumber;}

PROG0509.CPP OUTPUT

There is no program output. The program will not compile.Some messages indicating that IntNumber and FloatNumber are undefined will be your only output.

Page 24: Exposure C++ Chapter V Variables and Constants

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

Page 25: Exposure C++ Chapter V Variables and Constants

String Variablesapstring 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"

Page 26: Exposure C++ Chapter V Variables and Constants

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?

Page 27: Exposure C++ Chapter V Variables and Constants

When To Use < > And When To Use " "

Use <angle brackets> when the file you are including is one of the built-in 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.

Page 28: Exposure C++ Chapter V Variables and Constants

// PROG0510.CPP// This program introduces the string variable with apstring.

#include <iostream.h>#include "APSTRING.H"

void main(){ apstring Name1; // 1st string variable; apstring Name2; // 2nd string variable;

Name1 = "Suzie Snodgrass"; Name2 = "Seymour Schmittlap";

cout << "Name1 = " << Name1 << endl; cout << "Name2 = " << Name2 << endl;}

PROG0510.CPP OUTPUT

Name1 = Suzie SnodgrassName2 = Seymour Snodgrass

Page 29: Exposure C++ Chapter V Variables and Constants

// PROG0511.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 Name1 = "Kathy"; // 1st initialized string variable; apstring Name2 = "Tommy"; // 2nd initialized string variable;

cout << "Name1 = " << Name1 << endl; cout << "Name2 = " << Name2 << endl;

Name1 = Name2; Name2 = Name1;

cout << endl; cout << "Name1 = " << Name1 << endl; cout << "Name2 = " << Name2 << endl;}

PROG0511.CPP OUTPUT

Name1 = KathyName2 = Tommy

Name1 = TommyName2 = Tommy

Page 30: Exposure C++ Chapter V Variables and Constants

This is the result, in memory, after the two program statements:

apstring Name1 = "Kathy";apstring Name2 = "Tommy";

The next assignment statement is crucial. It helps to explain what is happening. Name1 gets the value from Name2. The result is that now both string variables contain the exact same value.

Name1 = Name2;

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.

Name2 = Name1;

Name1

Kathy

Name2

Tommy

Name1

Tommy

Name2

Tommy

Page 31: Exposure C++ Chapter V Variables and Constants

// PROG0512.CPP// This program demonstrates the correct swapping of two variable// values by using an extra variable.

#include <iostream.h>#include "APSTRING.H"

void main(){ apstring Animal1; apstring Animal2; apstring Temp; Animal1 = "Tiger"; Animal2 = "Giraffe"; cout << "Animal1 = " << Animal1 << endl; cout << "Animal2 = " << Animal2 << endl; Temp = Animal1; Animal1 = Animal2; Animal2 = Temp; cout << endl; cout << "Animal1 = " << Animal1 << endl; cout << "Animal2 = " << Animal2 << endl;}

PROG0512.CPP OUTPUT

Animal1 = TigerAnimal2 = Giraffe

Animal1 = GiraffeAnimal2 = Tiger

Page 32: Exposure C++ Chapter V Variables and Constants

Swapping Correctly with a Temporary Placeholder

Animal1 = "Tiger";Animal2 = "Giraffe";

We know from previous experience that the value in Animal1 is likely to be endangered. The next assignment statement preserves the value.

Temp = Animal1;

Animal1

Tiger

Animal2

Giraffe

Temp

Unknown

Animal1

Tiger

Animal2

Giraffe

Temp

Tiger

Page 33: Exposure C++ Chapter V Variables and Constants

With Tiger safely tugged away in Temp, we can copy the Giraffe value to Animal1 without losing anything.

Animal1 = Animal2;

We now observe that both Animal1 and Animal2 store Giraffe. However, this time Tiger is lurking in Temp ready to jump into action.

Animal2 = Temp;

Animal1

Giraffe

Animal2

Giraffe

Temp

Tiger

Animal1

Giraffe

Animal2

Tiger

Temp

Tiger

Page 34: Exposure C++ Chapter V Variables and Constants

// PROG0513.CPP// This program demonstrates that string variables can be// defined and initialized two different ways.

#include <iostream.h>#include "APSTRING.H"

void main(){ apstring String1 = "First String"; apstring String2("Second String");

cout << "String 1 = " << String1 << endl; cout << "String 2 = " << String2 << endl;}

PROG0513.CPP OUTPUT

String1 = First StringString2 = Second String

Page 35: Exposure C++ Chapter V Variables and Constants

// PROG0514.CPP// This program demonstrates how to use the // reserved word const.

#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(){ cout << "MAX: " << MAX << endl; cout << "PI: " << PI << endl; cout << "START: " << START << endl; cout << "GREETING: " << GREETING << endl;}

PROG0514.CPP OUTPUT

MAX: 5000PI: 3.14159START: AGREETING: Good Morning

Page 36: Exposure C++ Chapter V Variables and Constants

Syntax for Using const

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.

Page 37: Exposure C++ Chapter V Variables and Constants

// PROG0515.CPP// This program demonstrates that it is important to provide the// data type in a const statement.

#include <iostream.h>#include "APSTRING.H"

const MAX = 5000;const PI = 3.14159;const START = 'A';//const GREETING = "Good Morning";

void main(){ cout << "MAX: " << MAX << endl; cout << "PI: " << PI << endl; cout << "START: " << START << endl;// cout << "GREETING: " << GREETING << endl;}

PROG0515.CPP OUTPUT

MAX: 5000PI: 3START: 65

Page 38: Exposure C++ Chapter V Variables and Constants

// PROG0516.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(){ MAX = 10000; PI = 123.456 START = 'Q'; GREETING = "Good Afternoon"; cout << "MAX: " << MAX << endl; cout << "PI: " << PI << endl; cout << "START: " << START << endl; cout << "GREETING: " << GREETING << endl;}

This program will not compile

Error PROG0516.CPP: Cannot modify a const object

Page 39: Exposure C++ Chapter V Variables and Constants

Literal Constants and Symbolic Constants

Examples of literal constants are 2500, 12.75 and Joe Smith.

Number = 2500;PayRate = 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.

Page 40: Exposure C++ Chapter V Variables and Constants

// PROG0517.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(){ int IntVar; float FloatVar; char CharVar; apstring StringVar; IntVar = MAX; FloatVar = PI; CharVar = START; StringVar = GREETING; cout << "IntVar: " << IntVar << endl; cout << "FloatVar: " << FloatVar << endl; cout << "CharVar: " << CharVar << endl; cout << "StringVar: " << StringVar << endl;}

PROG0517.CPP OUTPUT

IntVar: 5000FloatVar: 3.14159CharVar: AStringVar: Good Morning