Structures. Lecture 15 COP 3014 Spring April 16, 2018

Similar documents
Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Structures. Lecture 5. Structures

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

Basics of Java Programming

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

Object Oriented Programming COP3330 / CGS5409

Functions. Lecture 6 COP 3014 Spring February 11, 2018

a data type is Types

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

Chapter 11: Structured Data

Lecture 3 Tao Wang 1

Compiling and Running a C Program in Unix

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Tin học cơ sở 4$ Structures!

LECTURE 11 STRUCTURED DATA

typedef int Array[10]; String name; Array ages;

C++ for Java Programmers

Chapter-14 STRUCTURES

Object-Oriented Principles and Practice / C++

Chapter 11: Structured Data

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Introduction to Computer Science Midterm 3 Fall, Points

BITG 1233: STRUCTURED DATA. LECTURE 11 (Sem 2, 17/18)

Software Design and Analysis for Engineers

this Pointer, Constant Functions, Static Data Members, and Static Member Functions this Pointer (11.1) Example of this pointer

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Object Oriented Programming COP3330 / CGS5409

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Chapter 10 C Structures, Unions, Bit Manipulations

Chapter 11: Abstract Data Types. Abstraction and Data Types. Combining Data into Structures 8/23/2014. Abstract Data Types

Pointers in C/C++ 1 Memory Addresses 2

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Lecture 5 Tao Wang 1

Ch. 12: Operator Overloading

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Classes and Data Abstraction. Topic 5

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

BITG 1113: POINTER LECTURE 12

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

CS 247: Software Engineering Principles. ADT Design

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CSC 215 Structures. Dr. Achraf El Allali

Fundamental of Programming (C)

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Stepwise Refinement. Lecture 12 COP 3014 Spring February 2, 2017

Object Oriented Design

Fundamentals of Programming Session 24

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

aggregate data types member functions cis15 advanced programming techniques, using c++ lecture # II.1

CMPT 354: Database System I. Lecture 2. Relational Model

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

Type Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Constant Member Functions

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Ch 6-1. Structures. March 30, Prof. Young-Tak Kim

Introduction to Programming

Data Types (C/C++) Structures

A structure is an aggregate data type which contains a fixed number of heterogeneous components.

IS 0020 Program Design and Software Tools

More on Functions. Lecture 7 COP 3014 Spring February 11, 2018

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Microcontroller Systems. ELET 3232 Topic 8: Structures, Arrays, & Pointers

String Objects: The string class library

Object Oriented Programming with Java

Protection Levels and Constructors The 'const' Keyword

.:: UNIT 1 ::. INTRODUCTION TO ALGORITHM & DATA STRUCTURES

F4104 ALGORITHM & DATA STRUCTURE

UNIT-2 Introduction to C++

that focuses on building models of

Syntax and Variables

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Introduction to Computers and Programming

Pointers, Dynamic Data, and Reference Types

Preview 11/1/2017. Constant Objects and Member Functions. Constant Objects and Member Functions. Constant Objects and Member Functions

Prof. Carl Schultheiss MS, PE. CLASS NOTES Lecture 12

C Pointers. 6th April 2017 Giulio Picierro

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

PIC 10A Objects/Classes

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology


Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

COMP322 - Introduction to C++

Variables and Constants

CSCI 1370 APRIL 26, 2017

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter Lab 8 - Structures

Variables, Memory and Pointers


CS304 Object Oriented Programming Final Term

Transcription:

Structures Lecture 15 COP 3014 Spring 2018 April 16, 2018

Motivation We have plenty of simple types for storing single items like numbers, characters. But is this really enough for storing more complex things, like patient records, address books, tables, etc.? It would be easier if we had mechanisms for building up more complex storage items that could be accessed with single variable names Compound Storage there are some built-in ways to encapsulate multiple pieces of data under one name Array we already know about this one. Indexed collections, and all items are the same type Structure keyword struct gives us another way to encapsulate multiple data items into one unit. In this case, items do not have to be the same type Structures are good for building records like database records, or records in a file.

What is a Structure? A structure is a collection of data elements, encapsulated into one unit. A structure definition is like a blueprint for the structure. It takes up no storage space itself it just specifies what variables of this structure type will look like An actual structure variable is like a box with multiple data fields inside of it. Consider the idea of a student database. One student record contains multiple items of information (name, address, SSN, GPA, etc) Properties of a structure: internal elements may be of various data types order of elements is arbitrary (no indexing, like with arrays) Fixed size, based on the combined sizes of the internal elements

Creating Structure definitions and variables Structure Definitions The basic format of a structure definition is: struct structurename { // data elements in the structure }; struct is a keyword The data elements inside are declared as normal variables. structurename becomes a new type. Note that the two examples below are both just blueprints specifying what will be in corresponding structure variables if and when we create them. By themselves, these definitions above are not variables and do not take up storage Fraction and Student can now be used as new type names

Example /* A structure representing the parts of a fraction (a rational number) */ struct Fraction { int num; // the numerator of the fraction int denom; // the denominator of the fraction }; /* A structure representing a record in a student database */ struct Student { char fname[20]; // first name char lname[20]; // last name int socsecnumber; // social security number double gpa; // grade point average };

Structure variables To create an actual structure variable, use the structure s name as a type, and declare a variable from it. Format: structurename variablename; Variations on this format include the usual forms for creating arrays and pointers, and the comma-separated list for multiple variables Examples Fraction f1; // f1 is now a Fraction Fraction flist[10]; // an array of Fraction //structures Fraction * fptr; // a pointer to a Fraction //structure Student stu1; // a Student structure variable Student mathclass[10]; // an array of 10 Students Student s1, s2, s3; // three Student variables

Legal variations in declaration syntax The definition of a structure and the creation of variables can be combined into a single declaration, as well. Just list the variables after the structure definition block (the blueprint), and before the semi-colon: struct structurename { // data elements in the structure } variable1, variable2,..., variablen; Example: struct Fraction { int num; // the numerator of the fraction int denom; // the denominator of the fraction } f1, flist[10], *fptr; // variable, array, and pointer created

Legal variations in declaration syntax In fact, if you only want structure variables, but don t plan to re-use the structure type (i.e. the blueprint), you don t even need a structure name: struct // note: no structure NAME given { int num; int denom; } f1, f2, f3; // three variables representing fractions Of course, the advantage of giving a structure definition a name is that it is reusable. It can be used to create structure variables at any point later on in a program, separate from the definition block.

Legal variations in declaration syntax You can even declare structures as variables inside of other structure defintions (of different types): struct Date // a structure to represent a date { int month; int day; int year; }; struct Employee // a structure to represent an employee of a company { char firstname[20]; char lastname[20]; Date hiredate; Date birthdate; };

Using structures Once a structure variable is created, how do we use it? How do we access its internal variables (often known as its members)? To access the contents of a structure, we use the dot-operator. Format: structvariablename.datavariablename Example, using the fraction structure: Fraction f1, f2; f1.num = 4; // set f1 s numerator to 4 f1.denom = 5; // set f1 s denominator to 5 f2.num = 3; // set f2 s numerator to 3 f2.denom = 10; // set f2 s denominator to 10 cout << f1.num << / << f1.denom; // prints 4/5 cout << f2.num << / << f2.denom; // prints 3/10

Example, using the student structure: Student slist[10]; // array of 10 students // set first student s data: (John Smith, SSN: 123456789, GPA: 3.75) strcpy(slist[0].fname, "John"); strcpy(slist[0].lname, "Smith"); slist[0].socsecnumber = 123456789; slist[0].gpa = 3.75; // assume there s more code here that initializes other students // This loop prints all 10 students -- their names and their GPA cout << fixed << setprecision(2); for (int i = 0; i < 10; i++) { cout << slist[i].fname << << slist[i].lname << << slist[i].gpa << \n ; }

A shortcut for initializing structs While we can certainly initialize each variable in a structure separately, we can use an initializer list on the declaration line, too This is similar to what we saw with arrays This is only usable on the declaration line (like with arrays) The initializer set should contain the struct contents in the same order that they appear in the struct definition Example (using the fraction structure): Fraction f1 = 3, 5; //initialize num=3, denom=5 // This would be the same as doing the following: f1.num = 3; f1.denom = 5; Example (using the student structure): Student s1 = {"John", "Smith", 123456789, 3.75}; Student s2 = {"Alice", "Jones", 123123123, 2.66};

If we have a pointer to a structure, things are a little trickier: Fraction f1; // a fraction structure Fraction *fptr; // pointer to a fraction fptr = &f1; // fptr now points to f1 f1.num = 3; // this is legal, of course fptr.num = 10; // how about this? NO! ILLEGAL // cannot put a pointer on the left side // of the dot-operator Remember that to get to the target of a pointer, we dereference it. The target of fptr is *fptr. So how about this? *fptr.num = 10; // closer, but still NO (not quite) The problem with this is that the dot-operator has higher precedence, so this would be interpreted as: *(fptr.num) = 10; // cannot put a pointer on the left of the dot

Accessing internal data using a pointer to a structure But if we use parentheses to force the dereference to happen first, then it works: (*fptr).num = 10; // YES! Alternative operator for pointers: While the above example works, it s a little cumbersome to have to use the parentheses and the dereference operator all the time. So there is a special operator for use with pointers to structures. It is the arrow operator: pointertostruct -> datavariable Example: Fraction * fptr; // pointer to a fraction // assume this has been pointed at a valid target fptr->num = 10; // set fraction s numerator to 10 fptr->denom = 11; // denominator set to 11 cout << fptr->num << / << fptr->denom; // prints: 10/11

Accessing members of nested structures Earlier, we saw an example of a structure variable used within another structure definition struct Date // Date is now a type name { int month; int day; int year; }; // so that "Date" is the type name struct Employee { char firstname[20]; char lastname[20]; Date hiredate; Date birthdate; };

Accessing members of nested structures Here s an example of initializing all the data elements for one employee variable: Employee emp; // emp is an employee variable // Set the name to "Alice Jones" strcpy(emp.firstname, "Alice"); strcpy(emp.lastname, "Jones"); // set the hire date to March 14, 2001 emp.hiredate.month = 3; emp.hiredate.day = 14; emp.hiredate.year = 2001; // sets the birth date to Sept 15, 1972 emp.birthdate.month = 9; emp.birthdate.day = 15; emp.birthdate.year = 1972;

Accessing members of nested structures Here s an example of an employee initialization using our shortcut initializer form: Employee emp2 = { "John", "Smith", {6, 10, 2003}, {2, 19, 1981} }; // John Smith, whose birthday is Feb 19, 1981, was hired on June 10, 2003

Structures and the assignment operator With regular primitive types we have a wide variety of operations available, including assignment, comparisons, arithmetic, etc. Most of these operations would NOT make sense on structures. Arithmetic and comparisons, for example: Student s1, s2; s1 = s1 + s2; // ILLEGAL! // How would we add two students, anyway? if (s1 < s2) // ILLEGAL. What would this mean? // yadda yadda Using the assignment operator on structures IS legal, as long as they are the same type. Example (using previous struct definitions): Student s1, s2; Fraction f1, f2; s1 = s2; // LEGAL. Copies contents of s2 into s1 f1 = f2; // LEGAL. Copies f2 into f1

Structures and the assignment operator Note that in the above example, the two assignment statements are equivalent to doing the following: // these 4 lines are equivalent to s1 = s2; strcpy(s1.fname, s2.fname); strcpy(s1.lname, s2.lname); s1.socsecnumber = s2.socsecnumber; s1.gpa = s2.gpa; //these 2 lines are equivalent to f1 = f2; f1.num = f2.num; f1.denom = f2.denom; Clearly, direct assignment between entire structures is easier, if a full copy of the whole thing is the desired result!

Passing structures into and out of functions Just like a variable of a basic type, a structure can be passed into functions, and a structure can be returned from a function. To use structures in functions, use structname as the parameter type, or as a return type, on a function declaration Examples (assuming struct definition examples from previous page): // function that passes a structure variable as a //parameter void PrintStudent(Student s); // function that passes in structure variables // and returns a struct Fraction Add(Fraction f1, Fraction f2);

Pass by value, reference, address Just like with regular varaibles, structures can be passed by value or by reference, or a pointer to a structure can be passed (i.e. pass by address) If just a plain structure variable is passed, as in the above examples, it s pass by value. A copy of the structure is made To pass by reference, use the & on the structure type, just as with regular data types To pass by address, use pointers to structures as the parameters and/or return As with pointers to the built-in types, you can use const to ensure a function cannot change the target of a pointer It s often a GOOD idea to pass structures to and from functions by address or by reference structures are compound data, usually larger than plain atomic variables Pass-by-value means copying a structure. NOT copying is desirable for efficiency, especially if the structure is very large

Example // function that passes a pointer to student //structure as a parameter void GetStudentData(Student* s); // function that passes in structures by const // reference, and returns a struct by value Fraction Add(const Fraction& f1, const Fraction& f2); //function that uses const on a structure pointer // parameter. This function could take in an array // of Students, or the address of one student. void PrintStudents(const Student* s); // or, this prototype is equivalent to the one above void PrintStudents(const Student s[]);