9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 ©1995-2002 Barnette ND & McQuain WD 1 Data Types data type:a collection of.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Functions and Arrays Dale Roberts, Lecturer Computer.
C Language.
1 C++ Syntax and Semantics The Development Process.
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
1 Chapter 10 Various Topics User defined Types Enumerated Types Type Casting Syntactic Sugar Type Coercion.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 –HW3 is posted Questions?
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
A1 Visual C++.NET Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Quick Introduction The following.
Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
CSC 270 – Survey of Programming Languages C++ Lecture 1 : C++ As A Better C.
Chapter 05 (Part III) Control Statements: Part II.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
Organizing Heterogeneous Data Arrays allow a programmer to organize lists of values that are all of the same type (homogeneous). But we are often faced.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
1 CS161 Introduction to Computer Science Topic #8.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
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.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (23 left – as of last night). Are you able.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Iteration pass (or iteration)-one.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
1 Chapter 10 & 11 enum & Structured Types Dale/Weems.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Jan 2016 Solar Lunar Data.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Structure, Unions, typedef and enumeration
Basic Elements of C++.
C++ Simple Data Types Simple types Integral Floating
Basic Elements of C++ Chapter 2.
C Structures, Unions, and Enumerations
توابع در C++ قسمت اول اصول كامپيوتر 1.
C Structures, Unions, Bit Manipulations and Enumerations
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Introduction to C++ Programming
Enumerations CS Fall 1999 Enumerations.
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
C Structures, Unions, Bit Manipulations and Enumerations
QUIZ.
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Fundamental Programming
CISC/CMPE320 - Prof. McLeod
Presentation transcript:

9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Data Types data type:a collection of values and the definition of one or more operations that can be performed on those values C++ includes a variety of built-in or base data types: short, int, long, float, double, char, etc. The values are ordered and atomic. C++ supports several mechanisms for aggregate data types: arrays, structures, classes. These allow complex combinations of other types as single entities. C++ also supports other mechanisms that allow programmers to define their own custom data types: enum types and typedef s.

9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 2 Enumerated Types enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; enum Season {WINTER, SPRING, SUMMER, FALL}; enum Hemisphere {NORTH, SOUTH, EAST, WEST}; Month Mon; Season Period; Hemisphere Region;... if (Mon == JAN && Region == NORTH) Period = WINTER; An enumerated type is defined by giving a name for the type and then giving a list of labels, which are the only values that a variable of that type is allowed to have. Enumerated types allow the creation of specialized types that support the use of meaningful labels within a program. They promote code readability with very little overhead in memory or runtime cost. An enumerated type is allowed to have up to 256 values and a variable of an enumerated type will occupy one byte of memory. It is an error for the same label to be listed as a value for two different enumerated types.

9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 3 Designing with Enumerated Types Enumerated types provide a mechanism for the abstraction of real world entities. Enumerated type variables do not contain the character string of the value. The internal representation uses integer values; it is bad practice to rely on those values. Since the labels are logically constant values it is common to express them using all capital letters, just as for named constants. Enumerated type values cannot be read or (usefully) written directly. Enumerated type variables and values can be used in relational comparisons, in assignments and in switch statements as selectors and case labels. Enumerated type variables can be passed as parameters and used as the return value of a function. Good use of enumerated data types make the program more readable by providing a way to incorporate meaningful labels (thus easier to debug) and they help in self- documenting the program.

9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 4 Integer Parity Check enum ParityType {EVEN, ODD}; // file-scoped type definition ParityType FindParity(int N); int main() { int k; ParityType kParity; cout << "Please enter an integer: "; // get an integer cin >> k; kParity = FindParity(k); // determine its parity if (kParity == EVEN) // can’t usefully print cout << "That was even." << endl; // the value of kParity else // directly cout << "Hmmm... that was odd." << endl; return 0; } ParityType FindParity(int N) { if (N % 2 == 0) return EVEN; else return ODD; }

9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 5 Other Uses of enum Types string parityToString(ParityType Parity) { const int NUMPARITYVALUES = 2; const string parityLabel[NUMPARITYVALUES] = {"even", "odd"}; if ( int(Parity) >= NUMPARITYVALUES ) { return "unknown"; } return parityLabel[Parity]; } Since an enumerated type is represented in hardware by integer values, enum variables may be used as switch selectors and as array indices: User input strings can be represented internally using an enumerated type: enum editCommand {INSERT, DELETE, MARKBEGIN, MARKEND,..., EXIT, EDITUNKNOWN};... editCommand getCommand(ifstream& In) { string userEntry; getline(In, userEntry, '\t'); if ( userEntry == "insert" ) return INSERT; if ( userEntry == "delete" ) return DELETE;... return EDITUNKNOWN; }

9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 6 typedef Statements typedef ; Typedefs allow the programmer to map existing data types to a problem-specific names. Good use of typedefs can improve data organization. Correct application of typedefs aids in the production of self-documenting code. … but no useful additional type-checking is enabled. The typedef statement allows a programmer to define custom types by creating an "alias" for an existing type: For example: typedef string PlaceName; typedef int Vector3D[3]; void Reverse(Vector3D X) { X[0] = -X[0]; X[1] = -X[1]; X[2] = -X[2]; }