1 Chapter 10 Various Topics User defined Types Enumerated Types Type Casting Syntactic Sugar Type Coercion.

Slides:



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

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Functions and Arrays Dale Roberts, Lecturer Computer.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
HOW TO MAKE A CLIMATE GRAPH CLIMATE GRAPHING ASSIGNMENT PT.2.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
Chapter 6 Control Structures.
CS150 Introduction to Computer Science 1
 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
Basic Elements of C++ Chapter 2.
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 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.
Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 –HW3 is posted Questions?
1 Lecture 3 Part 1 Functions with math and randomness.
Chapter 2 part #4 Operator
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
1 Chapter 10 Simple Data Types: Built-In and User- Defined.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
1 Chapter 10 Simple Data Types: Built- In and User- Defined Dale/Weems.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
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.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
User Defined Data Types - updated Chapter 10: User Defined Data Types Objectives: In this chapter you will learn about, Introduction Declaring.
1 Chapter 10 Simple Data Types: Built-In and User- Defined.
Chapter 10 Simple Data Types: Built-In and User-Defined.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
Simple Data Types: Built-In and User-Defined
Dale/Weems/Headington
Chapter 3 More Flow of Control Goals: To analyze the use of Boolean expressions To analyze the use of Boolean expressions To introduce the notion of enumerated.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
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 Operations Chapter 4 & Section Expressions As we've seen, in a C++ program, any finite sequence of objects and operations that combine to produce.
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.
1 Chapter 10 & 11 enum & Structured Types Dale/Weems.
Chapter 10 Simple Data Types: Built-In and User-Defined
Chapter Topics The Basics of a C++ Program Data Types
Jan 2016 Solar Lunar Data.
Chapter 7: Expressions and Assignment Statements
Chapter 10 Simple Data Types: Built-In and User-Defined
Structure, Unions, typedef and enumeration
Basic Elements of C++.
Chapter 7: Expressions and Assignment Statements
Payroll Calendar Fiscal Year
Baltimore.
solve the following problem...
C++ Simple Data Types Simple types Integral Floating
Basic Elements of C++ Chapter 2.
Average Monthly Temperature and Rainfall
توابع در C++ قسمت اول اصول كامپيوتر 1.
Chapter 7 Additional Control Structures
Enumerations CS Fall 1999 Enumerations.
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
FY 2019 Close Schedule Bi-Weekly Payroll governs close schedule
QUIZ.
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Operator King Saud University
Presentation transcript:

1 Chapter 10 Various Topics User defined Types Enumerated Types Type Casting Syntactic Sugar Type Coercion

2 C++ Has Combined Assignment Operators int age ; cin >> age ; Write a statement to add 3 to age. age = age + 3 ; OR, age + = 3 ;

3 Write a statement to subtract 10 from weight int weight ; cin >> weight ; weight = weight - 10 ; OR, weight - = 10 ;

4 Write a statement to divide money by 5.0 float money ; cin >> money ; money = money / 5.0 ; OR, money / = 5.0 ;

5 Write a statement to double profits float profits ; cin >> profits ; profits = profits * 2.0 ; OR, profits * = 2.0 ;

6 Write a statement to raise cost 15% float cost; cin >> cost; cost = cost + cost *.15 ; OR, cost = 1.15 * cost; OR, cost * = 1.15 ;

7 Type Cast Operator The C++ cast operator is used to explicitly request a type conversion. The cast operation has two forms. int intVar; float floatVar = ; intVar = int ( floatVar ) ; // functional notation, OR intVar = ( int ) floatVar ; // prefix notation uses ( ) floatVar intVar

8 typedef statement typedef creates an additional name for an already existing data type 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 ;

9 Enumeration Types 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

10 enum Type Declaration enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } ; 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 the listed values are ordered as listed. That is, JAN < FEB < MAR < APR, and so on you must still declare variables of this type

11 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 ;

12 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

13 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

14 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. Instead, functions can be written for this purpose. 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++. SOME EXAMPLES...

15 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” ; }

16 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. }

17 void WriteOutName ( /* in */ MonthType month ) // Prints out calendar name corresponding to month // Precondition: month is assigned // Postcondition: calendar name for month 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 ; }

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

19 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 }

20 Implicit type coercion occurs... whenever values of different data types are used in: 1. arithmetic and relational expressions 2. assignment operations 3. parameter passage 4. returning a function value with return (from a value-returning function) TWO RULES APPLY...

21 Promotion (or widening) in C++... is the conversion of a value from a “lower” type to a “higher” type--specifically, for mixed type expressions: Step 1. Each char, short, bool, or enumeration value is promoted to int. If both operands are now int, the result is an int expression. Step 2. If Step 1 leaves a mixed-type expression, the following precedence of types is used (from lowest to highest): int, unsigned int, long, unsigned long, float, double, long double The value of the operand of “lower” type is promoted to that of the “higher” type. For an arithmetic expression, the result is an expression of that higher type. For a relational expression, the result is always bool (true or false).

22 is the conversion of a value from a “higher” type to a “lower” type, and may cause loss of information FOR EXAMPLE, temperature number float temperature = 98.6 ; int number ; number = temperature ; // demotion occurs Demotion (or narrowing)...