structures - part ii aggregate operations arrays of type struct nested structures

21
Structures - Part II aggregate operations arrays of type struct nested structures compared to classes

Upload: hyman

Post on 12-Jan-2016

48 views

Category:

Documents


7 download

DESCRIPTION

Structures - Part II aggregate operations arrays of type struct nested structures compared to classes. Aggregate Operations. OperationArraysStructs I/ONo ( except strings )No AssignmentNo Yes ArithmeticNoNo ComparisonNoNo - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Structures - Part II aggregate operations arrays of type struct nested structures

Structures - Part II

aggregate operations

arrays of type struct

nested structures

compared to classes

Page 2: Structures - Part II aggregate operations arrays of type struct nested structures

Aggregate Operations

Operation Arrays Structs

I/O No (except strings) No

Assignment No Yes

Arithmetic No No

Comparison No NoParameter pass. Ref. only Either

value or ref.

Funct. return value No Yes

Page 3: Structures - Part II aggregate operations arrays of type struct nested structures

Arrays of Structures

struct Salaried{ char dept[5];

int salary; int vac_days;

};

// array of 20 elements, each of type // array of 20 elements, each of type SalariedSalariedSalaried emp[20];

Page 4: Structures - Part II aggregate operations arrays of type struct nested structures

Arrays of Structures

emp[0].dept = “Purc”;emp[0].salary = 34560;emp[0].vac_days = 14;...emp[2].salary = 32100;emp[2].dept = “Ship”;emp[2].vac_days = 10;

…emp[19].dept = “Acct”;emp[19].salary = 22500;emp[19].vac_days = 12;

Page 5: Structures - Part II aggregate operations arrays of type struct nested structures

Arrays of Structures

struct Payroll{

int id;char name[15];double payrate;

};

// an array of 3 records of type Payroll// an array of 3 records of type PayrollPayroll employee[3];

Page 6: Structures - Part II aggregate operations arrays of type struct nested structures

Arrays of Structures// load array -- there are other ways to load the array// load array -- there are other ways to load the array

Payroll employee[3] = { {11, “Begay”, 7.25}, {12, “Gioseffi”, 6.50}, {13, “Marra”, 9.00} };

// display array// display array

for(ndx = 0; ndx < 3; ndx++)cout << ‘\n’ << employee[ndx].id << setw(20) << employee[ndx].name << setw(20) << employee[ndx].payrate;

Page 7: Structures - Part II aggregate operations arrays of type struct nested structures

Arrays of Structures

struct Payroll{ int id; char name[15]; double payrate; };

*

// prototypesvoid loadarray(Payroll [3]); void showarray(Payroll [3]);

Page 8: Structures - Part II aggregate operations arrays of type struct nested structures

Arrays of Structures

void main(){ //declare array of 3 records of type Payroll//declare array of 3 records of type Payroll // and initialize the first record// and initialize the first record Payroll employee[3] = { 11, "Begay", 7.25 };

loadarray(employee); // calls// calls showarray(employee);

cout << endl<<endl; // for formatting// for formatting} // end main()// end main()

Page 9: Structures - Part II aggregate operations arrays of type struct nested structures

Arrays of Structures

// load array - data typically entered via file input// load array - data typically entered via file inputvoid loadarray(PayrollPayroll staff[3])

{ // begin at 1 because [0] already entered// begin at 1 because [0] already entered for(int ndx = 1; ndx < 3; ndx++) { cout << "Enter the ID, name, and pay rate: "; cin >> staff[ndx].id >> staff[ndx].name >> staff[ndx].payrate; cout << endl; }}

Page 10: Structures - Part II aggregate operations arrays of type struct nested structures

Arrays of Structures

void showarray(PayrollPayroll staff[3]){ cout << setiosflags(ios::fixed) << setprecision(2);

for(int ndx = 0; ndx < 3; ndx++) cout << '\n' << setw(5) << staff[ndx].id << setw(13) << staff[ndx].name << setw(10) << staff[ndx].payrate;}

Page 11: Structures - Part II aggregate operations arrays of type struct nested structures

In Class Assign. 3Elementat. num at. mass densitytungsten (W) 74 183.850 19.300sulfur (S) 16 32.064 2.07carbon (C) 6 12.011 2.260

9. Write a program which:a. creates an array of structures b. uses a global constant for the array sizec. contains functions to load and to displayd. format similar to the above chart

Page 12: Structures - Part II aggregate operations arrays of type struct nested structures

In Class Assign. 3-ans.#include<iostream>#include<iomanip>using namespace std;struct Element{ char name[20];

char symbol; int at_num; double at_mass; double density; };

const int ARR_SIZE = 5;

void getData(Element *); // used pointer// used pointer to cycle thru arrayto cycle thru arrayvoid showData(Element *); // See filearray.cc sent on 4/30// See filearray.cc sent on 4/30

void main(){ //declare array - type Element//declare array - type Element Element FiveElements[5];// first 3 records are initialized in main// first 3 records are initialized in main getData(&FiveElements[3]); // 1// 1stst call gets address of 4 call gets address of 4thth struct struct showData(FiveElements);// 2nd call gets beginning address of array// 2nd call gets beginning address of array cout << ‘\n’; } // end main()// end main()

Page 13: Structures - Part II aggregate operations arrays of type struct nested structures

Nested Structuresstruct Date{ int month;

int day;int year; };

struct Vital_Data{ char name[15];

char dept[10];int ID;Date birth; // Date must be // Date must be previouslypreviously defined defineddouble payrate;

};*

Page 14: Structures - Part II aggregate operations arrays of type struct nested structures

Nested StructuresVital_Data Courtney;Vital_Data Courtney; // declaration of an object// declaration of an object// assignments of data to an object// assignments of data to an object strcpy (Courtney.name,“Lawrence”);strcpy (Courtney.dept,“personnel”);Courtney.ID = 1234;Courtney.birth.month = 10;// birth is a struct// birth is a structCourtney.birth.day = 25; // note: two // note: two periodsperiodsCourtney.birth.year = 77; // individually assigned// individually assignedCourtney.payrate = 12.75;Vital_Data Personnel[2]; // an array of structs// an array of structs// this assignment syntax works on our Unix compiler// this assignment syntax works on our Unix compiler

Page 15: Structures - Part II aggregate operations arrays of type struct nested structures

Nested Structures

1. Write the cin statements for thedepartment and the birthday.

2. Write the cout statements for thedepartment and the birthday.

Page 16: Structures - Part II aggregate operations arrays of type struct nested structures

Nested Structuresvoid loadarray(vital_Data personnel[2]) { for(int i = 0; i < 2 ; i++) { cout << "\nEnter the name: "; cin >> cout << "Enter the department: "; cin >> cout << "Enter the id# and the payrate: "; cin >> >> cout << "Enter the birth date (dd mm yy) "; cin >> >> >> } }

personnel[i].name;personnel[i].name;

personnel[i].dept;personnel[i].dept;

personnel[i].IDpersonnel[i].ID personnel[i].payrate;personnel[i].payrate;

personnel[i].birth.yearpersonnel[i].birth.year;;personnel[i].birth.monthpersonnel[i].birth.monthpersonnel[i].birth.daypersonnel[i].birth.day

* * ** *

Page 17: Structures - Part II aggregate operations arrays of type struct nested structures

Formatted Output ID# name department birthday payrate1234 Lawrence personnel 10/ 5/77 12.75 765 Newman shipping 2/29/59 13.11

cout << setiosflags(ios::fixed | ios::right);setiosflags(ios::fixed | ios::right); for(int i = 0; i < 2 ; i++) cout << '\n' << setw(5) << setprecision(0)setprecision(0) << personnel[i].ID << setw(12) << personnel[i].name << setw(11) << personnel[i].dept << setw(5) << personnel[i].birth.day <<'/' << setw(2) << personnel[i].birth.month <<'/' << setw(2) << personnel[i].birth.year << setw(7) << setprecision(2)setprecision(2) << personnel[i].payrate;

Page 18: Structures - Part II aggregate operations arrays of type struct nested structures

Structure vs. Class

By defaultBy default:

struct have public member variables

class have private member variables

Page 19: Structures - Part II aggregate operations arrays of type struct nested structures

Class

Class syntax:Class syntax:

class Classname{

private:list of private variable declarations

public:list of function prototypes

};

Page 20: Structures - Part II aggregate operations arrays of type struct nested structures

Class

An example:An example:class Date{

private:int day;int month;int year;

public:(member functions)

};

Page 21: Structures - Part II aggregate operations arrays of type struct nested structures

Imagination Imagination

is more importantis more important

than knowledge.than knowledge.

Albert EinsteinAlbert Einstein