CIS 22B - Notes for Thursday, 10/22

  Announcements and Reminders

Recording

Structs

A struct is 
  • an abstract data type
  • a composite type
  • a user-defined type
  • almost the exact same thing as a class in C++ (but we'll get to that later)
Structs contain members, accessible through the . (dot) operator, and maybe through the -> (arrow) operator.

Example:

struct something        // struct declaration
{
   int a;               // members
   float b;
   char c;
};

...

something var;          // you can also say "struct something var;"

// assign the members of the something variable (object)
var.a = 19;
var.b = 3.14;
var.c = 'x';

cout << var.a << ' ' << var.b << ' ' << var.c;    // prints 19 3.14 x
...

something* ps;         // ps is a pointer to something
ps = &var;             // assign the address of a struct variable to a pointer

cout << (*ps).a;       // prints 19
cout << ps->a;         // prints 19

struct initialization

Arrays of structs

Structs with arrays

The tm struct

Example

enum (enumerated type)

  • is a user-defined type
  • is an integer type with a discrete number of values
  • can be converted to an int (or a short or a long or an unsigned type)
  • does not automatically act like an int, not int arithmetic
  • an int can be cast to an enum
  • can be used as an array index, or the condition expression of a switch
  • adheres to scoping rules
  • can be anonymous
Example

Textbook Examples

Pr11-9.cpp - enum values used to index an array

Pr11-10.cpp - enum values used as an iteration value in a for loop

Pr11-11.cpp - enum values used in a switch condition

  unions

  • like a struct, it contains members
  • members occupy the same area of memory
  • the size of the union is the size of its largest member
  • unions are used to represent mutually exclusive characteristics
  • adheres to scoping rules
  • can be anonymous
Example 1
Example 2

Midterm - What to expect

Topics

  • file I/O
  • arrays - 1D, 2D
  • sorting, searching
  • passing an array to a function, passing one row of a 2D array to a function
  • pointers
  • dynamic memory allocation
  • c-strings
  • cctype functions
  • string class
  • structs
  • enums
  • unions

Midterm format

  • Midterm is online and timed
  • Written problems
  • Examples
  1. Write a function that converts a string into an int.  Assume the int is between 10 and 99.  Do not use the atoi() or the stoi() function.
  2. Write a function prototype for problem 1.
  3. Write a function call for the function you defined in problem 1.
  4. Write a function that converts an int between 10 and 99 into a string.
  5. Write a function prototype for problem 4.
  6. Write a function call for function you defined in problem 4.
  7. Declare a 2-dimensional array with 30 rows and 40 columns
  8. Write a function that takes the 2-dimensional array argument from problem 7 and assigns the even rows random even numbers between 1 and 100 and assigns the odd rows random odd numbers between 1 and 100.
  9. Write a function prototype for problem 8.
  10. Write a function call for function you defined in problem 8.
  11. What's the output from the following ...
  • How to study for the midterm.