Structures
Chapter 4
Introduction
• Suppose you want to store information about students
in a class
– Assuming that the class has 5 students
• You might want to store their names, addresses, and
grades
2
Introduction
string name[5] = {“Tom”, “Jerry”, “Pluto”,
“Donald”, “Mickey”};
string adr[5] = {“12 Cat st.”,
“34 Mouse st.”,
“56 Dog st.”,
“78 Duck st.”,
“90 Mouse st.”};
float grade[5] = {1.1, 3.3, 5.5, 7.7, 9.9};
for (i = 0; i < 5; i++)
cout << name[i] << adr[i] << grade[i];
3
Introduction
for (i = 0; i < 4; i++)
for (j = i + 1; j < 5; j++)
if (name[i] > name[j])
swap(name[i] > name[j]);
for (i = 0; i < 5; i++)
cout << name[i] << adr[i] << grade[i];
// {“Donald”,“Jerry”,“Mickey”, “Pluto”, “Tom”}
// {1.1, 3.3, 5.5, 7.7, 9.9}
4
C++ Structures
• A structure is a user-defined type
• A single structure can hold items of more than one data
type
• How to define a structure type?
struct <tag> {
<type 0> <member/field name 0>;
<type 1> <member/field name 1>;
…
}; 5
C++ Structures
struct student {
string name;
string adr;
float grade;
};
student a, b, * ps, group[5];
6
Accessing Structure Members
• The dot operator is used to access structure members
<name of structure variable>.<member name>
cout << "Enter the student's name: ";
getline(cin, a.name);
cout << "Enter the student's address: ";
getline(cin, a.adr);
cout << "Enter the student's grade: ";
cin >> a.grade;
cout << a.name << a.adr << a.grade;
group[0] = a;
cout << group[0].name << group[0].adr <<
group[0].grade; 7
Structures as Function Arguments/Return Values
• Consider the following function prototypes:
void sortData(student []);
void inputItem(student &);
student inputItem();
student * inputItem();
void outputItem(student);
void outputItem(student *);
8
Initializing a Structure
• The members of a structure variable may be initialized
with starting values
student a = {"Tom", "12 Cat st.", 1.1};
student group[5] = {
{"Tom", "12 Cat st.", 1.1},
{"Jerry", "34 Mouse st.", 3.3},
{"Pluto", "56 Dog st.", 5.5},
{"Donald", "78 Duct st.", 7.7},
{"Mickey", "90 Mouse st.", 9.9}
};
9
Pointers to Structures
• Similar to define pointers to built-in types, you may
define pointers to structures
student a, * p, * q, group[5];
p = &a;
q = group;
• In general, there are three ways to access structure
members
a.name p->name (*p).name
10
Dynamically Allocating a Structure
• You can use a structure pointer and the new operator to
dynamically allocate a structure
student * p;
p = new student;
getline(cin, p->name); getline(cin, p->adr);
cin >> p->grade; …; delete p;
• You can also dynamically allocate an array of structures
student * group;
group = new student [10];
getline(cin, group[5].name);
cin >> group[5].grade; …
delete [] group; 11
Pointers As Members
• Sometimes structures contain pointers as members
struct ABC {
int * ptr;
};
ABC a, * p;
• Consider several ways to access a pointer that is a
member of a structure
a.ptr ⇔ p->ptr ⇔ (*p).ptr
*a.ptr ⇔ *p->ptr ⇔ *(*p).ptr
12
Pointers As Members
struct ABC {
int * ptr;
};
ABC a, b;
a.ptr = new int [N];
b.ptr = new int [N];
a = b;
delete [] a.ptr;
delete [] b.ptr; // ???
13
Nested Structures
• Consider the following type definition
struct student {
string name;
string adr;
float grade;
int dayOfBirth;
int monthOfBirth;
int yearOfBirth;
};
• It would be better if all information related to date of
birth is unified into a single structure …
14
Nested Structures
struct dateOfBirth {
int day, month, year;
};
• … and the new structure is nested inside struct
student
struct student {
string name;
string adr;
float grade;
dateOfBirth birthDate;
};
15
Nested Structures
struct dateOfBirth {
int day, month, year;
};
• … and the new structure is nested inside struct
student
struct student {
…
dateOfBirth birthDate;
};
student a;
cin >> a.birthDate.day;
student b = {"Tom", "Cat st.", 1.1, {1,2,3}};
16
Case Study
Write a program that uses a structure to store the following data:
17
Member Name Description
Name Student name
Tests Pointer to an array of 𝑘 test scores
Average Average test score
• The program should keep a group of students
• It should ask the user how many test scores there are to be and
how many students there are
• It should then dynamically allocate an array of structures
• Each structure’s Tests member should point to a dynamically
allocated array that will hold the test scores
A list of students that are sorted by Average in descending order
should be displayed on the screen

Chapter 4 - Structures - Student.pdf - slides

  • 1.
  • 2.
    Introduction • Suppose youwant to store information about students in a class – Assuming that the class has 5 students • You might want to store their names, addresses, and grades 2
  • 3.
    Introduction string name[5] ={“Tom”, “Jerry”, “Pluto”, “Donald”, “Mickey”}; string adr[5] = {“12 Cat st.”, “34 Mouse st.”, “56 Dog st.”, “78 Duck st.”, “90 Mouse st.”}; float grade[5] = {1.1, 3.3, 5.5, 7.7, 9.9}; for (i = 0; i < 5; i++) cout << name[i] << adr[i] << grade[i]; 3
  • 4.
    Introduction for (i =0; i < 4; i++) for (j = i + 1; j < 5; j++) if (name[i] > name[j]) swap(name[i] > name[j]); for (i = 0; i < 5; i++) cout << name[i] << adr[i] << grade[i]; // {“Donald”,“Jerry”,“Mickey”, “Pluto”, “Tom”} // {1.1, 3.3, 5.5, 7.7, 9.9} 4
  • 5.
    C++ Structures • Astructure is a user-defined type • A single structure can hold items of more than one data type • How to define a structure type? struct <tag> { <type 0> <member/field name 0>; <type 1> <member/field name 1>; … }; 5
  • 6.
    C++ Structures struct student{ string name; string adr; float grade; }; student a, b, * ps, group[5]; 6
  • 7.
    Accessing Structure Members •The dot operator is used to access structure members <name of structure variable>.<member name> cout << "Enter the student's name: "; getline(cin, a.name); cout << "Enter the student's address: "; getline(cin, a.adr); cout << "Enter the student's grade: "; cin >> a.grade; cout << a.name << a.adr << a.grade; group[0] = a; cout << group[0].name << group[0].adr << group[0].grade; 7
  • 8.
    Structures as FunctionArguments/Return Values • Consider the following function prototypes: void sortData(student []); void inputItem(student &); student inputItem(); student * inputItem(); void outputItem(student); void outputItem(student *); 8
  • 9.
    Initializing a Structure •The members of a structure variable may be initialized with starting values student a = {"Tom", "12 Cat st.", 1.1}; student group[5] = { {"Tom", "12 Cat st.", 1.1}, {"Jerry", "34 Mouse st.", 3.3}, {"Pluto", "56 Dog st.", 5.5}, {"Donald", "78 Duct st.", 7.7}, {"Mickey", "90 Mouse st.", 9.9} }; 9
  • 10.
    Pointers to Structures •Similar to define pointers to built-in types, you may define pointers to structures student a, * p, * q, group[5]; p = &a; q = group; • In general, there are three ways to access structure members a.name p->name (*p).name 10
  • 11.
    Dynamically Allocating aStructure • You can use a structure pointer and the new operator to dynamically allocate a structure student * p; p = new student; getline(cin, p->name); getline(cin, p->adr); cin >> p->grade; …; delete p; • You can also dynamically allocate an array of structures student * group; group = new student [10]; getline(cin, group[5].name); cin >> group[5].grade; … delete [] group; 11
  • 12.
    Pointers As Members •Sometimes structures contain pointers as members struct ABC { int * ptr; }; ABC a, * p; • Consider several ways to access a pointer that is a member of a structure a.ptr ⇔ p->ptr ⇔ (*p).ptr *a.ptr ⇔ *p->ptr ⇔ *(*p).ptr 12
  • 13.
    Pointers As Members structABC { int * ptr; }; ABC a, b; a.ptr = new int [N]; b.ptr = new int [N]; a = b; delete [] a.ptr; delete [] b.ptr; // ??? 13
  • 14.
    Nested Structures • Considerthe following type definition struct student { string name; string adr; float grade; int dayOfBirth; int monthOfBirth; int yearOfBirth; }; • It would be better if all information related to date of birth is unified into a single structure … 14
  • 15.
    Nested Structures struct dateOfBirth{ int day, month, year; }; • … and the new structure is nested inside struct student struct student { string name; string adr; float grade; dateOfBirth birthDate; }; 15
  • 16.
    Nested Structures struct dateOfBirth{ int day, month, year; }; • … and the new structure is nested inside struct student struct student { … dateOfBirth birthDate; }; student a; cin >> a.birthDate.day; student b = {"Tom", "Cat st.", 1.1, {1,2,3}}; 16
  • 17.
    Case Study Write aprogram that uses a structure to store the following data: 17 Member Name Description Name Student name Tests Pointer to an array of 𝑘 test scores Average Average test score • The program should keep a group of students • It should ask the user how many test scores there are to be and how many students there are • It should then dynamically allocate an array of structures • Each structure’s Tests member should point to a dynamically allocated array that will hold the test scores A list of students that are sorted by Average in descending order should be displayed on the screen