1. 0
Outline:
◦ Definition of Structures
◦ Array of structures
◦ Structure and functions
◦ Enumerated data type
2. A structure in C++ are a group of data elements grouped together under
one name. these data elements known as a member of a structure.
A structure type in C++ is called struct.
A struct is heterogeneous in that it can be composed of data of different
types.
In contrast, array is homogeneous since it can contain only data of the same
type.
1
3. Unlike an array, a structure can contain many different data types (int,
string, bool, etc.).
Structures hold data that belong together.
Examples:
◦ Student record: student id, name, major, gender, start year, …
◦ Bank account: account number, name, currency, balance, …
◦ Address book: name, address, telephone number, …
In database applications, structures are called records.
Individual components of a struct type are called members (or fields).
2
4. Members can be of different types (simple, array or
struct).
A struct is named as a whole while individual members
are named using field identifiers.
Complex data structures can be formed by defining
arrays of structs.
3
5. The keyword ‘struct ‘ is used for creating a structure.
The structure declaration appears at the top of the source code file
before any variables and structures are defined.
Then inside the curly braces, you can declare one or more members
(declare variables inside curly braces) of that structure. For
example:
Definition of a structure:
struct structure_name
{
data-type member-1;
data-type member-2;
data-type member-3;
data-type member-4;
} ;
4
Example:
struct Date {
int day;
int month;
int year;
} ;
The “Date” structure
has 3 members,
day, month & year.
Notice the
required ;
6. 5
Example:
struct StudentInfo{
int Id;
int age;
char Gender;
double CGPA;
};
Example:
struct StudentGrade{
char Name[15];
char Course[9];
int Lab[5];
int Homework[3];
int Exam[2];
};
The “StudentInfo”
structure has 4 members
of different types.
The “StudentGrade”
structure has 5
members of
different array types.
7. struct names commonly begin with an uppercase
letter
Multiple fields of same type can be in a comma-
separated list.
string name,address;
6
The “Person”
structure has 3 members
of different types.
8. 7
Struct declaration does not allocate memory or create
variables.
To allocate memory spaces for structure members, we must
create an object which is called structure variable.
Let’s create a structure having 3 variable a, b, c.
Method 1:
struct structure_name
{
member definition;
member definition;
...
member definition;
} ;
struct structure_name a,b,c;
9. 8
Method 2:
struct structure_name
{
member definition;
member definition;
...
member definition;
} ;
structure_name a,b,c;
Method 3
struct structure_name
{
member definition;
member definition;
...
member definition;
}a,b,c ;
10. To access any member of a structure, we use the member access operator
(.). The member access operator is coded as a period between the structure
variable name and the structure member that we wish to access.
◦ structure variable name. structure member
9
p.name; is char array element of
structure p.
p.age; is the integer element of
structure p.
p.salary; is a float element of
structure p.
The “Person”
structure has 3 members
of different types.
name
age
salary
Person
name
age
salary
p
11. To display the contents of a struct variable, you must display each field
separately, using the dot operator
struct Student
{
int studentID;
string name;
short year;
double gpa;
};
Student s1;
cout << s1; // won’t work!
cout << s1.studentID << endl;
cout << s1.name << endl;
cout << s1.year << endl;
cout << s1.gpa;
10
Wrong:
Correct
12. 11
Cannot initialize members in the structure declaration,
because no memory has been allocated yet.
struct Student // Illegal
{ //initialization
int studentID = 1145;
string name = "Alex";
short year = 1;
float gpa = 2.95;
};
13. 12
Structure members are initialized at the time a structure
variable is created.
Can initialize a structure variable’s members with
either
◦ an initialization list
◦ a constructor
An initialization list is an ordered set of values,
separated by commas and contained in { }, that
provides initial values for a set of data members.
{12, 6, 3} // initialization list
// with 3 values
14. 13
Order of list elements matters: The first value
initializes the first data member, the second value
initializes the second data member, etc.
Elements of an initialization list can be constants,
variables, or expressions.
{12, W, L/W + 1} // initialization
//List with 3 items
15. 14
Initialization list example:
struct Dimensions
{
int length,
int width,
int height;
};
Dimensions box = {12,6,3};
box
length 12
width 6
height 3
Can initialize just some members, but cannot skip over
members.
Dimensions box1 = {12,6}; //OK
Dimensions box2 = {12,,3}; //illegal
Can’t omit a value for a member without omitting values for all following members.
16. 15
C++ Program to assign data to members of a structure variable and
display it.
Output
Enter Full name: abebe
kebede
Enter age: 27
Enter salary: 1024.4
Displaying Information.
Name: abebe kebede
Age: 27
Salary: 1024.4
17. We can create an array of structures. thus we can use
the same structure for more than one variable which
adds more flexibility to your program.
In another word array of structure means making the
structure variable an array.
suppose we need to store the data of 100 students.
Declaring 100 separate variables of the structure is
definitely not a good option. For that, we need to create
an array of structures.
16
19. We can also create an object of a structure in another
structure as members of a structure.
If you have data like student information (name, id, age,
section), and the address of a person (kebele, kefle-Ketama,
and road name). what are you going to do? You must be
able to incorporate this type of data in other structs. The
following program declares two structs one for address and
the other for students.
18
21. Structure variables can be passed to a function and returned in a similar way as
normal arguments.
There are two methods by which we can pass structures to functions.
• Passing by Value
• Passing by Reference
The call/pass-by value method of passing arguments to a function, copies the actual
value of an argument into the formal parameter of the function.
In this case changes made to the parameter inside the function have no effect on the
argument.
By default, C++ uses call-by-value to pass arguments. In general, this means that
code within a function cannot alter the arguments used to call the function.
20
22. The call/pass by reference method of passing arguments to
a function, copies the reference of an argument into the
formal parameter.
Inside the function, the reference is used to access the actual
argument used in the call. this means that changes made to
the parameter affect the passed arguments.
To pass the value by reference, argument reference is
passed to the functions just like any other value.
21
27. An enumeration is a user-defined data type that consists
of integral constants.
Enums or enumerations are generally used when you
expect the variable to select one value from the
possible set of values.
To define an enumeration, the keyword enum is used.
26
28. 27
By default, spring is 0, summer is 1 and so on. You can change the default value of an
enum element during declaration (if necessary).