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

Slides:



Advertisements
Similar presentations
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Advertisements

Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Chapter Objectives You should be able to describe: Assignment Pointers as Class Members Additional Class Features Common Programming Errors.
Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
 2003 Prentice Hall, Inc. All rights reserved Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior)
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
Arrays One-Dimensional initialize & display Arrays as Arguments Two-dimensional initialize & display Part I.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
Java and C++, The Difference An introduction Unit - 00.
Structured Data Types array array union union struct struct class class.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 10: Records ( struct s)
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
1 STRUCTURES AND POINTERS. 2 A VARIABLE OF THIS COMPOSITE TYPE CAN HAVE MORE THAN ONE VALUE, GROUPED TOGETHER TO DESCRIBE AN ENTITY. THE COMPONENTS OF.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
計算機程式語言 Lecture 13-1 國立臺灣大學生物機電系 13 Structures.
Module 4: Structures ITEI222 Advanced Programming.
Structure A structure is a collection of variables of different data type under one name. It is a derived data type. e.g. struct employee {int empno; char.
C to C++ © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
CSCI-383 Object-Oriented Programming & Design Lecture 5.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
11 Introduction to Object Oriented Programming (Continued) Cats.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
is a specific set of data values along with a set of operations on those values. Review * Data type.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Struct Data Type in C++ What Are Structures?
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Data Types Storage Size Domain of all possible values Operations 1.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
11 Introduction to Object Oriented Programming (Continued) Cats.
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
CO1401 Program Design and Implementation
Structures - Part II aggregate operations arrays of type struct
Dynamic Memory Allocation Reference Variables
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
More About Data Types & Functions
Pointers & Functions.
Structured Data Types array union struct class.
Struct Data Type in C++ What Are Structures?
Fundamental Programming
Pointers & Functions.
CS31 Discussion 1D Winter18: week 6
Structures Structured Data types Data abstraction structs ---
CS31 Discussion 1H Fall18: week 9
Presentation transcript:

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

Aggregate Operations OperationArrays Structs I/ONo ( except strings )No AssignmentNo Yes ArithmeticNo No ComparisonNo No Parameter pass.Ref. only Either value or ref. Funct. return value No Yes

Arrays of Structures struct Salaried { char dept[5]; int salary; int vac_days; }; // array of 20 elements, each of type Salaried Salaried emp[20];

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;

Arrays of Structures struct Payroll { int id; char name[15]; double payrate; }; // an array of 3 records of type Payroll Payroll employee[3];

Arrays of Structures // 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 for(ndx = 0; ndx < 3; ndx++) cout << ‘\n’ << employee[ndx].id << setw(20) << employee[ndx].name << setw(20) << employee[ndx].payrate;

Arrays of Structures struct Payroll { int id; char name[15]; double payrate; }; * // prototypes void loadarray(Payroll [3]); void showarray(Payroll [3]);

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 }; // calls loadarray(employee);// calls showarray(employee); // for formatting cout << endl<<endl;// for formatting // end main() } // end main()

Arrays of Structures // load array - data typically entered via file input Payroll void loadarray(Payroll 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; } }

Arrays of Structures Payroll void showarray(Payroll 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; }

In Class Assign. 3 Elementat. num at. massdensity tungsten (W) sulfur (S) carbon (C) Write a program which: a. creates an array of structures b. uses a global constant for the array size c. contains functions to load and to display d. format similar to the above chart

In Class Assign. 3-ans. #include using namespace std; struct Element {char name[20]; char symbol; int at_num; double at_mass; double density; }; const int ARR_SIZE = 5; // used pointerto cycle thru array void getData(Element *); // used pointer to cycle thru array // See filearray.cc sent on 4/30 void showData(Element *); // 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 getData(&FiveElements[3]); // 1 st call gets address of 4 th struct showData(FiveElements); // 2nd call gets beginning address of array // end main() cout << ‘\n’; } // end main()

Nested Structures struct Date {int month; int day; int year; }; struct Vital_Data {char name[15]; char dept[10]; int ID; // Date must be previously defined Date birth; // Date must be previously defined double payrate; }; *

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

Nested Structures 1. Write the cin statements for the department and the birthday. 2. Write the cout statements for the department and the birthday.

Nested Structures void 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].dept; personnel[i].ID personnel[i].payrate; personnel[i].birth.year ; personnel[i].birth.month personnel[i].birth.day * * * *

Formatted Output ID#namedepartment birthday payrate 1234Lawrence personnel 10/ 5/ Newman shipping 2/29/ setiosflags(ios::fixed | ios::right); cout << setiosflags(ios::fixed | ios::right); for(int i = 0; i < 2 ; i++) setprecision(0) cout << '\n' << setw(5) << 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 setprecision(2) << setw(7) << setprecision(2) << personnel[i].payrate;

Structure vs. Class By default By default: struct have public member variables class have private member variables

Class Class syntax: class Classname { private: list of private variable declarations public: list of function prototypes };

Class An example: class Date { private: int day; int month; int year; public: (member functions) };

Imagination is more important than knowledge. than knowledge. Albert Einstein