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
The
tm struct
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
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
Midterm - What to expectTopics- 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
- 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.
- Write a function prototype for problem 1.
- Write a function call for the function you defined in problem 1.
- Write a function that converts an int between 10 and 99 into a string.
- Write a function prototype for problem 4.
- Write a function call for function you defined in problem 4.
- Declare a 2-dimensional array with 30 rows and 40 columns
- 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.
- Write a function prototype for problem 8.
- Write a function call for function you defined in problem 8.
- What's the output from the following ...
- How to study for the midterm.
|