Objectives In this chapter, you will:

Slides:



Advertisements
Similar presentations
Test 2 Jason Jenkins Lauren Sears Aaron Stricklin Brandon Wolfe Julie Alexander.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
1 Week 1304/07/2005Course ISM3230Dr. Simon Qiu  Learn how to create and manipulate enumeration type  Become aware of the typedef statement  Learn about.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
C++ Programming:. Program Design Including
Chapter 15: Operator Overloading
CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Chapter 8 Arrays and Strings
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Beginning C++ Through Game Programming, Second Edition
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: Simple Data Types, Namespaces, and the string Type.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
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.
Chapter 9: Value-Returning Functions
Chapter 13: Overloading and Templates
Chapter 7: User-Defined Functions II
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.
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.
Chapter 15: Overloading and Templates
User-Defined Functions
Chapter 14: Exception Handling
11/10/2018.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Data Types Chapter 8.
Review for Final Exam.
More On Enumeration Types
Chapter 7: User-Defined Functions II
Review for Final Exam.
Presentation transcript:

Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type

Objectives In this chapter, you will: Create and manipulate your own simple data type called the enumeration type Become familiar with the typedef statement Learn about the namespace mechanism Explore the string data type and various string functions to manipulate strings C++ Programming: Program Design Including Data Structures, Sixth Edition

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 enumeration type, you need: A name for the data type A set of values for the data type A set of operations on the values C++ Programming: Program Design Including Data Structures, Sixth Edition

Enumeration Type (cont’d.) You can specify the name and the values, but not the operations Syntax: value1, value2, … are identifiers called enumerators List specifies the ordering: value1 < value2 < value3 <... C++ Programming: Program Design Including Data Structures, Sixth Edition

Enumeration Type (cont’d.) The enumeration type is an ordered set of values Default value assigned to enumerators starts at 0 A value used in one enumeration type cannot be used by another in same block Same rules apply to enumeration types declared outside of any blocks C++ Programming: Program Design Including Data Structures, Sixth Edition

Enumeration Type (cont’d.) C++ Programming: Program Design Including Data Structures, Sixth Edition

Enumeration Type (cont’d.) C++ Programming: Program Design Including Data Structures, Sixth Edition

Declaring Variables Syntax: Example: Can declare variables such as: C++ Programming: Program Design Including Data Structures, Sixth Edition

Assignment Values can be stored in enumeration data types: popularSport = FOOTBALL; Stores FOOTBALL into popularSport C++ Programming: Program Design Including Data Structures, Sixth Edition

Operations on Enumeration Types No arithmetic operations are allowed on enumeration types : ++ and -- are illegal, too: Solution: use a static cast C++ Programming: Program Design Including Data Structures, Sixth Edition

Relational Operators An enumeration type is an ordered set of values: An enumeration type is an integral data type and can be used in loops: C++ Programming: Program Design Including Data Structures, Sixth Edition

Input /Output of Enumeration Types An enumeration type cannot be input/output (directly) Can input and output indirectly C++ Programming: Program Design Including Data Structures, Sixth Edition

Functions and Enumeration Types Enumeration types can be passed as parameters to functions either by value or by reference A function can return a value of the enumeration type C++ Programming: Program Design Including Data Structures, Sixth Edition

Declaring Variables When Defining the Enumeration Type Can declare variables of an enumeration type when you define an enumeration type: C++ Programming: Program Design Including Data Structures, Sixth Edition

Anonymous Data Types Anonymous type: values are directly specified in the declaration, with no type name Example: C++ Programming: Program Design Including Data Structures, Sixth Edition

Anonymous Data Types (cont’d.) Drawbacks: Cannot pass/return an anonymous type to/from a function Values used in one type can be used in another, but are treated differently: Best practices: to avoid confusion, define an enumeration type first, then declare variables C++ Programming: Program Design Including Data Structures, Sixth Edition

typedef Statement typedef statement: used to create synonyms or aliases to a data type Syntax: typedef does not create any new data types Only creates an alias to an existing data type C++ Programming: Program Design Including Data Structures, Sixth Edition

Namespaces ANSI/ISO standard C++ was officially approved in July 1998 Most recent compilers are compatible with ANSI/ISO standard C++ For the most part, standard C++ and ANSI/ISO standard C++ are the same However, ANSI/ISO Standard C++ has some features not available in Standard C++ C++ Programming: Program Design Including Data Structures, Sixth Edition

Namespaces (cont’d.) Global identifiers in a header file used in a program become global in the program Syntax error occurs if a program’s identifier has same name as a global identifier in the header file Same problem can occur with third-party libraries Common solution: third-party vendors begin their global identifiers with _ (underscore) Do not begin identifiers in your program with _ C++ Programming: Program Design Including Data Structures, Sixth Edition

Namespaces (cont’d.) ANSI/ISO Standard C++ attempts to solve this problem with the namespace mechanism Syntax: Where members consist of variable declarations, named constants, functions, or another namespace C++ Programming: Program Design Including Data Structures, Sixth Edition

Namespaces (cont’d.) C++ Programming: Program Design Including Data Structures, Sixth Edition

Namespaces (cont’d.) A namespace member has scope local to the namespace A namespace member can be accessed outside the namespace: C++ Programming: Program Design Including Data Structures, Sixth Edition

Namespaces (cont’d.) Examples: globalType::RATE using namespace globalType::printResult(); using globalType::RATE; After the using statement, it is not necessary to put the namespace_name:: before the namespace member Unless a namespace member and a global identifier or a block identifier have the same name C++ Programming: Program Design Including Data Structures, Sixth Edition

string Type To use data type string, a program must include the header file string A string is a sequence of 0 or more characters The first character is in position 0 The second character is in position 1, etc. Binary operator + performs the string concatenation operation Array subscript operator [] allows access to an individual character in a string C++ Programming: Program Design Including Data Structures, Sixth Edition

Additional string Operations C++ Programming: Program Design Including Data Structures, Sixth Edition

Example 7-18: swap Function C++ Programming: Program Design Including Data Structures, Sixth Edition

Summary Enumeration type: set of ordered values Reserved word enum creates an enumeration type No arithmetic operations are allowed on the enumeration type Relational operators can be used with enum values Enumeration type values cannot be input or output directly Enumeration types can be passed as parameters to functions by value or by reference C++ Programming: Program Design Including Data Structures, Sixth Edition

Summary (cont’d.) Anonymous type: a variable’s values are specified without any type name Reserved word typedef creates synonyms or aliases to previously defined data types The namespace mechanism is a feature of ANSI/ISO Standard C++ A namespace member is usually a named constant, variable, function, or another namespace Scope of a namespace member is local to namespace C++ Programming: Program Design Including Data Structures, Sixth Edition

Summary (cont’d.) using statement simplifies access to namespace members A string is a sequence of 0 or more characters Strings in C++ are enclosed in "" First character of a string is in position 0 In C++, [] is the array subscript operator C++ Programming: Program Design Including Data Structures, Sixth Edition