Upload
others
View
10
Download
0
Embed Size (px)
Citation preview
Data Types Chapter 8
Chapter Topics
Enumeration Data Types
Declaration
Assignment
Operations
Looping with Enumeration Types
Anonymous Data Types
The typedef statement
Namespaces
The string type
2
Enumeration Data Types
A data type is
A set of values together with
A set of operations on those values.
In order to define a new simple data type, called enumeration type, we need:
A name for the data type.
A set of values for the data type.
A set of operations on the values.
3
Enumeration Data Types
C++ allows the user to define a new simple data type by specifying:
Its name and the values
But not the operations.
The values that we specify for the data type must be legal identifiers
The syntax for declaring an enumeration type is:
enum typeName{value1, value2,
...};
4
Declaration of Enumerated Types
Consider the colors of the rainbow as an enumerated type:
enum rainbowColors =
{ red, orange, yellow,
green,
blue, indigo, violate }
The identifiers between { } are called enumerators
The order of the declaration is significant red < orange < yellow …
5
Declaration of Enumerated Types
Why are the following illegal declarations?
enum grades{'A', 'B', 'C', 'D',
'F'};
enum places{1st, 2nd, 3rd, 4th};
6
They do not have legal
identifiers in the list
• What could you do to make them legal?
enum grades{A, B, C, D, F};
enum places{first, second, third, fourth};
Declaration of Enumerated Types As with the declaration of any object
Specify the type name
Followed by the objects of that type
Given:
enum daysOfWeek { Sun, Mon,
Tue,Wed, Thu, Fri, Sat }
Then we declare:
daysOfWeek Today, payDay,
dayOff;
7
Assignment with Enumerated Types
Once an enumerated variable has been declared
It may be assigned an enumerated value
Assignment statement works as expected
payDay = Fri; // note no quotes
// Fri is a value, a constant
Enumerated variables may receive only values of that enumerated type 8
Operations on Enumerated Type Objects
Incrementing variables of an enumerated type
Do NOT use workaday += 1; NOR today++;
Instead, use explicit type conversion today = daysOfWeek (today + 1);
9
Operations on Enumerated Type Objects
Comparison
normal, OK
in order of the enumeration definition
I/O
generally not possible to do directly
can be sort of done, indirectly
Used primarily for program control, branching, looping
Possible to have functions return an enumerated type
10
Looping with Enumeration Types
Use an enumerated type variable as the loop control variable of a for loop
for (day = Mon; day < Sat;
day = static_cast<daysOfWeek>(day
+ 1))
{
. . .
}
11
This works because the values are
represented internally as integers
Functions with Enumerated 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.
daysOfWeek nextDay (daysOfWeek
d)
{
return (daysOfWeek) ((d +
1)%7);
}
12
Anonymous Data Types
Named Type
user defined type
declaration includes typedef
As with daysOfWeek or Boolean
Anonymous Type
does not have an associated type
enum (MILD, MEDIUM, HOT) salsa_sizzle;
variable declared without typedef
13
Anonymous Data Types
Disadvantages
Cannot pass an anonymous type as a parameter to a function.
A function cannot return a value of an anonymous type.
Problems when:
enum {English, French, Spanish, German, Russian}
languages;
enum {English, French, Spanish, German, Russian}
foreignLanguages;
languages = foreignLanguages; //illegal
14
Same values used but variables
treated as non compatible types
The typedef statement
Syntax:
typedef existing_type_name new_type_name;
Example: typedef int Boolean;
Does not really create a new type
is a valuable tool for writing self-documenting programs
15
Namespaces
Recall use of using namespace std;
Namespace is another word for scope
In C++ it is a mechanism programmer creates a "named scope"
namespace std { int abs ( int ); . . . }
16
Namespaces
Identifiers within a namespace can be used outside the body of the declaration only if
use scope resolution operator
x = std::abs(y); a using declaration
using std::abs; z = abs(q);
a using directive
using namespace std; p = abs(t);
17
Note the distinction
between declaration
and directive
Namespaces
We usually place the using directive in global scope
All blocks { . . . } then have identifiers available from the std namespace
18
The string type
We have used arrays of char to hold "string" information
char name[30]; cin >> name;
There are some problems with doing this
There is no assignment statement
Must use strcpy (name, "Clyde");
Cannot do comparisons with < or == or >
Must use if (strcmp (s1, s2) == 0) …
For all these must use #include <string.h>
19
The string type
C++ has a string type which bypasses all the problems we've encountered
Gain these capabilities by
#include <string> // note no .h
Now we can use statements as shown:
string name = "Clyde";
if (title1 < title2) …
str1 = str1 + "Day";
// assignment and
concatenation
20
The string type
Some functions are available
string name, title;
name = "Alexander";
cout << name.length()<<endl;
cout << name.find('x') <<endl;
cout << name.substr(1,3)
<<endl;
title = "Big Cheese";
title.swap(name);
cout << name<<endl; 21
Guess what will be
the output of these
lines of code