Structures putting data together.

Slides:



Advertisements
Similar presentations
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Advertisements

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
C Language.
CSE 303 Lecture 16 Multi-file (larger) programs
The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
Guide To UNIX Using Linux Third Edition
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Separating Definition & Implementation Headers and Comments.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
CS 261 – Data Structures Introduction to C Programming.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
THE PREPROCESSOR
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
TK1924 Program Design & Problem Solving Session 2011/2012
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Structures and Classes
Creating and Using Objects, Exceptions, Strings
Test 2 Review Outline.
What Is? function predefined, programmer-defined
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
11 Chapter Structured Data
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Separate Compilation and Namespaces
Chapter 13 - The Preprocessor
Classes Object-oriented programming: Example: Bank transactions
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Programmer-Defined Functions, Call-by-Value, Multiple Files Lab 5
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Pre-processor Directives
Introduction to Structured Data Types and Classes
Separate Compilation and Namespaces
Structures December 6, 2017.
Previous Lecture Review
7 Arrays.
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
Chapter 11: Structured Data.
C Preprocessor(CPP).
Separating Definition & Implementation
Learning Objectives Classes Constructors Principles of OOP
Introduction to Classes and Objects
Pointer to Structures Lesson xx
Structures putting data together.
Structured Data Types array union struct class.
Code Organization CSCE 121 J. Michael Moore.
C++ Compilation Model C++ is a compiled language
Objectives In this chapter, you will:
Standard Version of Starting Out with C++, 4th Edition
What Is? function predefined, programmer-defined
Introduction to Programming - 1
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
More C++ Classes Systems Programming.
Presentation transcript:

Structures putting data together

Structure Definitions aggregate construct allows to manipulate several data items as a single whole structures are an aggregate construct; what other aggregate constructs have we studied? example: a particular date has month, day and year structure definition: struct Date{ int month; int day; int year; }; note the semicolon at the end of the structure definition elements of the structure are called members or member variables structure definition is not executable – good candidate for a header file members of the same structure must have different names, different structures can contain members with the same name that is: the scope of member variable name is the structure definition

Preprocessor Directives (Review) each definition (e.g. global constant def.) can be encountered only once during compilation when definition is placed in a header file, it may be included multiple times header file must structured so it is safe in case of multiple inclusion; term – multiple inclusion protection mechanism - preprocessor directives #define name value note that substitution is textual problem: #define press 50+5 int myvar = press * 20; changes order of operations, do not use #define instead of global constants #ifdef name - true if name defined, #ifndef name - true if not #endif - completes #if header file myheader.h containing definitions usually has the following structure: #ifndef MYHEADER_H #define MYHEADER_H // text of the header file goes here #endif

Structure Variables when structure Date is defined it becomes a type; a structure variables can be declared as any other variable: Date today, birthday; each structure variable of Date contains three member variables programmer can refer to member variable by specifying structure variable name “dot” member variable name for example today contains: today.month today.day today.year member variables are of type specified in the structure definition a member variable can be used as any other (scalar) variable today.year=2001; today.day = birthday.day+1; cin >> today.month;

Assigning Value to Structure Variable initialization structure variable can be initialized at declaration: Date birthday={10, 31, 2035}; order of initialization corresponds to the order member of member variables in structure definition more values than variable members – error less values than variable members – the rest initialized to zero structure variable can be assigned the value of another structure variable of the same structure type Date deadline; deadline=today; structure variables cannot be directly compared if (deadline == today) // error how would you compare two structure variables?

Passing Structures as Parameters, Returning Structures structures can be passed by value and by reference, a function can return a structure: Date setDate(int m, int d, int y){ Date tmp; tmp.month=m; tmp.day=d; tmp.year=y; return tmp; } what does this code do? Date projectDue; projectDue=setDate(10,31,2035);

Complex Structures a member may be of basic type or of type structure substructure – member variable of type structure // example structure struct Example{ int a; string b; }; // example complex structure struct CompExample{ int c; Example d; // substructure if there is a declaration compExample me; how do you access substructure member variables?

Structures and Arrays a member may be an array struct ExampleWArray{ // complex structure definition int a; int b[5]; // member array }; ExampleWArray se; // declaring structure variable se.a = 123; se.b[3]=456; // accessing members an array of structures may be declared struct Example{ //structure string b; Example as[4]; // declaring array of structures as[3].a=123; as[3].b=”Hello”; // accessing members how do you access characters of string b? more complicated constructs (arrays of structures with substructures, etc.) are possible