Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

The Cast Operation, Additional C++ Operators | CPE 112, Study notes of Engineering

Material Type: Notes; Class: INTRO COMPUTER PROG FOR ENGR; Subject: Computer Engineering; University: University of Alabama - Huntsville; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 07/23/2009

koofers-user-3ok
koofers-user-3ok 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Electrical and Computer Engineering 1of 12
UAH CPE 112
Simple Data Types: Built-In
and User-Defined
Additional C++ operators: +=, -=, *=, /=
Statement Equivalent Statement
i += 5; i = i + 5;
pivotPoint *= n + 3; pivotPoint * (n + 3);
The Cast Operation
Two forms:
intVar = (int)floatVar;
intVar = int(floatVar); (only works if the data type name is a
single identifier)
myVar = (unsigned int) someFloat; (the only way for this one)
Electrical and Computer Engineering 2of 12
UAH CPE 112
Working with Character Data
Each computer uses a particular character set, two
in substantial use today are ASCII and EBCDIC.
The vast majority use ASCII.
Character Issues:
Comparing Characters: if (ch >= ‘a’ && ch <= ‘z’)
islower is preferred usage
Converting Digit Characters to Integers: ‘9’ ‘0’ = 9
Converting to Lowercase and Uppercase: toupper and
tolower can help (#include <cctype>)
Accessing Characters Within a String: Use position
number, e.g. ch = inputStr[2];
Electrical and Computer Engineering 3of 12
UAH CPE 112
More on Floating-Point Numbers
Comparing Floating-Point Numbers: check for an
error within some range
Underflow and Overflow: Numbers can be too
large or too small to be represented.
Cancellation Error: problems can occur when
arithmetic operations occur on values of wildly
different magnitudes: (1 + 0.00001234 1) =
0.00001234. For four digits of precision, this
doesn’t work, why?
Electrical and Computer Engineering 4of 12
UAH CPE 112
Practical Implications of
Limited Precision
Limited precision has led to
Mercury splashdown in unknown location, delaying the
recovery of the spacecraft and the astronaut.
Specialized high-tension cables linking a hydroelectric
dam to the nearest power distribution point had to be
redone due to precision problems.
A bank employee stole infinitesimal amounts from a
large number of accounts.
Electrical and Computer Engineering 5of 12
UAH CPE 112
User-Defined Simple Types
Enumeration Types C++ allows the user
to define a new simple type by listing
(enumerating) the literal values that make
up the domain of the type. These literal
values are identifiers, not numbers,
separated by commas.
Example:
enum Days (SUN, MON, TUE, WED,
THU, FRI, SAT);
Electrical and Computer Engineering 6of 12
UAH CPE 112
Enumeration Type Declaration
EnumDeclaration
enum Name {Enumerator, Enumerator, …};
Enumerator
Identifier = ConstIntExpression
Legal or Illegal?
enum Vowel {‘A’, ‘E’, ‘I’, ‘O’, ‘U’};
enum Places {1st, 2nd, 3rd};
enum Starch {CORN, RICE, POTATO, BEAN};
enum Grain {WHEAT, CORN, RYE, BARLEY, SORGHUM};
pf2

Partial preview of the text

Download The Cast Operation, Additional C++ Operators | CPE 112 and more Study notes Engineering in PDF only on Docsity!

Electrical and Computer Engineering

1 of 12

UAH CPE 112

Simple Data Types: Built-In

and User-Defined

• Additional C++ operators: +=, -=, *=, /=

Statement Equivalent Statement

i += 5; i = i + 5; pivotPoint *= n + 3; pivotPoint * (n + 3);

• The Cast Operation

Two forms:

intVar = (int) floatVar;

intVar = int(floatVar); (only works if the data type name is a

single identifier)

myVar = (unsigned int) someFloat; (the only way for this one)

Electrical and Computer Engineering

2 of 12

UAH CPE 112

Working with Character Data

• Each computer uses a particular character set, two

in substantial use today are ASCII and EBCDIC.

The vast majority use ASCII.

• Character Issues:

– Comparing Characters: if (ch >= ‘a’ && ch <= ‘z’) –

islower is preferred usage

– Converting Digit Characters to Integers: ‘9’ – ‘0’ = 9

– Converting to Lowercase and Uppercase: toupper and

tolower can help (#include )

– Accessing Characters Within a String: Use position

number, e.g. ch = inputStr[2];

Electrical and Computer Engineering

3 of 12

UAH CPE 112

More on Floating-Point Numbers

• Comparing Floating-Point Numbers: check for an

error within some range

• Underflow and Overflow: Numbers can be too

large or too small to be represented.

• Cancellation Error: problems can occur when

arithmetic operations occur on values of wildly

different magnitudes: (1 + 0.00001234 – 1) =

0.00001234. For four digits of precision, this

doesn’t work, why?

Electrical and Computer Engineering

4 of 12

UAH CPE 112

Practical Implications of

Limited Precision

• Limited precision has led to

– Mercury splashdown in unknown location, delaying the

recovery of the spacecraft and the astronaut.

– Specialized high-tension cables linking a hydroelectric

dam to the nearest power distribution point had to be

redone due to precision problems.

– A bank employee stole infinitesimal amounts from a

large number of accounts.

Electrical and Computer Engineering 5 of 12

UAH CPE 112

User-Defined Simple Types

• Enumeration Types – C++ allows the user

to define a new simple type by listing

(enumerating) the literal values that make

up the domain of the type. These literal

values are identifiers, not numbers,

separated by commas.

• Example:

enum Days (SUN, MON, TUE, WED,

THU, FRI, SAT);

Electrical and Computer Engineering 6 of 12

UAH CPE 112

Enumeration Type Declaration

• EnumDeclaration

enum Name {Enumerator, Enumerator, …};

• Enumerator

Identifier = ConstIntExpression

• Legal or Illegal?

enum Vowel {‘A’, ‘E’, ‘I’, ‘O’, ‘U’}; enum Places {1st, 2nd, 3rd}; enum Starch {CORN, RICE, POTATO, BEAN}; enum Grain {WHEAT, CORN, RYE, BARLEY, SORGHUM};

Electrical and Computer Engineering

7 of 12

UAH CPE 112

More About Enumeration Types

Consider the following declaration:

enum Animals {RODENT, CAT, DOG, BIRD, REPTILE, HORSE,

BOVINE, SHEEP};

and the following variable declarations:

Animals inPatient; Animals outPatient;

Acceptable assignemnts are

inPatient = DOG; outPatient = inPatient; someInt = DOG; inPatient = Animals(inPatient + 1);

Electrical and Computer Engineering

8 of 12

UAH CPE 112

Typical Usage of an

Enumeration Type

switch (inPatient) { case RODENT : case CAT : case DOG : case BIRD : cout << “Cage ward”; break; case REPTILE : cout << “Terrarium ward”; break; case HORSE : case BOVINE : case SHEEP : cout “Barn”; break; }

Electrical and Computer Engineering

9 of 12

UAH CPE 112

Enumeration Type Input

  • Enumeration types are known only internally to the program, they are

input and output indirectly.

string animalName; cin >> animalName;

switch (toupper(animalName[0])) { case ‘R’ : if (toupper(animalName[1] == ‘O’) inPatient = RODENT; else inPatient = REPTILE; break; case ‘C’ : inpatient = CAT; break;. ..

Electrical and Computer Engineering

10 of 12

UAH CPE 112

Enumeration Type Output

  • Enumeration types are known only internally to the program, they are

input and output indirectly.

switch (inPatient) { case RODENT : cout << “Rodent”; break; case CAT : cout << “Cat”; break; case DOG : cout << “Dog”; break; case BIRD : cout << “Bird”;

. break; .. }

Electrical and Computer Engineering 11 of 12

UAH CPE 112

Using a Value-Returning

Function for Input

Animals StrToAnimal (/in/ string str) { Animals inPatient; switch (toupper(str[0])) { case ‘R’ : if (toupper(animalName[1] == ‘O’) inPatient = RODENT; else inPatient = REPTILE; break; case ‘C’ : inPatient = CAT; break; case ‘D’ : inPatient = DOG; break; . . . return inPatient; } } Electrical and Computer Engineering 12 of 12

UAH CPE 112

User-Written Header Files

• User-defined data types can be useful in

more than one program.

• Declarations can be placed in a header file

and included using the #include directive.

• #include looks in the standard

include directory

• #include “months.h” looks in the current

directory