Listing 10, Structures02.cpp
/*File: Structures02.cpp This C++ program illustrates the sequence structure with a selection structure as one of the elements in the sequence. The selection structure asks the user to enter an integer and then displays one of two different messages depending upon whether the user input is even or odd. The program displays the following output on the screen when the user input is even: Hello Viet Nam Hello Iraq Enter an integer: 4 Your number was even Hello America Hello World The program displays the following output on the screen when the user input is odd: Hello Viet Nam Hello Iraq Enter an integer: 5 Your number was odd Hello America Hello World ************************************************/ #include <iostream> using namespace std; class Structures02{ public: static void classMain(){ Structures02* ptrToObject = new Structures02(); ptrToObject -> doSomething(); }//End classMain function //-------------------------------------------// //An instance function of the Structures02 // class made up of a sequence structure and // a selection structure. void doSelection(){ //Begin sequence structure int temp = 0; cout << "Enter an integer: "; cin >> temp; //End sequence structure //Begin selection structure if(temp % 2 == 0){ cout << "Your number was even" << endl; }else{ cout << "Your number was odd" << endl; }//end else //End selection structure }//end doSelection //-------------------------------------------// //An instance function of the Structures02 // class void doSomething(){ //This function executes a sequence of // statements and then terminates. One of // the elements in the sequence is a // selection 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"; doSelection(); cout << "Hello America\n"; cout << "Hello World\n"; }//end doSomething function };//End Structures02 class //---------------------------------------------// int main(){ Structures02::classMain(); return 0; }//end main Listing 10 |