Lecture 12 Structures
Lecture 12 Structures
Structures
1
COMP102 Prog. Fundamentals, Structures / Slide 2
Contents
Objectives
Structures Basics
Initialization
Accessing data members
Structure to structure assignment
Examples
2
COMP102 Prog. Fundamentals, Structures / Slide 3
Objectives
Why do we need structures?
Store data using simple variables; but how many variables
we can use?
Arrays can solve this issue.
Although, arrays greatly improved our ability to store data,
each element in an array must be of the same type.
Sometimes, need to store data of different data types. We
use structures:
Structure is a collection of variables of different data types under a
single name.
Used different types of variables, can create our own type
3
COMP102 Prog. Fundamentals, Structures / Slide 4
4
COMP102 Prog. Fundamentals, Structures / Slide 5
Structures
Structures can be looked as a way to provide user-defined data types.
A Structure is a collection of simple variables of different types.
The data items in a structure are called the members of the structure.
A structure in C++ is very much a class, although it may not fulfill some
properties associated with classes.
Structure is a collection of data while class contains data & functions
A structure type in C++ is defined by keyword struct.
A struct is heterogeneous in that it consists of data of different data
types.
In contrast, array is homogeneous since it can contain only data of the
same data type.
5
COMP102 Prog. Fundamentals, Structures / Slide 6
Structures
Structures hold data that belong to each other.
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.
6
COMP102 Prog. Fundamentals, Structures / Slide 7
Structures
Individual components of a struct type are called members.
Members can be of different types (simple, array or struct).
A struct is named as a whole while individual members are
named using identifiers.
Members defined in structure are just a blueprint for the creation
of variables.
They don’t occupy any memory unless they are used & assigned some
value. This is against simple variable declarations.
However, definition of structure variable reserves
enough space to hold all the members of structure ac
cording to data types of members.
7
COMP102 Prog. Fundamentals, Structures / Slide 8
Structure Basics
We can declare an object of type structure in same
way as we declare variables of some data type: i.e.
Student first_student;
The declarations of the structure are enclosed in
braces.
A semicolon follows the closing brace, terminating the
entire structure.
struct <struct-type>{
<type> <identifier_list>; Each identifier
<type> <identifier_list>; defines a member
... of the structure.
} ;
8
COMP102 Prog. Fundamentals, Structures / Slide 9
Structure’s Function
A structure defines
Structure name
Structure variable
It assigns values to its members
It displays the values stored.
9
COMP102 Prog. Fundamentals, Structures / Slide 10
Structure Examples
Example:
struct StudentInfo{
int Id; The “StudentInfo”
int age;
char Gender; structure has 4 members
double CGA; of different types.
};
Example:
struct StudentGrade{
char Name[15];
char Course[9]; The “StudentGrade”
int Lab[5]; structure has 5
int Homework[3];
int Exam[2];
members of
}; different array types.
10
COMP102 Prog. Fundamentals, Structures / Slide 11
Structure Examples
Example:
struct BankAccount{
char Name[15]; The “BankAcount”
int AcountNo[10]; structure has simple,
double balance; array and structure
Date Birthday;
}; types as members.
Example:
struct StudentRecord{
char Name[15]; The “StudentRecord”
int Id; structure has 4
char Dept[5];
char Gender; members.
};
11
COMP102 Prog. Fundamentals, Structures / Slide 12
12
COMP102 Prog. Fundamentals, Structures / Slide 13
Declaration
Most efficient method of dealing with structure
variables is to define structure globally
13
COMP102 Prog. Fundamentals, Structures / Slide 14
Structure Declaration
Declaration of a variable of struct type:
<struct-type> <identifier_list>;
Example:
StudentRecord Student1, Student2;
Name Name
Student1 Student2
Id Gender Id Gender
Dept Dept
14
COMP102 Prog. Fundamentals, Structures / Slide 15
Structure Initialization-1
struct part //declare a structure
{
int modelnumber; //ID number
int partnumber; //ID number
float cost; //cost of part
};
int main()
{
part part1; //define a structure variable
part1.modelnumber = 6244; //give values to structure members
part1.partnumber = 373;
part1.cost = 217.55F;
//display structure members
cout << “Model “ << part1.modelnumber;
cout << “, part “ << part1.partnumber;
cout << “, costs $” << part1.cost << endl;
return 0; 15
}
COMP102 Prog. Fundamentals, Structures / Slide 16
16
COMP102 Prog. Fundamentals, Structures / Slide 17
17
COMP102 Prog. Fundamentals, Structures / Slide 18
18
COMP102 Prog. Fundamentals, Structures / Slide 19
Example:
strcpy(Student1.Name, “Abc");
Student1.Id = 12345;
Student1
strcpy(Student1.Dept, "COMP");
Student1.gender = 'M';
cout << "The student is ";
switch (Student1.gender){
case 'F': cout << "Ms. "; break;
case 'M': cout << "Mr. "; break; Name
}
cout << Student1.Name << endl;
Id Gender
Dept
Abc
12345 M
COMP
19
COMP102 Prog. Fundamentals, Structures / Slide 20
Example: Student1
strcpy(Student1.Name,
"Chan Tai Man");
Student1.Id = 12345; Chan Tai Man
strcpy(Student1.Dept, "COMP");
Student1.gender = 'M';
12345 M
Student2 = Student1;
COMP
12345 M
Student2
COMP 20
COMP102 Prog. Fundamentals, Structures / Slide 21
int main()
{
Box firstBox = { 80.0, 50.0, 40.0 };
COMP102 Prog. Fundamentals, Structures / Slide 22
cout << "Increasing the box dimensions by 10% has increased the volume by "
<<
static_cast<long>((volume(secondBox)-firstBoxVolume)*100.0/firstBoxVolume)
<< "%"
<< endl;
return 0;
}
// Function to calculate the volume of a box
double volume(Box& aBox)
{
return aBox.length * aBox.breadth * aBox.height;
}
COMP102 Prog. Fundamentals, Structures / Slide 24
Nested Structures
26
COMP102 Prog. Fundamentals, Structures / Slide 27
Nested Structures
27
COMP102 Prog. Fundamentals, Structures / Slide 28
Arrays of structures
An ordinary array: One type of data
0 1 2 … 98 99
0 1 2 … 98 99
28
COMP102 Prog. Fundamentals, Structures / Slide 29
Arrays of structures
We often use arrays of structures.
Example:
StudentRecord Class[100];
strcpy(Class[98].Name, "Chan Tai Man");
Class[98].Id = 12345;
strcpy(Class[98].Dept, "COMP");
Class[98].gender = 'M';
Class[0] = Class[98]; Chan Tai Man
12345 M
COMP
...
0 1 2 … 98 99 29
COMP102 Prog. Fundamentals, Structures / Slide 30
Program Output
30
COMP102 Prog. Fundamentals, Structures / Slide 31
Remove Errors
31
COMP102 Prog. Fundamentals, Structures / Slide 32
Unions
Union is a data type which allows you to use same block of
memory to store values of different types at different times.
The syntax is:
union weight
{
int paisas;
double rupees;
};
We can declare a variable of type union just like we did for
structures. Similarly data members of the union are
accessed using the dot operator.
The major difference is that at any given point in time only
one of the data members of the union can have a value.
COMP102 Prog. Fundamentals, Structures / Slide 33
Enumeration
An enumeration is a user-defined data type that consists of
integral constants.
It enables you to create a new data type that has a fixed
range of possible values, and the variable can select one
value from the set of values.
e.g., selecting only one flavor of ice cream in a shop.
Enumerated types work when you know in advance a finite
short list of values that a data type can take on.
An enum declaration defines the set of all names that
will be permissible values of the type.
enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
33
COMP102 Prog. Fundamentals, Structures / Slide 34
35
COMP102 Prog. Fundamentals, Structures / Slide 36
Example
An enum variable takes only one value out of many possible
values.
#include <iostream>
using namespace std;
int main()
{
week today;
today = Wednesday;
cout << "Day " << today+1;
return 0;
}
Output :
Day 4 36
COMP102 Prog. Fundamentals, Structures / Slide 37
Example
An enum variable takes only one value out of many possible
values.
#include <iostream>
using namespace std;
enum suit {
club = 0,
diamonds = 10,
hearts = 20, Output:
spades = 3
} card; Size of enum variable 4 bytes.
int main()
{
card = club;
cout << "Size of enum variable " << sizeof(card) << " bytes.";
return 0; 37
}