Structures putting data together.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
CSE 303 Lecture 16 Multi-file (larger) programs
The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
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.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Guide To UNIX Using Linux Third Edition
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
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.
Separating Definition & Implementation Headers and Comments.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
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.
15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.
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?
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
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
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
L what is a void-function? l what is a boolean function? l is it possible for a function to have no parameters? l what is program stack? function frame?
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
Test 2 Review Outline.
What Is? function predefined, programmer-defined
Programmer Defined Types and Classes
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?
Objectives In this chapter, you will:
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.
Separate Compilation and Namespaces
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
CS Computer Science IB: Object Oriented Programming
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
Using local variable without initialization is an error.
Introduction to Structured Data Types and Classes
Learning Objectives Structures Structure types
Separate Compilation and Namespaces
Structures putting data together.
Previous Lecture Review
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,
C Preprocessor(CPP).
Separating Definition & Implementation
Introduction to Classes and Objects
C++ Compilation Model C++ is a compiled language
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Lecture 5: Macros, Namespace
Objectives In this chapter, you will:
What Is? function predefined, programmer-defined
Class rational part2.
Introduction to Programming - 1
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
ITM 352 Functions.
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: bank’s certificate of deposit (CD) - has balance (int), rate (int) and months until maturity (int) structure definition: struct CDAacct{ int balance; int rate; int term; }; 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 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 CDAcct is defined, structure variables can be declared as any other variables: CDAcct myacct, youracct; each structure variable of CDAcct contains three member variables programmer can refer to member variable by specifying structure variable name “dot” member variable name for example myacct contains: myacct.balance myacct.rate myacct.term member variables are of type specified in the structure definition member variables can be used as any other (scalar) variables myacct.balance=1000; myacct.rate = youracct.rate+1; cin >> myacct.term;

Assigning Values to Structure Variables initialization structure variable can be initialized: CDAcct myacct={1000, 9, 12}; 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 0 structure variable can be assigned the value of another structure variable of the same structure type CDAcct youracct; youracct=myacct; structure variables cannot be directly compared if (youracct == myacct) // error

Passing Structures as Parameters, Returning Structures structures can be passed by value and by reference, a function can return a structure: CDAccount setCD(int b, int r, int t){ CDAccount tmp; tmp.balance=b; tmp.rate=r; tmp.term=t; return tmp; }

Complex Structures a member may be of basic type or of type structure (referred to as substructure): // 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 we 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 we access characters of string b? more complicated constructs (arrays of structures with substructures, etc.) are possible