0% found this document useful (0 votes)
3 views30 pages

Lecture03-Part I

Uploaded by

Yunus Gk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views30 pages

Lecture03-Part I

Uploaded by

Yunus Gk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Lecture 03

Part I: User-Defined Simple Data Types, Namespaces, and the string Type

C++ Programming: Program Design Including Data Structures, Eighth Edition

1
Objectives (1 of 2)

• 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
• Learn how to output data stored in an enum type

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 2
or otherwise on a password-protected website for classroom
Objectives (2 of 2)

• 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
• Learn about the namespace mechanism
• Explore the string data type, and learn how to use string functions to
manipulate strings

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 3
or otherwise on a password-protected website for classroom
Enumeration Type (1 of 5)

• A data type is a set of values with a set of operations on them


• Enumeration type is 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

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 4
or otherwise on a password-protected website for classroom
Enumeration Type (2 of 5)

• You can specify the name and the values, but not the operations
• The syntax for enumeration type is:

• value1, value2, … are identifiers called enumerators


• List specifies the ordering:
value1 < value2 < value3 <...

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 5
or otherwise on a password-protected website for classroom
Enumeration Type (3 of 5)

• The enumeration type is an ordered set of values


• Default value starts at 0 on enumerators
• A value used in one enumeration type cannot be used by another in the same
block
• Same rules apply to enumeration types declared outside of any blocks

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 6
or otherwise on a password-protected website for classroom
Enumeration Type (4 of 5)

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 7
or otherwise on a password-protected website for classroom
Enumeration Type (5 of 5)

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 8
or otherwise on a password-protected website for classroom
Declaring Variables

• Syntax

• Example
enum sports {BASKETBALL, FOOTBALL, HOCKEY, BASEBALL, SOCCER,
VOLLEYBALL};
• Can declare variables such as:
sports popularSport, mySport;

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 9
or otherwise on a password-protected website for classroom
Assignment

• Values can be stored in enumeration data types:


popularSport = FOOTBALL;
• Stores FOOTBALL into popularSport

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 10
or otherwise on a password-protected website for classroom
Operations on Enumeration Types

• No arithmetic operations are allowed on enumeration types


mySport = popularSport + 2; //illegal
popularSport = FOOTBALL + SOCCER; //illegal
popularSport = popularSport * 2; //illegal
• ++ and -- are illegal,
popularSport++; //illegal
popularSport--; //illegal
• The solution is applying the cast operator
popularSport = FOOTBALL;
popularSport = static_cast<sports>(popularSport + 1);

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 11
or otherwise on a password-protected website for classroom
Relational Operators

enum sports {BASKETBALL, FOOTBALL, HOCKEY, BASEBALL, SOCCER,


VOLLEYBALL};

• An enumeration type is an ordered set of values:


FOOTBALL <= SOCCER is true
HOCKEY > BASKETBALL is true
BASEBALL < FOOTBALL is false
• An enumeration type is an integral data type and can be used in loops:
for (mySport = BASKETBALL; mySport <= SOCCER; mySport =
static_cast<sports>(mySport + 1))
.
.
.

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 12
or otherwise on a password-protected website for classroom
Input /Output of Enumeration Types

• An enumeration type cannot be input/output (directly)


• Can input and output indirectly – refer to code segments below:
enum courses {ALGEBRA, BASIC, PYTHON, CPP, PHILOSOPHY,
ANALYSIS, CHEMISTRY, HISTORY};
courses registered;

switch (ch1)
{
case 'a':
if (ch2 == 'l')
registered = ALGEBRA;
else
registered = ANALYSIS;
break;

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 13
or otherwise on a password-protected website for classroom
Functions and Enumeration Types

• Enumeration types pass as parameters by value or by reference


• A function can return a value of the enumeration type

enum color {red, green, blue};

void myFunc(color c);

int main ()
{
myFunc(blue);

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 14
or otherwise on a password-protected website for classroom
Functions and Enumeration Types

• Enumeration types pass as parameters by value or by reference


• A function can return a value of the enumeration type

enum info_code {Success, Fail};

info_code myFunc2(int, int&);

int main ()
{
int num1=1, num2;
info_code result;

result= myFunc2(num1, num2);

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 15
or otherwise on a password-protected website for classroom
Declaring Variables When Defining the Enumeration Type

• Can declare variables of an enumeration type when you define an enumeration


type:
enum grades {A, B, C, D, F} courseGrade;

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 16
or otherwise on a password-protected website for classroom
Anonymous Data Types (1 of 2)

• Anonymous type values are directly specified in the declaration, with no type
name
• Example:
enum {BASKETBALL, FOOTBALL, BASEBALL, HOCKEY} mySport;

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 17
or otherwise on a password-protected website for classroom
Anonymous Data Types (2 of 2)

• Drawbacks:
• Cannot pass/return an anonymous type to/from a function
• Values used in one type can be used in another, but are processed diversely:
enum {TURKISH, ENGLISH, FRENCH, SPANISH, GERMAN} languages;
enum {TURKISH, ENGLISH, FRENCH, SPANISH, GERMAN} foreignLanguages;

• This statement is illegal:


languages = foreignLanguages; //Illegal

• Best practices: to avoid confusion, define an enumeration type first, then


declare variables

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 18
or otherwise on a password-protected website for classroom
typedef Statement

• The typedef statement is used to create synonyms or aliases to a data type


• The syntax of the typedef statement is:

• typedef does not create any new data types


• Only creates an alias to an existing data type

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 19
or otherwise on a password-protected website for classroom
Namespaces (1 of 6)

• ANSI/ISO standard C++ was officially approved in July 1998


• Most 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++

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 20
or otherwise on a password-protected website for classroom
Namespaces (2 of 6)

• Global identifiers in a header file used in a program become global in the


program
• A syntax error occurs if a program’s identifier has the same name as a global identifier
in the header file
• The same problem can occur with third-party libraries
• Common solution: third-party vendors begin their global identifiers with _
• Do not begin identifiers in your program with _

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 21
or otherwise on a password-protected website for classroom
Namespaces (3 of 6)

• ANSI/ISO Standard C++ attempts to solve with the namespace mechanism


• The general syntax of the statement namespace is:

members can be variable declarations, named constants, functions, or another


namespace

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 22
or otherwise on a password-protected website for classroom
Namespaces (4 of 6)

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 23
or otherwise on a password-protected website for classroom
Namespaces (5 of 6)

• A namespace member has scope local to the namespace


• A namespace member can be accessed outside the namespace
• The general syntax for accessing a namespace member is:

• ANSI/ISO Standard C++ provides the use of the statement using

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 24
or otherwise on a password-protected website for classroom
Namespaces (6 of 6)

• Examples with namespaces


using namespace globalType;
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

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 25
or otherwise on a password-protected website for classroom
string Type

• To use data type string, a program must include the header file string
• A string is a sequence of zero 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

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 26
or otherwise on a password-protected website for classroom
Example 7-18: swap Function

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 27
or otherwise on a password-protected website for classroom
Quick Review (1 of 3)

• 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

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 28
or otherwise on a password-protected website for classroom
Quick Review (2 of 3)

• 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

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 29
or otherwise on a password-protected website for classroom
Quick Review (3 of 3)

• using statement simplifies access to namespace members


• A string is a sequence of zero or more characters
• Strings in C++ are enclosed in ""
• The first character of a string is in position 0
• In C++, [] is the array subscript operator

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 30
or otherwise on a password-protected website for classroom

You might also like