Chapter 2: Fundamental Data Types

Chapter Goals


Number Types (coins1.cpp)

Number Types

Number Types (Syntax 2.1: Output Statement)

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.

Number Types

Number Types (Syntax 2.2: Variable Definition)

Syntax 2.2 : Variable Definition

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.

 

Number Types (Syntax 2.3: Comment)

Syntax 2.3 : 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.

 



Input and Output (coins2.cpp)

Input and Output

Input and Output (Syntax 2.4: Input Statement)

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)

Assignment (Assignment Operator)

Assignment (Syntax 2.5 : Assignment)

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 (volume2.cpp)



Constants


Constants (Syntax 2.7 : Constant Definition)

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 (Basic Operations)

Arithmetic (Integer Division)

Arithmetic (coins4.cpp)

Arithmetic (Syntax 2.8 : Function Call)

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

Strings (Syntax 2.9 : Member Function Call)

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)