Reference – Sam’s Teach Yourself C++ pg 46

 

Program Constants

 

 

What we have not covered yet are constants that use an identifier, called symbolic constants.  In this case we want to use an identifier and it looks just like a variable, but it is a constant.  One really terrific clue that an identifier is a constant is the appearance of the C++ reserved word const in front of the identifier, like in the following program

 

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

 

#include <iostream.h>

#include <conio.h>

#include <STRING>

 

const int MAX = 5000;

const double PI = 3.14159;

const char START = 'A';

const char *GREETING = "Good Morning";

 

void main()

{

  

   cout << "MAX:       " << MAX << endl;

   cout << "PI:        " << PI << endl;

   cout << "START:     " << START << endl;

   cout << "GREETING:  " << GREETING << endl;

   getch();

}

 

MAX:       5000

PI:        3.14159

START:     A

Greeting:  Good Morning

 

The word const is used in front of what normally would be a variable definition.

 

Second, the const statements are located near the top of the program and not inside the braces { } of the main function.

 

The word const is logical.  You are stating that the identifier that follows will get one value and it had better be happy about that one value.  There are not going to be any changes.  In a small program there will seem little value in using any constants.  In a large program there are some benefits.  Certain values change very rarely, but when they do, changing the const  value at the top of the program will make a change throughout the program. 

 

The second difference - the constant statements appearing above main - makes the constants global.  This means that the constant identifiers can be used anywhere in the program.  At this stage this simply means in the main part of the program.  However, doesn’t main imply something that is not main.  We have not seen that yet, but you will soon.

 

Right now you have to realize and accept something.  The nature of computer science is that you are frequently introduced to concepts and tools that are difficult to justify at their introduction.  Do have patience, and faith, that clarity will be around the corner.  If clarity is not around the corner, hike a few more blocks and you will see the light soon enough.  I have seen many a beginning skier grumbling about a tiring exercise of side stepping up a hill.  I see such skiers looking at the many comfortable ski lifts heading up the mountain.  Beginning skiers are a simple lot --- to them the lift takes you up and gravity brings you down.  Later in the trip these jolly beginners lose a pole or they need to assist a buddy who falls down.  Oh no, there is not a short-distance lift, and you need to move up the steep mountain.  Well what do you know, those dumb ol’ side step exercises are coming in handy after all.

 

A constant cannot be changed; the compiler should will flag an error.

 

Now how about assigning a const value to a variable identifier?  No problem at all.  C++ is more than happy to let you use constants in practically every situation.  The only important restriction is that you do not try to alter the constant value.  There is no point in calling an identifier a constant if a constant can be changed.

 

 

The Following is extracted from - Pg 62 Sam’s Teach Yourself C++

 

The Relational Operators

 

Name

Operator

Sample

Evaluates

Equals

==

100 == 50

50 == 50

False

True

Not Equals

!=

100 != 50

50 != 50

True

False

Greater Than

>

100 > 50

50 > 50

True

false

Greater Than or Equal to

>=

100 >= 50

50 >= 50

True

True

Less Than

<

50 < 50

100 < 50

False

False

Less Than or Equal to

<=

100 <= 50

50 <= 50

False

True

 

 

A little more on Pointers

 

char buf[8] = “abcdefg”;

 

buf[0] == *buf == ‘a’  // true

 

*(buf +1) ==  buf[1] //true

 

buf == &buf[0] //true

 

 

Assignments for Lesson 5B

 

 

·       Read Sam’s Teach Yourself C++ from page 46 to 68

·       Lab’s write a C++ to do strlen, strcpy and strcmp

·       Reference: for lab assignments are in Hour 9 of Sam’s Teach Yourself C++

 

Strlen is the length of an arbitrary Character String.

 

Strcpy Copies the contents of the first string into a second string

 

Strcmp Compares two strings and returns true if the  contents of string 1 match the contents of string 2