0% found this document useful (0 votes)
3 views

Lec_05_Structures-2

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

Lec_05_Structures-2

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

COMPUTER AND

PROGRAMMING
Dr. Abdelhady Mostafa

Assiut University – Dept. of Mech. Eng.


MET 111 - First semester 2022/2023

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

■ A structure is a collection of simple variables.

■ Variables in a structure can be of different types: Some can be


int, some can be float, and so on.

■ Data items in a structure are called members

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

part Battery1 = {6244, 373, 217.55}; int main()


{
part Battery2;
part Battery1;
Battery1 = Battery2 Battery1.modelnumber = 6244;
Battery1.partnumber = 373;
Battery1.cost = 217.55;
11
Other Structure Features

3. Structure Variables in Assignment Statements

part Battery1 = {6244, 373, 217.55};


part Battery2;
Battery1 = Battery2

■ Each member of Battery2 will be assigned to the corrsponding


member of Battery1
■ Battery1 and Battery2 must be of the same structure type

12
Other Structure Features
Remark
part Battery1 = { 6244, 373, 217.55 };
part Battery2 = { 2000, 15, 430.65 };
Part Battery3;
Battery3 = Battery1 + Battery2; // invalid!

■ You cannot perform arithmetic operations on structure


variables
■ You can perform arithmetic operations on members

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

■ Enumerated types work when you know in advance a finite (usually


short) list of values that a data type can take on

■ An enumeration is a list of all possible values

■ 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

Enumerations are treated internally as integers, the first name is


given the value 0, the next name is given the value 1, and so on
21
Enumerations rules
#include <iostream>
▪ You cannot use values that were using namespace std;
not listed in the declaration enum days_of_week { Sun, Mon, Tue, Wed, Thu,
Fri, Sat };
▪ You can use arithmetic and int main()
comparison operators on enum {
types days_of_week day1, day2;
day1 = Mon;
▪ Arithmetic operations on enum day2 = Thu;
types take place on the integer int diff = day2 - day1;
cout <<“Days between = ” << diff << endl;
values
if(day1 < day2)
cout << “day1 comes before day2\n”;
return 0;
}

22
Specifying Integer Values

▪ You can change the starting integer value

enum days_of_week {Sun=1, Mon, Tue, Wed, Thu, Fri, Sat};

▪ You can give a specified value to any enumerator

enum days_of_week {Sun=1, Mon=3, Tue=12, Wed=40, Thu=5, Fri=7, Sat=2};

23
Not perfect
▪ What do you expect for the output of the following code

enum direction { north, south, east, west };

direction dir1 = south;

cout << dir1;

▪ C++ I/O treats variables of enum types as integers, so the output


would be 1.

24
Examples of enumerated data
enum months { Jan, Feb, Mar, Apr, May, Jun,

Jul, Aug, Sep, Oct, Nov, Dec };

enum switch { off, on };

enum meridian { am, pm };

enum chess { pawn, knight, bishop, rook, queen, king };

enum coins { penny, nickel, dime, quarter, half_dollar, dollar };

25
Exercise

26
Wrap up
■ We have covered two topics: structures and enumerations

■ Structures are an important component of C++

■ Structures used to group several data items together to form a single entity

■ Values inside a structure variable called members

■ Enumeration is a programmer-defined type limited to a fixed list of values

■ Values inside an enumeration called enumerators

■ Internally the compiler treats enumeration variables as integers

27
Next Lecture …

Functions

28
Questions?

29

You might also like