C++ Programming: Enumeration Types and Strings, Slides of Computer Science

An introduction to enumeration types and strings in C++ programming. It covers the definition, values, and operations on enumeration types, as well as the use of strings, string concatenation, and functions like swap and find. It also explains the difference between value and reference parameters in functions.

Typology: Slides

2021/2022

Uploaded on 05/14/2022

mohamad-yassine
mohamad-yassine 🇱🇧

1 / 68

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44

Partial preview of the text

Download C++ Programming: Enumeration Types and Strings and more Slides Computer Science in PDF only on Docsity!

Lecture

Enum, Arrays, Strings, Struct and more

Dr. Mageda Sharafeddin

Objectives In this chapter, you will:

  • Learn how to create and manipulate your own simple data type—called the enumeration type
  • Become aware of the typedef statement
  • Explore the string data type, and learn how to use the various string functions to manipulate strings
  • Learn about records (structs)
  • Examine various operations on a struct
  • Manipulate data using a struct
  • Learn about the relationship between a struct and functions
  • Examine the difference between arrays and structs C++ Programming: From Problem Analysis to Program Design, Seventh Edition 2

C++ Programming: Program Design Including Data Structures, Second Edition 4 Enumeration Type

  • Data type - a set of values together with a set of operations on those values
  • To define a new simple data type, called enumeration type, we need three things: - 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, Second Edition 5 Enumeration Type (continued)

  • A new simple data type can be defined by specifying its name and the values, but not the operations
  • The values must be identifiers

C++ Programming: Program Design Including Data Structures, Second Edition 7 Examples

  • The following are illegal enumeration types because none of the values is an identifier: enum grades{'A', 'B', 'C', 'D', 'F'}; enum places{1st, 2nd, 3rd, 4th};
  • The following are legal enumeration types: enum grades{A, B, C, D, F}; enum places{first, second, third, fourth};

C++ Programming: Program Design Including Data Structures, Second Edition 8 Declaring Variables

  • The syntax for declaring variables is: dataType identifier, identifier,...;
  • The following statement defines an enumeration type sports enum sports{basketball, football, hockey, baseball, soccer, volleyball};
  • The following statement declares variables of the type sports. sports popularSport, mySport;

10 Operations

  • No arithmetic operation is allowed on enumeration types
  • The following statements are illegal; mySport = popularSport + 2; //illegal popularSport = football + soccer; //illegal popularSport = popularSport * 2; // illegal

C++ Programming: Program Design Including Data Structures, Second Edition 11 Operations (continued)

  • The increment and decrement operations are not allowed on enumeration types
  • The following statements are illegal; popularSport++; //illegal popularSport--; //illegal

Examples of legal enum operations enum sports {BASKETBALL, FOOTBALL, HOCKEY, BASEBALL, SOCCER, VOLLEYBALL}; sports popularSport, mySport; popularSport = FOOTBALL; popularSport = static_cast(popularSport + 1); What is the value of popularSport after the last statement is executed? C++ Programming: Program Design Including Data Structures, Second Edition 13

Examples of legal enum operations enum sports {BASKETBALL, FOOTBALL, HOCKEY, BASEBALL, SOCCER, VOLLEYBALL}; FOOTBALL <= SOCCER HOCKEY > BASKETBALL BASEBALL < FOOTBALL for (mySport = BASKETBALL; mySport <= SOCCER; mySport = static_cast(mySport + 1))

-. -. -. C++ Programming: Program Design Including Data Structures, Second Edition 14

C++ Programming: Program Design Including Data Structures, Second Edition 16 Functions and Enumeration Types

  • Enumeration type can be passed as parameters to functions either by value or by reference
  • A function can return a value of the enumeration type

Enumeration as input or output

  • enum courses {ALGEBRA, BASIC, PASCAL, CPP, PHILOSOPHY, ANALYSIS, CHEMISTRY, HISTORY};
  • courses registered;
  • char ch1, ch2;
  • cin >> ch1 >> ch2; //Read two characters C++ Programming: Program Design Including Data Structures, Second Edition 17

C++ Programming: Program Design Including Data Structures, Second Edition 19 typedef Statement

  • You can create synonyms or aliases to a previously defined data type by using the typedef statement
  • The syntax of the typedef statement is: typedef existingTypeName newTypeName Ex: typedef int integer; integer i;

20 ANSI/ISO Standard C++, Namespaces namespace namespace_name { members } namespace globalType { const int N = 10; const double RATE = 7.50; int count = 0; void printResult(); } globalType::RATE; globalType::printResult();