Lec_05_Structures-2
Lec_05_Structures-2
PROGRAMMING
Dr. Abdelhady Mostafa
Lecture 5
Course contents
■ Lecture 1: Introduction
■ Lecture 2: C++ Hello world!
■ Lecture 3: Flow of control (Loops)
■ Lecture 4: Flow of control (Decisions)
■ Lecture 5: Structures
■ Lecture 6: Functions → (Exam 1)
■ Lecture 7: Functions (cont.)
■ Lecture 8: Objects and classes
■ Lecture 9: Objects and classes (cont.)
■ Lecture 10: Arrays and strings
■ Lecture 11: Arrays and strings (cont.) → (Exam 2)
■ Lecture 12: Advanced topics 2
Today’s menu
■ Structures
■ Enumerations
3
Structures
4
Example
#include <iostream>
using namespace std;
struct part
{
int modelnumber;
int partnumber; Members
float cost;
cout <<“Model ”
};
<< Battery.modelnumber;
int main() cout <<“, part ”
{ << Battery.partnumber;
part Battery; cout <<“, costs $ ”
Battery.modelnumber = 6244; << Battery.cost << endl;
Battery.partnumber = 373; return 0;
Battery.cost = 217.55; }
5
Defining the structure
#include <iostream>
using namespace std;
struct part
{
int modelnumber;
int partnumber;
float cost;
};
int main()
{
part Battery;
Battery.modelnumber = 6244;
Battery.partnumber = 373;
Battery.cost = 217.55;
6
Defining a Structure Variable
#include <iostream>
using namespace std;
Defining a structure varible is the same
struct part
as that for defining a basic built-in data
{
type such as int
int modelnumber;
int partnumber;
float cost;
};
int main()
{
part Battery;
Battery.modelnumber = 6244;
Battery.partnumber = 373;
Battery.cost = 217.55;
7
Accessing Structure Members
#include <iostream>
using namespace std;
Structure member written in three parts:
struct part
• name of the structure variable
• dot operator {
int modelnumber;
• member name
int partnumber;
float cost;
};
Note the first part is the name of structure int main()
variable (Battery), not the name of the {
structure definition (part) part Battery;
Battery.modelnumber = 6244;
Battery.partnumber = 373;
Battery.cost = 217.55;
8
Other Structure Features
#include <iostream>
using namespace std;
1. Initializing Structure Members
struct part
{
int modelnumber;
int partnumber;
float cost;
};
int main()
{
part Battery = { 6244, 373, 217.55 }; part Battery;
Battery.modelnumber = 6244;
Battery.partnumber = 373;
Battery.cost = 217.55;
9
Other Structure Features
#include <iostream>
using namespace std;
2. More variables for a given structure
struct part
{
int modelnumber;
int partnumber;
float cost;
};
int main()
part Battery1 = {6244, 373, 217.55};
{
part Battery2; part Battery1;
Battery1.modelnumber = 6244;
Battery1.partnumber = 373;
Battery1.cost = 217.55;
10
Other Structure Features
#include <iostream>
using namespace std;
3. Structure Variables in Assignment
struct part
Statements
{
int modelnumber;
int partnumber;
float cost;
};
12
Other Structure Features
Remark
part Battery1 = { 6244, 373, 217.55 };
part Battery2 = { 2000, 15, 430.65 };
Part Battery3;
Battery3 = Battery1 + Battery2; // invalid!
13
Other Structure Features
4. Structures can be nested within other structures
struct Distance
{
int feet;
float inches;
};
struct Room
{
Distance length;
Distance width;
};
14
Other Structure Features
4. Structures can be nested within other structures
struct Distance
{
• How to initialize nested structure variables? int feet;
float inches;
• How to access members? };
struct Room
{
Distance length;
Distance width;
};
15
Example
#include <iostream> int main()
using namespace std; {
Room dining;
struct Distance
dining.length.feet = 13;
{
dining.length.inches = 6.5;
int feet;
float inches;
dining.width.feet = 10;
};
dining.width.inches = 0.0;
struct Room
float l = dining.length.feet + dining.length.inches/12;
{
float w = dining.width.feet + dining.width.inches/12;
Distance length;
Distance width;
cout << “Dining room area is ” << l * w
};
<< “ square feet\n” ;
return 0;
} 16
Initializing Nested Structures
Room dining = { {13, 6.5}, {10, 0.0} };
#include <iostream>
Is equivalent to using namespace std;
Room dining;
struct Distance
{
dining.length.feet = 13; int feet;
float inches;
dining.length.inches = 6.5;
};
struct Room
dining.width.feet = 10; {
Distance length;
dining.width.inches = 0.0; Distance width;
};
17
Today’s menu
■ Structures
■ Enumerations
18
Enumerations
■ Enumeration is a different approach to define your own data
■ When you define your own data types, you can simplify and clarify your
programming
19
Example
#include <iostream>
using namespace std;
enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
int main()
{
days_of_week day1, day2;
day1 = Mon;
day2 = Thu;
int diff = day2 - day1;
cout << “Days between = ” << diff << endl;
if(day1 < day2)
cout << “day1 comes before day2\n”;
return 0;
}
20
Enumerations sytax
22
Specifying Integer Values
23
Not perfect
▪ What do you expect for the output of the following code
24
Examples of enumerated data
enum months { Jan, Feb, Mar, Apr, May, Jun,
25
Exercise
26
Wrap up
■ We have covered two topics: structures and enumerations
■ Structures used to group several data items together to form a single entity
27
Next Lecture …
Functions
28
Questions?
29