OOP Lec4
OOP Lec4
II
Lecture 4
Nested Structures
Enumerations
Structures within structures
Nested Structures #include <iostream>
You can nest structures within other using namespace std;
structures. struct Distance{ //English distance
int feet;
Here’s a variation on the ENGLSTRC program
float inches;};
that shows how this looks.
struct Room{ //rectangular area
In this program we want to create a data {Distance length; //length of rectangle
structure that stores the dimensions of a
Distance width; }; //width of rectangle
typical room:
int main() {
its length and width. Room dining; //define a room
Since we’re working with English distances, dining.length.feet = 13; //assign values to room
we’ll use two variables of type Distance as dining.length.inches = 6.5;
the length and width variables. dining.width.feet = 10;
dining.width.inches = 0.0;
float l = dining.length.feet + dining.length.inches/12; //convert
length & width
struct Room{
float w = dining.width.feet + dining.width.inches/12;
Distance length; cout << “Dining room area is “ << l * w << “ square feet\n” ;
return 0;}
Distance width;}
Structures within structures
Nested Structures #include <iostream>
This program defines a single variable—dining— using namespace std;
of type Room, in the line struct Distance{ //English distance
Variables of an enumerated type, like day1 and day2, can // demonstrates enum types
be given any of the values listed in the enum declaration. #include <iostream>
In the example we give them the values Mon and Thu. You using namespace std;
can’t use values that weren’t listed in the declaration. //specify enum type
Hence, statements as enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
day1 = halloween; int main()
{
are illegal.
days_of_week day1, day2; //define variables
You can use the standard arithmetic operators on enum
types. //of type days_of_week
The wdcount example counts the words in a phrase typed in by the user.
#include <iostream>
It doesn’t simply count spaces to determine the number of words. #include <conio.h> //for getche()
It passes over (nonspace) characters until it finds a space. At this point it if( isWord == YES ) { //and doing a word, then it’s end
of word
counts a word.
wordcount++; //count the word
Then it passes over spaces until it finds a character, and again counts isWord = NO; //reset flag
} //otherwise, it’s
Doing this requires the program to remember whether it’s in the middle of else //normal character
a word, or in the middle of a string of spaces. if( isWord == NO ) //if start of word,
It remembers this with the enum variable isWord. isWord = YES; //then set flag
return 0;}
Enumeration: wdcount Example // wdcount.cpp
// demonstrates enums, counts words in phrase
#include <iostream>
using namespace std;
isWord is defined to be of type itsaWord. #include <conio.h> //for getche()
This type is specified in the statement enum itsaWord { NO, YES }; //NO=0, YES=1
int main() { //NO when in whitespace
enum itsaWord { NO, YES }; itsaWord isWord = NO; //YES when in a word,
char ch = ‘a’; //character read from keyboard
Variables of type itsaWord have only two int wordcount = 0; //number of words read
cout << “Enter a phrase:\n”;
possible values: NO and YES. do {
Notice that the list starts with NO, so this ch = getche(); //get character
if(ch==’ ‘ || ch==’\r’){ //if white space,
value will be given the value 0—the value if( isWord == YES ) { //and doing a word, then it’s end of
that indicates false. word
wordcount++; //count the word
Can u apply the same idea with isWord = NO; //reset flag