0% found this document useful (0 votes)
6 views11 pages

OOP Lec4

Uploaded by

alirayan647
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

OOP Lec4

Uploaded by

alirayan647
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Programming Language

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

Room dining; // variable dining of type int feet;


Room float inches;};

 struct Room{ //rectangular area


It then assigns values to the various members of
this structure. {Distance length; //length of rectangle
Distance width; }; //width of rectangle
 Because one structure is nested inside another,
int main() {
we must apply the dot operator twice to access
the structure members. Room dining; //define a room
dining.length.feet = 13; //assign values to room
dining.length.feet = 13;
dining.length.inches = 6.5;
 In this statement, dining is the name of the dining.width.feet = 10;
structure variable, as before; length is the name
dining.width.inches = 0.0;
of a member in the outer structure (Room);
float l = dining.length.feet + dining.length.inches/12; //convert
 feet is the name of a member of the inner length & width
structure (Distance). float w = dining.width.feet + dining.width.inches/12;
 The statement means “take the feet member of cout << “Dining room area is “ << l * w << “ square feet\n” ;
the length member of the variable dining and return 0;}
assign it the value 13.”
Structures within structures
Nested Structures #include <iostream>
 Initializing Nested Structures using namespace std;
struct Distance{ //English distance
 How do you initialize a structure variable that
int feet;
itself contains structures?
float inches;};
 The following statement initializes the variable
struct Room{ //rectangular area
dining to the same values it is given in the
ENGLAREA program: {Distance length; //length of rectangle
Distance width; }; //width of rectangle
Room dining = { {13, 6.5}, int main() {

{10, 0.0} }; Room dining; //define a room


dining.length.feet = 13; //assign
 Each structure of type Distance, which is values to room
embedded in Room, is initialized separately. dining.length.inches = 6.5;
 Remember that this involves surrounding the dining.width.feet = 10;
values with braces and separating them with dining.width.inches = 0.0;
commas. float l = dining.length.feet + dining.length.inches/12; //convert
length & width
 The first Distance is initialized to {13, 6.5}
float w = dining.width.feet + dining.width.inches/12;
cout << “Dining room area is “ << l * w << “ square feet\n” ;
return 0;}
 Depth of nesting??????
Enumerations

 Structures can be looked at as a way to provide user-defined data types.


 Also, enumeration is a different approach to defining your own data type.
 However, This feature of C++ is less crucial than structures.
 They are very much in the spirit of C++, in that, by allowing you to define your own data
types, they can simplify and clarify your programming.
 Enumerated types work when you know in advance a finite (usually short) list of values
that a data type can take on.
Enumeration: dayenum Example
// dayenum.cpp
// demonstrates enum types
 An enum declaration defines the set of all
#include <iostream>
names that will be permissible values of the
type. using namespace std;
//specify enum type
 This is unlike the specification of an int, for
enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
example, which is given in terms of a range of
values. int main()
{
 In an enum you must give a specific name to
days_of_week day1, day2; //define variables
every possible value
//of type days_of_week
 These permissible values are called day1 = Mon; //give values to
enumerators. day2 = Thu; //variables
 The enum type days_of_week has seven int diff = day2 - day1; //can do integer arithmetic
enumerators: Sun, Mon, Tue, and so on, up to cout << “Days between = “ << diff << endl;
Sat if(day1 < day2) //can do comparisons
cout << “day1 comes before day2\n”;
return 0;
}

Syntax of enum specifier.


Enumeration: dayenum Example 2
// dayenum.cpp

 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

 In the program we subtract two values. day1 = Mon; //give values to

 day2 = Thu; //variables


You can also use the comparison operators, as we show
int diff = day2 - day1; //can do integer arithmetic
Here’s the program’s output:
cout << “Days between = “ << diff << endl;
days between = 3
if(day1 < day2) //can do comparisons
day1 comes before day2
cout << “day1 comes before day2\n”;
return 0;
Can you guess what is the output of
}
cout<< day1 ?????
Enumeration: dayenum Example
// dayenum.cpp
 Enumerations are treated internally as integers. // demonstrates enum types
 That is why you can perform arithmetic and relational operations on #include <iostream>
them. using namespace std;
 Ordinarily the first name in the list is given the value 0, the next //specify enum type
name is given the value 1, and so on.
enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
 In the DAYENUM example, the values Sun through Sat are stored as int main()
the integer values 0–6.
{
 Although the compiler knows that your enum variables are really
days_of_week day1, day2; //define variables
integers, you must be careful of trying to take advantage of this fact.
//of type days_of_week
 If you say day1 = 5;
day1 = Mon; //give values to
 The compiler will issue a warning (although it will compile). It’s
day2 = Thu; //variables
better to forget—whenever possible—that enums are really
integers. int diff = day2 - day1; //can do integer arithmetic
 The use of arithmetic and relational operators doesn’t make much cout << “Days between = “ << diff << endl;
sense with some enum types. if(day1 < day2) //can do comparisons
 For example, if you have the declaration cout << “day1 comes before day2\n”;

enum pets { cat, dog, hamster, canary, ocelot }; return 0;


 then it may not be clear what expressions like }

dog + canary or (cat < hamster) mean.


Enumeration: wdcount Example // wdcount.cpp

// demonstrates enums, counts words in phrase

 The wdcount example counts the words in a phrase typed in by the user.
#include <iostream>

using namespace std;

 It doesn’t simply count spaces to determine the number of words. #include <conio.h> //for getche()

enum itsaWord { NO, YES }; //NO=0, YES=1


 Instead it counts the places where a string of non-space characters int main() { //NO when in
changes to a space. whitespace

itsaWord isWord = NO; //YES when in a word,


 This way you don’t get a false count if you type multiple spaces between char ch = ‘a’; //character read
words. from keyboard

int wordcount = 0; //number of words


 (It still doesn’t handle tabs and other whitespace characters.) read

cout << “Enter a phrase:\n”;


 This example shows an enumeration with only two enumerators. do {

 The program cycles in a do loop, reading characters from the keyboard.


ch = getche(); //get character

if(ch==’ ‘ || ch==’\r’){ //if white space,

 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

characters until it finds a space. }

} //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

} while( ch != ‘\r’ ); //quit on Enter key

cout << “\n---Word count is “ << wordcount << “---\n”;

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

different data type??? }


} //otherwise, it’s
else //normal character
if( isWord == NO ) //if start of word,
isWord = YES; //then set flag
} while( ch != ‘\r’ ); //quit on Enter key
cout << “\n---Word count is “ << wordcount << “---\n”;
return 0;}
Enumeration: wdcount Example // wdcount.cpp
// demonstrates enums, counts words in phrase
#include <iostream>
 The isWord variable is set to NO when the using namespace std;

program starts. #include <conio.h> //for getche()


enum itsaWord { NO, YES }; //NO=0, YES=1
 When the program encounters the first int main() { //NO when in whitespace
itsaWord isWord = NO; //YES when in a word,
nonspace character, it sets isWord to YES char ch = ‘a’; //character read from keyboard
to indicate that it’s in the middle of a word. int wordcount = 0; //number of words read
cout << “Enter a phrase:\n”;
 It keeps this value until the next space is do {
found, at which point it’s set back to NO. ch = getche(); //get character
if(ch==’ ‘ || ch==’\r’){ //if white space,
 Behind the scenes, NO has the value 0 and if( isWord == YES ) { //and doing a word, then it’s end of
YES has the value 1, word
wordcount++; //count the word
 but we avoid making use of this fact. isWord = NO; //reset flag
}
 We could have used if(isWord) instead of } //otherwise, it’s
if(isWord == YES), and if(!isWord) instead else //normal character

of if( isWord == NO ) //if start of word,


isWord = YES; //then set flag
 9isWord == NO), but this is not good style. } while( ch != ‘\r’ ); //quit on Enter key
cout << “\n---Word count is “ << wordcount << “---\n”;
return 0;}

You might also like