91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction Structures are somewhat like an array in that.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,
Basic Elements of C++ Chapter 2.
Chapter 6: Functions.
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.
Constructors and Other Tools Version 1.0 Topics Constructors & Destructors Composition const Parameter Modifier const objects const functions In-line.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
18. DECLARATIONS.
Learners Support Publications Classes and Objects.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
CPS120: Introduction to Computer Science Decision Making in Programs.
Agenda Object Oriented Programming Reading: Chapter 14.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
C++ Classes and Data Structures Jeffrey S. Childs
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University CONTROL STRUCTURES Simple If: if (boolean exp) { statements.
Data Structures & Algorithms
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 If’s – The Basic Idea “Program” for hubby: take out garbage.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
1 CS161 Introduction to Computer Science Topic #15.
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.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
REEM ALMOTIRI Information Technology Department Majmaah University.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University 1 Exercise 10 The study of thermodynamics involves a lot of.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Chapter Structured Data 11. Combining Data into Structures 11.2.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
User-Written Functions
Student Book An Introduction
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company. 1
Structures putting data together.
Classes and Objects.
Java Programming Language
Presentation transcript:

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction Structures are somewhat like an array in that both consist of a collection of elements. The elements are names instead of being numbered, however, and need not all be of the same type. int [0] [1] [2] [3] [4] int double.student_no.year.program_code.GPA Array: all elements are of the same type elements are identified by position Structure: elements can be of different types elements are identified by name

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 2 Defining A Structure Type Introducing structures into a program is a two step process. First a new type must be defined. Then variables of this type can be declared. struct student_data { int student_no; int year; int program_code; double GPA; }; // Note the semi-colon here!! The above declaration defines a new type called “student_data”. Variables of this type consist of four elements named “student_no” and so on. Now, in addition to the “built-in” types (int, double, char, bool, long), we have a new one that we’ve defined ourselves. Structure definitions are normally placed at the start of a program, so makng them globally accessible (the usual “scope” rules apply).

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 3 Declaring Structure Variables Once a structure type has been defined, it may be used in declaring variables. All always, a variable consists essentially of a type name followed by one or more variable names. int a; // type = “int”, variable name = “a” student_data s1, s2; // type = “student_data” // variable names = “s1”, “s2” The above declarations give us a single “int” variable called “a” and two “student_data” variables called “s1” and s2”. int doubleint double int a s1 s2

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 4 Structure Operations Structure variables may be assigned to other structure variables of the same type. s1 = s2; // legal - each element of s1 gets the // value of the corresponding element // of s2 Otherwise structures are like arrays in that one can’t do very much with them (unless we define the operations ourselves, which is beyond this course…). cout << s1; // illegal cin >> s2; // illegal if (s1 == s2) { // illegal... s1 = s1 + s2; // illegal

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 5 The Dot Operator The individual elements of a structure variable may be accessed by using the ‘.’ (dot) operator. structure_var. name the name of the element to be accessed. a hard and fast name must be used - nice as it might be, a string variables are not permitted. a structure variable (or a reference to such a variable) Once an element has been selected, it may be used in any of the ways a simple variable of the same type might be used (exactly as with arrays and the [] operator). Thus s1.GPA may be used wherever a double variable is permitted. s1.GPA = 3.9; if (...) { // caught cheating s1.GPA -= 1.0;

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 6 Structures and Functions (1) Structures may be passed to functions. Either call by value or call by reference may be used, but call by reference is more typical, and using call by value may produce a “structure passed by value” warning message. Call by reference is preferred because it is more efficient, especially when dealing the large structures. // the structure type is defined in the “global scope” struct point { double lat, lng; // note short form }; // in this case call be reference is essential because the // function is supposed to modify the variable supplied void read_position (point &pnt) {... } void main (void) { point pnt_1, pmt_2;... read_position (pnt1);...

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 7 Structures and Functions (2) When call by reference is used only for efficiency, and the function does not modify the structure, it is a good idea to precede the parameter declation with “const”. This works exactly as it does with strings. The “const” indicates that the function does not modify the structure supplied, and the compiler will reject any attempts to do so. // computing the distance between two points does not // involve modifying either of them double find_distance (const point &pnt_1, const point &pnt_2) {... // if we put “pnt_1.lat = 0;” here, we’d get a compiler // error message (“cannot modify const object”). In this particular case, because the structure is so small, call by value would also be a reasonable choice. double find_distance (point pnt_1, point pnt_2) { See sample program dstnce3.cpp.

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 8 Structures And Arrays The elements of an array may be structures (or other arrays…) and the elements of a structure may be arrays (or other structures). Whenever we have a structure, we can use the ‘.’ operator, and whenever we have an array, we can use the ‘[]’ operator. Assume: struct sample { int a [4]; double b[5]; point p; // point is as defined a few slides back… }; sample x, y[100]; // x is a structure of type “sample” // y is an array of such structues x.a is an array of integers x.a[1] is the second element of this array y[0] is a structure of type sample y[0].p is a structure of type point y[0].p.lat is a double etc.

Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 9 Brokerage Example A broker who buys and sells bonds needs a program to help him keep track of his inventory. The fact that we’re dealing with bonds doesn’t really matter. If you prefer, you can think in terms of, say, an ice cream dealer who buys and sells different types of ice cream. Bonds (as simplified for this example) are characterized by a maturity date (month and year) and a coupon rate (in per cent). If two bonds have the same maturity date and the same coupon rate, they are identical. Every new kind of bond added to the inventory is to be assigned a unique id. The program is to support three operations. The broker can add bonds to his inventory (necessary inputs: maturity date, coupon rate, number of units), list the bonds in the inventory (no inputs required) he has available, and sell bonds (necessary inputs: id of bond, number of units). The code which handles the list operation should behave reasonably if there are no bonds in the inventory. The sell operation has four possible outcomes. Either there is no bond having the specified id (error message) there are not enough units available (error message, no other action), the sale is possible but leaves some units unsold, or the sale completely wipes out the units available. See sample program broker.cpp.