SI204

Class 4: Types & Expressions II

Reading

None

 

Lecture

Types

Up to this point, the only kind of object we have dealt with in our programs is double. Now we'll look at several other types, and as the course progresses we'll look at more and more, culminating with us defining new types of our own. I cannot stress this enough the idea of type is key to programming, regardless of the language. As we discuss these types, we'll also go over some of the operations defined for those types. This list is not exhaustive.  

double (and float)

We've seen doubles quite a bit already. We use double to simulate computing with real numbers (i.e. anything that can be represented on a number line). I say "simulate", because most numbers can only be approximated by a double, analogous to the way we approximate 1/3 as the 6-digit decimal 0.333333. We'd need an infinite number of digits to get 1/3 exactly, and in just the same way, the double type is limited to just approximating numbers like 1/3. The issue of how to get good and reliable answers when we can only approximate such numbers is the concern of numerical analysis, which is an important field on the border between mathematics and computer science. The float type is a "smaller", less accurate version of double.

Operation(s)

Comments

+,-,*,/

all more or less work as you'd expect

cmath

Use cmath for other typical mathematical operations. Remember #include <cmath>, and remember that all these operations are in the std namespace.

 

 

int

The int stands for integers. This type is the real workhorse of computing. As with integers, there are no fractions, but unlike integers there are limits on both the positive and negative ends (in the billions on a typical PC today). If you use a number like 65 in your program, it is interpreted as an int. If you used 65.0, on the other hand, it would be a double. (Numbers hard-coded into a program like this are called literals.)

Operation(s)

Comments

+,-,*

all more or less work as you'd expect

/

Division is tricky, since fractions aren't involved. Any fractional part leftover after division is simply chopped off (truncated). This leads to some odd "identities". For example, if x is the int 5, and y is the int 2, then x/y is 2! The most infamous version of this is that cout << 1/2; prints out 0. This will trick you at some point or another, I promise.

%

This is the "modulus" operator. a % b returns the remainder when a is divided by b. So, for example, 5 % 3 is 2. The signs of numbers simply change the sign of the result, according to the same rules as we have for multiplication. The % operator is surprisingly important. Please look at the Minutes and Seconds program to see an example of why!

 

 

char

The char stands for character. These are letters or numbers, or stranger things ... for example there is a "bell character". Writing it causes your computer to beep! char literals are written inside single quotes, so for example the char a is written inside a program as 'a'. Unusual "characters", like tabs or newlines, use "escape codes". They are identified by a backslash. Tab is '\t', and newline is '\n'. Operations on char's will be more important later on.

 

bool

Characters and integers and (approximations of) real numbers are objects that you probably expect to want to compute with. Boolean values are perhaps not as obvious to you. A boolean value is either true or false, yes or no, one or zero. These are critical to computing because, as we'll soon discuss, much of programming comes down to decision making, and true/false - yes/no - 1/0 boolean values are our decisions. We'll discuss Boolean Logic in more detail later, but here are the basic operations.

Operation(s)

Comments

&&

The AND operator. For bool's a and b, a && b is true if both are true and false otherwise.

||

The OR operator. For bool's a and b, a||b is false if both are false and true otherwise.

!

The NOT operator. For bool a, !a is false if a is true and true if a is false.

The words true and false can be used in your programs for boolean literal values.

string

The types we've seen so far (int, double, float, char, bool) are all built-in types. This means that they are part of the core language rather than part of some library. The last type we'll talk about today, the type string, is not a built-in type, it is defined in the library string. It is used for strings of characters. We've already dealt with some string literals, for example "Hello World!", quotes and all, is a string literal. As long as you remember #include <string> and using namespace std;, you can use strings just like any other type. The true name of this type is std::string, but with the using statement we can drop the std::.

Operation(s)

Comments

+

Concatenation. For example, if s is the string "Hello" and t is the string "World", then s + t is the string "HelloWorld".

In fact, there are many, many more types in C++. Even cin and cout are objects with types: cin is an object of type istream and cout is an object of type ostream. It will be a while before we discuss what these types are all about, but most things in C++ are objects with types.

 

The Power of Types

One of the easiest ways to see the importance of types in C++ is to watch how they affect cin. When you use cin >> x, the behavior of cin depends on the type of x. In other words, cin uses type information to do the right thing.

 

Code

User Input

Program Output

char c;
cin >> c;
cout << c << endl;

23.005%

2

int k;
cin >> k;
cout << k << endl;

23.005%

23

double x;
cin >> x;
cout << x << endl;

23.005%

23.005

string s;
cin >> s;
cout << s << endl;

23.005%

23.005%

bool b;
cin >> b;
cout << b << endl;

23.005%

1

 

Look carefully at this table, and make sure that you understand why cin did what it did based on the types of the variables into which it read.

NOTE: Any string tab, space, and newline characters is called whitespace. An important feature of cin is that it skips leading whitespace before it starts reading.

 

Input and Output as Streams of Characters

Input like:

Jones 3:25
 Smith 4:11

seems to be spread over several lines and composed of different elements - numbers, strings, and characters. However, it is in fact just one long line of characters, and by reading data we move through this line of characters called an input stream. Suppose, for example, we ran the code

string str1,str2;
int m1,m2,s1,s2;
char c1,c2;
cin >> str1 >> m1 >> c1 >> s1;
cin >> str2 >> m2 >> c2 >> s2;

with the above as input. What follows shows you this input stream, and demonstrates how we move through the input stream. The ^ carrot, shows what the "next character to be read" is. The tab character is represented by \t, and the newline character by \n.

 J o n e s \t 3 : 2 5 \n S m i t h \t 4 : 1 1 \n
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
^
 
 J o n e s \t 3 : 2 5 \n S m i t h \t 4 : 1 1 \n
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 ^
 
 J o n e s \t 3 : 2 5 \n S m i t h \t 4 : 1 1 \n
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 ^
 
 J o n e s \t 3 : 2 5 \n S m i t h \t 4 : 1 1 \n
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 ^
 
 J o n e s \t 3 : 2 5 \n S m i t h \t 4 : 1 1 \n
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 ^
 
 J o n e s \t 3 : 2 5 \n S m i t h \t 4 : 1 1 \n
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 ^
 
 J o n e s \t 3 : 2 5 \n S m i t h \t 4 : 1 1 \n
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 ^
 
 J o n e s \t 3 : 2 5 \n S m i t h \t 4 : 1 1 \n
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 ^
 
 J o n e s \t 3 : 2 5 \n S m i t h \t 4 : 1 1 \n
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 ^
 

 

Problems

         Length of Vectors

         Minutes and Seconds

         Class Year from E-Mail Addresses

         Converting hh:mm:ss to seconds

 

Do You Know ...

1.      The difference between an "object" and a "type"?

2.      Why 1/2 is 0 in C++?


Assoc Prof Christopher Brown

Last modified by LT M. Johnson 08/15/2007 09:12 AM