Statement. Sams Publishing Object-Oriented Programming in C++
Below you will find brief information for Object-Oriented Programming in C++. This book provides a comprehensive guide to object-oriented programming principles and techniques, using the C++ language. It covers topics such as classes, objects, inheritance, polymorphism, templates, and the Standard Template Library (STL).
Advertisement
Advertisement
04 3087 CH03 11/29/01 2:17 PM Page 94
94
Chapter 3
Decisions can be made in C++ in several ways. The most important is with the if...else
statement, which chooses between two alternatives. This statement can be used without the else
, as a simple if statement. Another decision statement, switch
, creates branches for multiple alternative sections of code, depending on the value of a single variable. Finally, the conditional operator is used in specialized situations. We’ll examine each of these constructions.
The
if
Statement
The if statement is the simplest of the decision statements. Our next program,
IFDEMO
, provides an example.
// ifdemo.cpp
// demonstrates IF statement
#include <iostream> using namespace std; int main()
{ int x; cout << “Enter a number: “; cin >> x; if( x > 100 ) cout << “That number is greater than 100\n”; return 0;
}
The if keyword is followed by a test expression in parentheses. The syntax of the if statement is shown in Figure 3.7. As you can see, the syntax of if is very much like that of while
. The difference is that the statements following the if are executed only once if the test expression is true; the statements following while are executed repeatedly until the test expression becomes false. Figure 3.8 shows the operation of the if statement.
Here’s an example of the
IFDEMO program’s output when the number entered by the user is greater than 100:
Enter a number: 2000
That number is greater than 100
If the number entered is not greater than 100, the program will terminate without printing the second line.
04 3087 CH03 11/29/01 2:17 PM Page 95
Loops and Decisions
95
3
F
IGURE
3.7
Syntax of the
if
statement.
F
IGURE
3.8
Operation of the
if
statement.
04 3087 CH03 11/29/01 2:17 PM Page 96
96
Chapter 3
Multiple Statements in the
if
Body
As in loops, the code in an if body can consist of a single statement—as shown in the
IFDEMO example—or a block of statements delimited by braces. This variation on
IFDEMO
, called
IF
2, shows how that looks.
// if2.cpp
// demonstrates IF with multiline body
#include <iostream> using namespace std; int main()
{ int x; cout << “Enter a number: “; cin >> x; if( x > 100 )
{ cout << “The number “ << x; cout << “ is greater than 100\n”;
} return 0;
}
Here’s some output from
IF
2:
Enter a number: 12345
The number 12345 is greater than 100
Nesting
if
s Inside Loops
The loop and decision structures we’ve seen so far can be nested inside one another. You can nest if s inside loops, loops inside if s, if s inside if s, and so on. Here’s an example,
PRIME
, that nests an if within a for loop. This example tells you whether a number you enter is a prime number. (Prime numbers are integers divisible only by themselves and 1. The first few primes are 2, 3, 5, 7, 11, 13, 17.)
// prime.cpp
// demonstrates IF statement with prime numbers
#include <iostream> using namespace std;
#include <process.h> //for exit() int main()
{ unsigned long n, j;
04 3087 CH03 11/29/01 2:17 PM Page 97
Loops and Decisions
97 cout << “Enter a number: “; cin >> n; //get number to test for(j=2; j <= n/2; j++) //divide by every integer from if(n%j == 0) //2 on up; if remainder is 0,
{ //it’s divisible by j cout << “It’s not prime; divisible by “ << j << endl; exit(0); //exit from the program
} cout << “It’s prime\n”; return 0;
}
In this example the user enters a number that is assigned to n
. The program then uses a for loop to divide n by all the numbers from
2 up to n/2
. The divisor is j
, the loop variable. If any value of j divides evenly into n
, then n is not prime. When a number divides evenly into another, the remainder is 0; we use the remainder operator
% in the if statement to test for this condition with each value of j
. If the number is not prime, we tell the user and we exit from the program.
Here’s output from three separate invocations of the program:
Enter a number: 13
It’s prime
Enter a number: 22229
It’s prime
Enter a number: 22231
It’s not prime; divisible by 11
Notice that there are no braces around the loop body. This is because the if statement, and the statements in its body, are considered to be a single statement. If you like you can insert braces for readability, even though the compiler doesn’t need them.
Library Function
exit()
When
PRIME discovers that a number is not prime, it exits immediately, since there’s no use proving more than once that a number isn’t prime. This is accomplished with the library function exit()
. This function causes the program to terminate, no matter where it is in the listing.
It has no return value. Its single argument, 0 in our example, is returned to the operating system when the program exits. (This value is useful in batch files, where you can use the
ERRORLEVEL value to query the return value provided by exit()
. The value 0 is normally used for a successful termination; other numbers indicate errors.)
3
04 3087 CH03 11/29/01 2:17 PM Page 98
98
Chapter 3
The
if...else
Statement
The if statement lets you do something if a condition is true. If it isn’t true, nothing happens.
But suppose we want to do one thing if a condition is true, and do something else if it’s false.
That’s where the if...else
statement comes in. It consists of an if statement, followed by a statement or block of statements, followed by the keyword else
, followed by another statement or block of statements. The syntax is shown in Figure 3.9.
F
IGURE
3.9
Syntax of the
if...else
statement.
Here’s a variation of our
IF example, with an else added to the if
:
// ifelse.cpp
// demonstrates IF...ELSE statememt
#include <iostream> using namespace std;
04 3087 CH03 11/29/01 2:17 PM Page 99
Loops and Decisions
99 int main()
{ int x; cout << “\nEnter a number: “; cin >> x; if( x > 100 ) cout << “That number is greater than 100\n”; else cout << “That number is not greater than 100\n”; return 0;
}
If the test expression in the if statement is true, the program prints one message; if it isn’t, it prints the other.
Here’s output from two different invocations of the program:
Enter a number: 300
That number is greater than 100
Enter a number: 3
That number is not greater than 100
The operation of the if...else
statement is shown in Figure 3.10.
3
F
IGURE
3.10
Operation of the
if...else
statement.
04 3087 CH03 11/29/01 2:17 PM Page 100
100
Chapter 3
The
getche()
Library Function
Our next example shows an if...else
statement embedded in a while loop. It also introduces a new library function: getche()
. This program,
CHCOUNT
, counts the number of words and the number of characters in a phrase typed in by the user.
// chcount.cpp
// counts characters and words typed in
#include <iostream> using namespace std;
#include <conio.h> //for getche() int main()
{ int chcount=0; //counts non-space characters int wdcount=1; //counts spaces between words char ch = ‘a’; //ensure it isn’t ‘\r’ cout << “Enter a phrase: “; while( ch != ‘\r’ ) //loop until Enter typed
{ ch = getche(); //read one character if( ch==’ ‘ ) //if it’s a space wdcount++; //count a word else //otherwise, chcount++; //count a character
} //display results cout << “\nWords=” << wdcount << endl
<< “Letters=” << (chcount-1) << endl; return 0;
}
So far we’ve used only cin and
>> for input. That approach requires that the user always press the Enter key to inform the program that the input is complete. This is true even for single characters: The user must type the character, then press Enter. However, as in the present example, a program often needs to process each character typed by the user without waiting for an Enter. The getche() library function performs this service. It returns each character as soon as it’s typed. It takes no arguments, and requires the
CONIO
.
H header file. In
CHCOUNT the value of the character returned from getche() is assigned to ch
. (The getche() function echoes the character to the screen. That’s why there’s an e at the end of getche
. Another function, getch()
, is similar to getche() but doesn’t echo the character to the screen.)
The if...else
statement causes the word count wdcount to be incremented if the character is a space, and the character count chcount to be incremented if the character is anything but a space. Thus anything that isn’t a space is assumed to count as a character. (Note that this program is fairly naïve; it will be fooled by multiple spaces between words.)
04 3087 CH03 11/29/01 2:17 PM Page 101
Loops and Decisions
101
Here’s some sample interaction with
CHCOUNT
:
For while and do
Words=4
Letters=13
The test expression in the while statement checks to see if ch is the
‘\r’ character, which is the character received from the keyboard when the Enter key is pressed. If so, the loop and the program terminate.
Assignment Expressions
The
CHCOUNT program can be rewritten to save a line of code and demonstrate some important points about assignment expressions and precedence. The result is a construction that looks rather peculiar but is commonly used in C++ (and in C).
Here’s the rewritten version, called
CHCNT
2:
// chcnt2.cpp
// counts characters and words typed in
#include <iostream> using namespace std;
#include <conio.h> // for getche() int main()
{ int chcount=0; int wdcount=1; // space between two words char ch; while( (ch=getche()) != ‘\r’ ) // loop until Enter typed
{ if( ch==’ ‘ ) // if it’s a space wdcount++; // count a word else // otherwise, chcount++; // count a character
} // display results cout << “\nWords=” << wdcount << endl
<< “Letters=” << chcount << endl; return 0;
}
The value returned by getche() is assigned to ch as before, but this entire assignment expression has been moved inside the test expression for while
. The assignment expression is compared with
‘\r’ to see whether the loop should terminate. This works because the entire assignment expression takes on the value used in the assignment. That is, if getche() returns
‘a’
, then not only does ch take on the value
‘a’
, but the expression
3
04 3087 CH03 11/29/01 2:17 PM Page 102
102
Chapter 3
(ch=getche()) also takes on the value
‘a’
. This is then compared with
‘\r’
.
The fact that assignment expressions have a value is also used in statements such as x = y = z = 0;
This is perfectly legal in C++. First, z takes on the value 0, then z = 0 takes on the value 0, which is assigned to y
. Then the expression y = z = 0 likewise takes on the value 0, which is assigned to x
.
The parentheses around the assignment expression in
(ch=getche()) are necessary because the assignment operator
= has a lower precedence than the relational operator
!=
. Without the parentheses the expression would be evaluated as while( ch = (getche() != ‘\r’) ) // not what we want which would assign a true or false value to ch
(not what we want).
The while statement in
CHCNT
2 provides a lot of power in a small space. It is not only a test expression (checking ch to see whether it’s
‘\r’
); it also gets a character from the keyboard and assigns it to ch
. It’s also not easy to unravel the first time you see it.
Nested
if...else
Statements
You’re probably too young to remember adventure games on early character-mode MS-DOS systems, but let’s resurrect the concept here. You moved your “character” around an imaginary landscape and discovered castles, sorcerers, treasure, and so on, using text—not pictures—for input and output. The next program,
ADIFELSE
, models a small part of such an adventure game.
// adifelse.cpp
// demonstrates IF...ELSE with adventure program
#include <iostream> using namespace std;
#include <conio.h> //for getche() int main()
{ char dir=’a’; int x=10, y=10; cout << “Type Enter to quit\n”; while( dir != ‘\r’ ) //until Enter is typed
{ cout << “\nYour location is “ << x << “, “ << y; cout << “\nPress direction key (n, s, e, w): “;
04 3087 CH03 11/29/01 2:17 PM Page 103
Loops and Decisions
103 dir = getche(); //get character if( dir==’n’) //go north y--; else if( dir==’s’ ) //go south y++; else if( dir==’e’ ) //go east x++; else if( dir==’w’ ) //go west x--;
} //end while return 0;
} //end main
When the game starts, you find yourself on a barren moor. You can go one “unit” north, south, east, or west, while the program keeps track of where you are and reports your position, which starts at coordinates 10,10. Unfortunately, nothing exciting happens to your character, no matter where you go; the moor stretches almost limitlessly in all directions, as shown in
Figure 3.11. We’ll try to provide a little more excitement to this game later on.
Here’s some sample interaction with
ADIFELSE
:
Your location is 10, 10
Press direction key (n, s, e, w): n
Your location is 10, 9
Press direction key (n, s, e, w): e
Your location is 11, 9
Press direction key (n, s, e, w):
You can press the Enter key to exit the program.
This program may not cause a sensation in the video arcades, but it does demonstrate one way to handle multiple branches. It uses an if statement nested inside an if...else
statement, which is nested inside another if...else
statement, which is nested inside yet another if...else
statement. If the first test condition is false, the second one is examined, and so on until all four have been checked. If any one proves true, the appropriate action is taken— changing the x or y coordinate—and the program exits from all the nested decisions. Such a nested group of if...else
statements is called a decision tree.
3
04 3087 CH03 11/29/01 2:17 PM Page 104
104
Chapter 3
F
IGURE
3.11
The barren moor.
Matching the
else
There’s a potential problem in nested if...else
statements: You can inadvertently match an else with the wrong if
.
BADELSE provides an example:
// badelse.cpp
// demonstrates ELSE matched with wrong IF
#include <iostream> using namespace std; int main()
{ int a, b, c; cout << “Enter three numbers, a, b, and c:\n”; cin >> a >> b >> c;
04 3087 CH03 11/29/01 2:17 PM Page 105
Loops and Decisions
105 if( a==b ) if( b==c ) cout << “a, b, and c are the same\n”; else cout << “a and b are different\n”; return 0;
}
We’ve used multiple values with a single cin
. Press Enter following each value you type in; the three values will be assigned to a
, b
, and c
.
What happens if you enter 2, then 3, and then 3? Variable a is 2, and b is 3. They’re different, so the first test expression is false, and you would expect the else to be invoked, printing a
and b are different. But in fact nothing is printed. Why not? Because the else is matched with the wrong if
. The indentation would lead you to believe that the else is matched with the first if
, but in fact it goes with the second if
. Here’s the rule: An else is matched with the last if that doesn’t have its own else
.
Here’s a corrected version: if(a==b) if(b==c) cout << “a, b, and c are the same\n”; else cout << “b and c are different\n”;
We changed the indentation and also the phrase printed by the else body. Now if you enter
2
,
3
,
3
, nothing will be printed. But entering
2
,
2
,
3 will cause the output b and c are different
If you really want to pair an else with an earlier if
, you can use braces around the inner if
: if(a==b)
{ if(b==c) cout << “a, b, and c are the same”;
} else cout << “a and b are different”;
Here the else is paired with the first if
, as the indentation indicates. The braces make the if within them invisible to the following else
.
3
Download
Advertisement
Key features
Comprehensive coverage of OOP principles
Practical examples and exercises
Focus on C++ implementation
Explanation of STL and templates
Frequently asked questions
This book covers the object-oriented programming paradigm.
The book primarily uses the C++ programming language.
Yes, the book includes a section on the Standard Template Library (STL).