Chapter 2: Fundamental Data Types

Chapter Goals


Number Types (coins1.cpp)

Consider the following simple problem:
I have 8 pennies, 4 dimes, and 3 quarters in my purse. What is the total value of the coins?

Syntax 2.1: Output Statement

cout << expression1 << expressioun2 << ... << expressionn;
Example:
cout << pennies;
cout << "Total value = " << total << "\n";
Purpose: Prints the values of one or more expressions.

Syntax 2.2: Comments

/* comment text */
// comment text
Example:
/* total value of coins */
// total value of coins
Purpose: Add a comment to help the human reader understand the program.

Syntax 2.3: Variable Definition Statement

type_name variable_name;
type_name variable_name = initial_value;
Example:
int pennies = 8;
double total;
Purpose: Defines a new variable of a particular type, and optionally supply an initial value.


Input and Output (coins2.cpp)

The program could ask how many coins I have of any kind, and then compute the total.

Syntax 2.4: Input Statement

cin >> variable1 >> variable2 >> ... >> variablen;
Example:
cin >> pennies;
cin >> first >> middle >> last;
Purpose: Read the value for one or more variables in from the console.


Assignment (coins3.cpp)

Let us compute the value of the coins by keeping a running total.

Assignment (Assignment Operator)

Syntax 2.5: Assignment

variable = expression;
Example:
total = pennies * 0.01;
Purpose: Store the value of an expression in a variable.

Assignment (Increment and Decrement)


Constants (volume1.cpp)

The program computes the amount of soda (or beer) in a refregerator that is filled two-liter bottles and 12-oz (1 fluid ounce = 29.586 ml) cans.

 

Constants (volume2.cpp)

Syntax 2.7: Constant Definition

const type_name constant_name = initial_value;
Example:
const double LITER_PER_OZ = 0.029586
Purpose: Define a new constant of a particular type and supply its value.


Arithmetic

Arithmetic (coins4.cpp)



Syntax 2.8: Function Call

function_name(expression1,expressioun2, ..., expressionn);
Example:
sqrt(x)
pow(z + y, n)
Purpose: The result of calling a function and supplying the values for the function parameters.

Arithmetic (cmath Library Functions)

Function
Description
sqrt(x)
square root of x
pow(x, y)
xy
sin(x)
sine of x (in radians)
cos(x)
cosine of x (in radians)
tan(x)
tangent of x (in radians)
exp(x)
ex
log(x)
(natural log) ln(x), x > 0
log10(x)
(decimal log) lg(x), x > 0
ceil(x)
smallest integer >= x
floor(x)
largest integer <= x
fabs(x)
absolute value |x|

Arithmetic (Analyzing an Expression)


Strings

Strings (Member Functions)

Name
Purpose
s.length()
The length of s
s.substr(i, n)
The substring of length n of s starting at index i
getline(f, s)
Read string s from the input stream f

Syntax 2.9: Member Function Call

expression.function_name(expression1,expressioun2, ..., expressionn)
Example:
name.length()
name.substr(0, n - 1)
Purpose: The result of calling a function and supplying the values for the function parameters.

Strings (Substrings)

Strings (Concatenation)

Strings (initials.cpp)

Strings (Output Formatting)