Listing 11, Structures03.cpp
/*File: Structures03.cpp
This C++ program illustrates the use of a
sequence structure that executes a loop structure
as one of the action elements in the sequence.
The loop structure executes a selection structure
as one of the action elements during each
iteration of the loop. Thus, this one program
illustrates all three of the primary structures:
sequence
selection
loop
The following is a typical output that appears
on the screen when the program is run:
Hello Viet Nam
Hello Iraq
Enter an even integer to quit
or an odd integer to loop: 1
Enter an even integer to quit
or an odd integer to loop: 3
Enter an even integer to quit
or an odd integer to loop: 5
Enter an even integer to quit
or an odd integer to loop: 6
I'm outta here!
Hello America
Hello World
************************************************/
#include <iostream>
using namespace std;
class Structures03{
public:
static void classMain(){
Structures03* ptrToObject =
new Structures03();
ptrToObject -> doSomething();
}//End classMain function
//-------------------------------------------//
//An instance function of the Structures03
// class that executes a selection structure.
// The function receives an incoming parameter
// of type int. It returns true if the incoming
// parameter is even and false if the incoming
// parametr is odd.
bool doSelection(int data){
bool result;
//Begin selection structure
if(data % 2 == 0){
result = true;
}else{
result = false;
}//end else
//End selection structure
return result;
}//end doSelection
//-------------------------------------------//
//An instance function of the Structures03
// class made up of a sequence structure
// followed by a loop structure. The loop
// structure uses a selection structure
// to decide when to quit. The user is
// asked to enter an integer during each
// iteration of the loop structure. When
// the user enters an even integer, the
// loop structure terminates.
void doLoop(){
//Begin sequence structure.
bool quit = false;
int temp = 0;
//End sequence structure
//Begin loop structure
while(quit != true){
cout << "Enter an even integer to quit\n"
<< "or an odd integer to loop: ";
cin >> temp;
//Execute a selection structure
quit = doSelection(temp);
}//end while loop
//End loop structure
cout << "I'm outta here!" << endl;
}//end doLoop
//-------------------------------------------//
//An instance function of the Structures03
// class
void doSomething(){
//This function executes a sequence of
// statements and then terminates. One of
// the elements in the sequence is a loop
// structure, which is implemented
// as a function, but could be implemented
// as inline code just as well.
cout << "Hello Viet Nam\n";
cout << "Hello Iraq\n";
//Execte the loop structure.
doLoop();
cout << "Hello America\n";
cout << "Hello World\n";
}//end doSomething function
};//End Structures03 class
//---------------------------------------------//
int main(){
Structures03::classMain();
return 0;
}//end main
Listing 11
|