Chapter 11 Structured Data

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Accessing struct members via pointer variables

*-Must use () to dereference pointer variable * EX: cout<< (*stuPtr).studentID; *-Can use struct pointer operator to eliminate () and use clearer notation* -> EX: cout << stuPtr*->* studentID;

Accessing Structure Members

*Used (.) dot operator* to refer to members of struct variables EX: getline(cin, student1.name); student1.gpa=3.75;

Display a Stuct Variable

*must display each field of a struct separately, using dot (.) operator* EX: cout << bill.studentname << endl; cout << bill.studentid << endl;

Enumerated Data Types

- is a programmer defined data type. -It consists of values known as enumerators, which represent integer constants.

Unions

-*All members share a single memory location, and only one member of the union can be used at a time* -Declared using union, otherwise the same as struct

Anonymous Union

-A union without a union tag: union { ... }; -Must use static if declared outside of a function -Allocates memory at declaration time -Can refer to members directly without dot operator -Uses only one memory location

Arrays of Structures

-Can be defined as arrays -Items in struct can be accessed using dot(.) notation EX: const int num_students=10; student stuList[num_students]; cout<< stuList.[5].studentID;

Returning a Structure from a Function

-Function must define a local structure, for use with return statement -need prototype, call and struct

Structs as Function Arguments

-May pass members of struct variables to functions EX: computeGPA(stu.gpa); -May pass entire struct variable to functions EX: showData(stu);

Struct

-Must have a ; after closing {} -struct name must be in CAPS -**struct declaration doesn't allocate memory/create variable** -*It creates a new datatype* EX: struct payRoll <-- new datatype { int empNumber; string name; } payRoll deptHead; <-Defining new variable with payRoll datatype

EX: enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY };

-The identifiers are the days, and they represent the values that belong to the Day data type. -not strings

what is an enumerator?

-Think of it as an integer named constant -Internally, the compiler assigns integer values to the enumerators, beginning at 0. MONDAY=0 TUESDAY=1 WEDNESDAY=2 etc etc code will output integer values cout << MONDAY << endl; displays 0

Notes for struct in Function Argument

-Using *value parameters for struct can slow down* program -Using a *reference parameter* will speed up program, but function *may change data in struct* -Using *const* reference parameters allows read-only access to reference parameters, doesn't waste space and speed increases

Combining Data in Structures

-allows multiple variables to be grouped together struct Employee <-(struct name) { defined variable; int payrate; };

ADTs

-is a data type created by programmer and is composed of one or more primitive data types. (bool, char, short int, int, long, float , double etc)

Pointers to Structures

-struct variable has an address -pointers to struct are variables that can hold the address of a struct EX: student *stuPtr -Can use & operator to assign address EX: stuPtr=&stu1; -can be a function parameter

Abstract Data Types (ADTs)

-values that can be stored -a definition that captures *general characteristics w/out details* EX: dog is abstract term= type of animal -captures essence of what all dogs are w/out specify detailed characteristics of particular type

Initializing a Structure

-variables can be initialized when defined -can be initialized member-by-member after defined EX: s.name="joan"; -cannot skip over members -*cannot initialize in struct declaration b/c struct doesn't allocate memory*

Assigning an integer to an enum Variable

-you must *cast* the integer: workDay = static_cast<Day>(3); -can assign enum to an int variable int x; x=THURSDAY; x now equals 3

Nested structures

Can contain another stuct as a member -*Use dot(.) operator to access in from struct* EX: struct PersonInfo {string name, address, city; }; struct student { int studentID; PersonInfo pData; double gpa; };

QUIZ: Before a structure variable can be created, the structure must be _________.

declared

The _________ operator allows you to access structure members.

dot (.)

A structure variable may not be a member of another structure.

false

All the members of a union may be used simultaneously.

false

An entire structure may not be passed to a function as an argument.

false

In a structure variable's initialization list, you do not have to provide initializers for all the members.

false

The contents of a structure variable can be displayed by passing the structure variable to the cout object.

false

You may not define pointers to unions.

false

Comparing Struct variables

if (student[0].gpa==student[1].gpa)

The variables declared inside a structure declaration are called _________.

members

A(n) _________ is required after the closing brace of a structure declaration.

semicolon (;)

EX of Returning struct from a Function

struct circle { double, radius, diameter, area; }; // function prototype circle getInfo(); int main() { circle c; c= getInfo(); c.area=PI x pow(c.radius, 2) cout << "area:" << c.area<< endl; return 0; } circle getInfo() { circle tempcircle; // temporary struct variable // store circle data in temp variable cin>> tempcircle.diameter; //return temp variable *return tempcircle;* }

A structure declaration does not define a variable.

true

If an anonymous union is defined globally (outside all functions), it must be declared static.

true

When a function returns a structure, it is always necessary for the function to have a local structure variable to hold the member values that are to be returned.

true

EX of const reference parameter struct struct Inventory { int partNum; string desciption; int onHand; double price; };

void showItem( const Inventory &p) { cout << "part name:" << p.partNum<< endl; cout<< "description:" << p.description << endl; }


Set pelajaran terkait

(4.3) Role based access control (RBAC)

View Set

Chapter 15 - Respiratory Emergencies

View Set

Qualified plans, and federal Tax considerations for life insurance and annuities

View Set

PY 205- Research Methods in Psychology - Final Study Guide Ch. 6-10

View Set