1 Chapter 10 & 11 enum & Structured Types Dale/Weems.

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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Functions and Arrays Dale Roberts, Lecturer Computer.
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.
1 Techniques of Programming CSCI 131 Lecture 24 Structs.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
1 Chapter 12 Arrays Dale/Weems/Headington. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
1 Lecture 24 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter 6 Control Structures.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Lecture 25 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
C++ Programming:. Program Design Including
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.
1 Simple Data Types: Built-in and Use-Defined. 2 Chapter 10 Topics  Built-In Simple Types  Integral and Floating Point Data Types  Using Combined Assignment.
1 Structured Types, Data Abstraction and Classes.
1 Chapter 10 Simple Data Types: Built-In and User- Defined.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
1 Chapter 10 Simple Data Types: Built- In and User- Defined Dale/Weems.
Chapter 10: Records (structs)
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
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.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
User Defined Data Types - updated Chapter 10: User Defined Data Types Objectives: In this chapter you will learn about, Introduction Declaring.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Chapter 10 Simple Data Types: Built-In and User- Defined.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
Chapter 10 Simple Data Types: Built-In and User-Defined.
1 Data Structures and Algorithms Week 3 Data Abstraction: Part II Structured Types, and Classes.
Simple Data Types: Built-In and User-Defined
Dale/Weems/Headington
1 Chapter 11 Arrays. 2 Chapter 11 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const in Function.
1 Chapter 12-2 Arrays Dale/Weems. 2 Using Arrays as Arguments to Functions Generally, functions that work with arrays require 2 items of information n.
1 Chapter 12 Arrays. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const in Function.
Structures (aka records, structs) Textbook Chapter 11 sections:
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
1 Structural Types, Data Abstractions and Classes.
Structures and Enumerations Introduction Structure definitions Nested structure Referring and initializing structure elements Passing structures to a function.
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.
1 Chapter 9 Scope, Lifetime, and More on Functions.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Chapter 12 Arrays Dale/Weems. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const.
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 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
1 Structured Types, Data Abstraction and Classes.
Chapter 10 Simple Data Types: Built-In and User-Defined
LESSON 06.
User Defined Data Types
Dale/Weems/Headington
C++ Data Types and Data Abstractions
Chapter 10 Simple Data Types: Built-In and User-Defined
Structure, Unions, typedef and enumeration
C++ Data Types and Data Abstractions
C++ Data Types Simple Structured Address Integral Floating
C++ Simple Data Types Simple types Integral Floating
توابع در C++ قسمت اول اصول كامپيوتر 1.
Enumerations CS Fall 1999 Enumerations.
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Structures Structured Data types Data abstraction structs ---
Presentation transcript:

1 Chapter 10 & 11 enum & Structured Types Dale/Weems

2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long double

3 typedef statement l typedef creates an additional name for an already existing data type l Before bool type became part of ISO-ANSI C++, a Boolean type was simulated this way typedef int Boolean; const Boolean true = 1; const Boolean false = 0;. Boolean dataOK;. dataOK = true;

4 Enumeration Types l C++ allows creation of a new simple type by listing (enumerating) all the ordered values in the domain of the type EXAMPLE enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; name of new typelist of all possible values of this new type

5 enum Type Declaration enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; l The enum declaration creates a new programmer- defined type and lists all the possible values of that type--any valid C++ identifiers can be used as values l The listed values are ordered as listed; that is, JAN < FEB < MAR < APR, and so on l You must still declare variables of this type

6 Declaring enum Type Variables enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; MonthType thisMonth; // Declares 2 variables MonthType lastMonth; // of type MonthType lastMonth = OCT; // Assigns values thisMonth = NOV; // to these variables. lastMonth = thisMonth; thisMonth = DEC; 6

7 Storage of enum Type Variables enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; stored as 0 stored as 1 stored as 2 stored as 3 etc. stored as 11

8 Use Type Cast to Increment enum Type Variables enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; MonthType thisMonth; MonthType lastMonth; lastMonth = OCT; thisMonth = NOV; lastMonth = thisMonth; thisMonth = thisMonth++; // COMPILE ERROR ! thisMonth = MonthType(thisMonth + 1); // Uses type cast 8

9 More about enum Type Enumeration type can be used in a Switch statement for the switch expression and the case labels Stream I/O (using the insertion > operators) is not defined for enumeration types; functions can be written for this purpos Comparison of enum type values is defined using the 6 relational operators (, >=, ==, !=) An enum type can be the return type of a value- returning function in C++

10 MonthType thisMonth; switch (thisMonth) // Using enum type switch expression { case JAN : case FEB : case MAR : cout << “Winter quarter”; break; case APR : case MAY : case JUN : cout << “Spring quarter”; break; case JUL : case AUG : case SEP : cout << “Summer quarter”; break; case OCT : case NOV : case DEC : cout << “Fall quarter”; } 10

11 Using enum type Control Variable with for Loop enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; void WriteOutName (/* in */ MonthType); // Prototype. MonthType month; for (month = JAN; month <= DEC; month = MonthType (month + 1)) // Requires use of type cast to increment { WriteOutName (month); // Function call to perform output. } 11

12 void WriteOutName ( /* in */ MonthType month) // Prints out month name // Precondition: month is assigned // Postcondition: month name has been written out {switch (month) { case JAN : cout << “ January ”; break; case FEB : cout << “ February ”; break; case MAR : cout << “ March ”; break; case APR : cout << “ April ”; break; case MAY : cout << “ May ”; break; case JUN : cout << “ June ”; break; case JUL : cout << “ July ”; break; case AUG : cout << “ August ”; break; case SEP : cout << “ September ”; break; case OCT : cout << “ October ”; break; case NOV : cout << “ November ”; break; case DEC : cout << “ December ”; break; } 12

13 enum SchoolType {PRE_SCHOOL, ELEM_SCHOOL, MIDDLE_SCHOOL, HIGH_SCHOOL, COLLEGE };. SchoolType GetSchoolData (void) // Obtains information from keyboard to determine level // Postcondition: Return value == personal school level { SchoolType schoolLevel; int age; int lastGrade; cout << “Enter age : “; // Prompt for information cin >> age; Function with enum Type Return Value 13

14 if (age < 6) schoolLevel = PRE_SCHOOL; else { cout << “Enter last grade completed in school: “; cin >> lastGrade; if (lastGrade < 5) schoolLevel = ELEM_SCHOOL; else if (lastGrade < 8) schoolLevel = MIDDLE_SCHOOL; else if (lastGrade < 12) schoolLevel = HIGH_SCHOOL; else schoolLevel = COLLEGE; } return schoolLevel; // Return enum type value } 14

15 Structured Data Type A structured data type is a type in which each value is a collection of component items n The entire collection has a single name n Each component can be accessed individually n Used to bundle together related data of various types for convenient access under the same identifier For example...

16 thisAnimal 5000.id name “giant panda”.genus “Ailuropoda”.species “melanoluka”.country “China”.age 18.weight health Good

17 anotherAnimal 6000.id name “llama”.genus “Lama”.species “peruana”.country “Peru”.age 7.weight health Excellent

18 struct AnimalType enum HealthType { Poor, Fair, Good, Excellent }; struct AnimalType // Declares a struct data type { // does not allocate memory long id; string name; string genus; string species; struct members string country; int age; float weight; HealthType health; }; // Declare variables of AnimalType AnimalType thisAnimal; AnimalType anotherAnimal; 18

19 struct type Declaration SYNTAX struct TypeName // Does not allocate memory { MemberList }; MemberList SYNTAX DataType MemberName;.

20 struct type Declaration The struct declaration names a type and names the members of the struct It does not allocate memory for any variables of that type! You still need to declare your struct variables

21 More about struct type declarations Scope of a struct If the struct type declaration precedes all functions, it will be visible throughout the rest of the file If it is placed within a function, only that function can use it It is common to place struct type declarations in a (.h) header file and #include that file It is possible for members of different struct types to have the same identifiers; also a non-struct variable may have the same identifier as a structure member

22 Accessing struct Members Dot (period) is the member selection operator After the struct type declaration, the various members can be used in your program only when they are preceded by a struct variable name and a dot EXAMPLES thisAnimal.weight anotherAnimal.country

23 Operations on struct Members The type of the member determines the allowable operations thisAnimal.age = 18; thisAnimal.id = ; cin >> thisAnimal.weight; getline (cin, thisAnimal.species); thisAnimal.name = “giant panda”; thisAnimal.genus[0] = toupper(thisAnimal.genus[0]); thisAnimal.age++;

24 Aggregate Operation An aggregation operation is an operation on a data structure as a whole, as opposed to an operation on an individual component of the data structure

25 Aggregate struct Operations l Operations valid on struct type variables are  Assignment to another struct variable of the same type  Pass as an argument (by value or by reference)  Return as value of a function l I/O, arithmetic, and comparisons of entire struct variables are NOT ALLOWED!

26 Aggregate struct Operations anotherAnimal = thisAnimal; // Assignment WriteOut(thisAnimal); // Value parameter ChangeWeightAndAge(thisAnimal); // Reference parameter thisAnimal = GetAnimalData(); // Function return value

27 void WriteOut( /* in */ AnimalType thisAnimal) // Prints out values of all members of thisAnimal // Precondition: all members of thisAnimal are assigned // Postcondition:all members have been written out { cout << “ID # “ << thisAnimal.id << thisAnimal.name << endl; cout << thisAnimal.genus << thisAnimal.species << endl; cout << thisAnimal.country << endl; cout << thisAnimal.age << “ years “ << endl; cout << thisAnimal.weight << “ lbs. “ << endl; cout << “General health : “; WriteWord (thisAnimal.health); }

28 void ChangeAge(/* inout */ AnimalType& thisAnimal) // Adds 1 to age // Precondition: thisAnimal.age is assigned // Postcondition:thisAnimal.age == // + 1 { thisAnimal.age++; } Passing a struct Type by Reference

29 AnimalType GetAnimalData () // Obtains all information about an animal from keyboard // Postcondition: // Return value == AnimalType members entered at kbd { AnimalType thisAnimal; char response; do { // Have user enter members until they are correct. } while (response != ‘Y’); return thisAnimal; } 29

30 Hierarchical Structures l The type of a struct member can be another struct type l This is called nested or hierarchical structures l Hierarchical structures are very useful when there is much detailed information in each record For example...

31 struct MachineRec Information about each machine in a shop contains: an idNumber, a written description, the purchase date, the cost, and a history (including failure rate, number of days down, and date of last service)