Objectives In this chapter, you will:

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Test 2 Jason Jenkins Lauren Sears Aaron Stricklin Brandon Wolfe Julie Alexander.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Kernighan/Ritchie: Kelley/Pohl:
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 10.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
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 8 Arrays and Strings
Guide To UNIX Using Linux Third Edition
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
Basic Elements of C++ Chapter 2.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 8 Arrays and Strings
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
1 C++ Syntax and Semantics, and the Program Development Process.
Beginning C++ Through Game Programming, Second Edition
C++ Programming: Basic Elements of C++.
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.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: Simple Data Types, Namespaces, and the string Type.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
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
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter Topics The Basics of a C++ Program Data Types
Chapter 13: Overloading and Templates
Chapter 7: User-Defined Functions II
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.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to C++ Systems Programming.
Basic Elements of C++.
C Short Overview Lembit Jürimägi.
User-Defined Functions
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
11/10/2018.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Data Types Chapter 8.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Operator Overloading; String and Array Objects
Chapter 2: Basic Elements of Java
Expressions and Assignment
Chapter 6: User-Defined Functions I
Objectives In this chapter, you will:
Classes, Objects and Methods
C Language B. DHIVYA 17PCA140 II MCA.
Presentation transcript:

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

Objectives In this chapter, you will: Learn how to create and manipulate your own simple data type - called the enumeration type Explore how the assignment statement, and arithmetic and relational operators work with enum types Learn how to use for loops with enum types Learn how to input data into an enum type C++ Programming: Program Design Including Data Structures, Seventh Edition

Objectives (cont’d.) Learn how to output data stored in an enum type Explore how to write functions to process enum types Learn how to declare variables when defining the enumeration type Become familiar with anonymous types Become familiar with the typedef statement C++ Programming: Program Design Including Data Structures, Seventh Edition

Objectives (cont’d.) Learn about the namespace mechanism Explore the string data type, and learn how to use string functions to manipulate strings C++ Programming: Program Design Including Data Structures, Seventh 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 By providing data types, you specify what values are legal and tell the user what kinds of operations are allowed on those values. The system thus provides you with built-in checks against errors. The data types that you have worked with until now were mostly int, bool, char, and double. Even though these data types are sufficient to solve just about any problem, situations occur when these data types are not adequate to solve a particular problem. C++ provides a mechanism for users to create their own data types, which greatly enhances the flexibility of the programming language. Tonight you will learn how to create your own simple data types, known as the enumeration types. C++ Programming: Program Design Including Data Structures, Seventh 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, Seventh 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, Seventh Edition

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

Enumeration Type (cont’d.) Enumeration type in C++ is an integral type. This will help clarify some of the syntax requirements in later sections. Fundamental types in C++ are divided into three categories: integral, floating point, and void. Integral types are capable of handling whole numbers.  C++ Programming: Program Design Including Data Structures, Seventh Edition

Declaring Variables The Syntax for declaring the variable type enum: The Statement example: Can declare variables such as: Once a data type is defined, you can declare variables of that type. The syntax for declaring variables of an enum type is the same as before: C++ Programming: Program Design Including Data Structures, Seventh Edition

Assignment Once a variable is declared, you can store values in it. Values can be stored in enumeration data types: popularSport = FOOTBALL; Stores FOOTBALL into popularSport C++ Programming: Program Design Including Data Structures, Seventh Edition

Operations on Enumeration Types Arithmetic operations not allowed on enumeration types ++ and -- are illegal, too: Solution: use a static cast Also, the increment and decrement operations are not allowed on enumeration types. So the following statements are illegal: Suppose you want to increment the value of popularSport by . You can use the cast operator as follows: C++ Programming: Program Design Including Data Structures, Seventh 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: Because an enumeration is an ordered set of values, the relational operators can be used with the enumeration type. Once again, suppose you have the enumeration typesports and the variables popularSport and mySport as defined earlier. Relational Operators: There are following relational operators supported by C++ language Assume variable A holds 10 and variable B holds 20, then: Show Examples Operator Description Example == Checks if the values of two operands are equal or not, if yes then condition becomes true.(A == B) is not true.!= Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. Recall that the enumeration type is an integral type and that, using the cast operator (that is, type name), you can increment, decrement, and compare the values of the enumeration type. Therefore, you can use these enumeration types in loops. SupposemySport is a variable as declared earlier. Consider the following for loop: C++ Programming: Program Design Including Data Structures, Seventh Edition

Input /Output of Enumeration Types An enumeration type cannot be input/output (directly) Can input and output indirectly Because input and output are defined only for built-in data types such as int, char,double, and so on, the enumeration type can be neither input nor output (directly). However, you can input and output enumeration indirectly.  C++ Programming: Program Design Including Data Structures, Seventh 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, Seventh Edition

Declaring Variables When Defining the Enumeration Type Can declare variables of an enumeration type when you define an enumeration type: Defines an enumeration type, grades, and declares a variable courseGrade of typegrades. C++ Programming: Program Design Including Data Structures, Seventh Edition

Anonymous Data Types Anonymous type: values are directly specified in the declaration, with no type name Example: A data type wherein you directly specify values in the variable declaration with no type name is called an anonymous type. The following statement creates an anonymous type: This statement specifies the values and declares a variable mySport, but no name is given to the data type. C++ Programming: Program Design Including Data Structures, Seventh 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 Creating an anonymous type, however, has drawbacks. First, because there is no name for the type, you cannot pass an anonymous type as a parameter to a function, and a function cannot return an anonymous type value. Second, values used in one anonymous type can be used in another anonymous type, but variables of those types are treated differently.  C++ Programming: Program Design Including Data Structures, Seventh 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 In C++, you can create synonyms or aliases to a previously defined data type by using the typedef statement. The general syntax of the typedef statement is: C++ Programming: Program Design Including Data Structures, Seventh 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++ When a header file, such as iostream, is included in a program, the global identifiers in the header file also become the global identifiers in the program. Therefore, if a global identifier in a program has the same name as one of the global identifiers in the header file, the compiler generates a syntax error (such as “identifier redefined”). The same problem can occur if a program uses third-party libraries. C++ Programming: Program Design Including Data Structures, Seventh 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, Seventh 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, Seventh Edition

Namespaces (cont’d.) C++ Programming: Program Design Including Data Structures, Seventh 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, Seventh 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, Seventh 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 The data type string is a programmer-defined type and is not part of the C++ language; the C++ standard library supplies it. Before using the data type string, the program must include the header file string, as follows: C++ Programming: Program Design Including Data Structures, Seventh Edition

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

Example 7-18: swap Function C++ Programming: Program Design Including Data Structures, Seventh 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, Seventh 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, Seventh 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, Seventh Edition