ch6 Structs Pointers Recursion
ch6 Structs Pointers Recursion
6.1 Structures
Enumeration Types
C++ allow the user to define new simple types by listing (enumerating) the
literal values that make up the domain of the type.
enum Days {SUN, MON, TUE, WED, THU, FRI, SAT};
Examples:
1
outputs the numbers 0, 1, 2, 3, 4, 5, 6; not the literals.
Examples:
enum Days {SUN, MON, TUE, WED, THU, FRI, SAT};
Days today;
int weekday;
. . .
switch (today)
{
case SUN:
case SAT:
weekday = FALSE;
break;
default:
weekday = TRUE;
}
Records
struct StudentRec
{
NameString firstName;
2
NameString lastName;
long studentId;
float gpa;
float programAvg;
float examAvg;
float labAvg;
int finalExam;
GradeType coursegrade;
}
StudentRec aStudent;
StudentRec aPupil;
aStudent.courseGrade =
aStudent.examAvg * 0.30 +
aStudent.programAvg * 0.25 +
aStudent.labAvg * 0.30 +
aStudent.finalExam * 0.15;
StudentRec aStudent =
{
"Ophelia",
"Payne",
12345678,
0.725,
54.3,
62.0,
25.4,
76.0,
D
};
The members firstName and lastName are arrays. To access their
individual components, specify the entire name of the field followed by the
index of the array member enclosed in square brackets.
aStudent.firstName[2] = 'i';
The following table compares aggregate operations for arrays and structs.
3
Comparison No No
Parameter passage By reference only By value or by reference
Unlike arrays, the contents of a struct can be assigned to another variable
of the same type.
aPupil = aStudent;
struct StudentRec
{
NameString firstName;
NameString lastName;
long studentId;
float gpa;
float programAvg;
float examAvg;
float labAvg;
int finalExam;
GradeType coursegrade;
} aStudent, aPupil;
Arrays of Records
struct StudentRec
{
NameString firstName;
NameString lastName;
long studentId;
float gpa;
float programAvg;
float examAvg;
float labAvg;
int finalExam;
GradeType coursegrade;
}
StudentRec gradeBook[MAX_STUDENTS];
To access individual members of the array, you must specify both the
array index and the field of the struct.
4
gradeBook[2].finalExam = 96.0;
In this lecture we introduce the notion of a pointer, which gives the programmer a
greater level of control over the way the program allocates and de-allocates
memory during its execution.
Declaring Pointers
int *number_ptr;
Given a particular data type, such as "int", we can write assignment statements
involving both ordinary variables and pointer variables of this data type using the
dereference operator "*" and the (complementary) address-of operator "&".
Roughly speaking, "*" means "the variable located at the address", and "&"
means "the address of the variable". We can illustrate the uses of these
operators with a simple example program:
#include <iostream>
using namespace std;
int main()
{
int *ptr_a, *ptr_b;
int num_c = 4, num_d = 7;
cout << *ptr_a << " " << *ptr_b << "\n";
cout << *ptr_a << " " << *ptr_b << "\n";
5
cout << *ptr_a << " " << *ptr_b << "\n";
cout << num_c << " " << *&*&*&num_c << "\n";
return 0;
}
Program 6.1.
44
47
77
77
Diagramatically, the state of the program after the assignments at lines 10 and
11 is:
Dynamic variables are "created" using the reserved word "new", and "destroyed"
(thus freeing-up memory for other uses) using the reserved word "delete". Below
is a program analogous to Program 6.1, which illustrates the use of these
operations:
#include <iostream>
using namespace std;
int main()
{
int *ptr_a, *ptr_b; /* LINE 7 */
6
ptr_a = new int; /* LINE 9 */
*ptr_a = 4;
ptr_b = ptr_a; /* LINE 11 */
cout << *ptr_a << " " << *ptr_b << "\n";
cout << *ptr_a << " " << *ptr_b << "\n";
delete ptr_a;
ptr_a = ptr_b; /* LINE 21 */
cout << *ptr_a << " " << *ptr_b << "\n";
return 0;
}
Program 6.2
44
47
77
7
Finally, after the "delete" statement in lines 25, the program state returns to:
The above programs demonstrate the use of pointers. But why bother with a
pointer when you already have a variable with access to that value? Pointers are
used, most often, for three tasks:
6.3 Recursion
What is Recursion?
A Recursive Call is a function call in which the function being called is the
same as the one making the call.
or
Recursive Functions
// Exponentiation program
#include <iostream.h>
int main()
{
int number;
int exponent;
return 0;
}
9
for ( ; n > 0; n--)
pow *= x;
n! = 1 * 2 * . . . * n
// Factorial Function
int Factorial ( int n )
{
if ( n < 2)
return 1;
return n * Factorial(n - 1);
}
10