Overview of Chapter 3, Programming and Problem Solving with C++, 5th edition (2010)
C++ Data Types
* Types CHAR, SHORT, INT, and LONG are unsigned. An unsigned integer value is assumed to be only positive or zero.
Examples of Named Constant Declarations for Numberic Types:
const float PI = 3.14159;
const float E = 2.71828;
const float int = MAX_SCORE = 100;
const float int MIN_SCORE = -100;
const char = LETTER = ‘W’;
const string NAME = “Elizabeth”;
Examples of Variable Declarations:
Given the following declarations:
the following are appropriate assignment statements:
alpha = 2856;
rate = 0.36;
ch = ‘B’;
num = alpha;
+ Unary plus
– Unary minus
/ Division (Integer dvision is without a fractional part)
% Modulus (Remainder from integer divsion)
The modules sign % is used to find the remainder of an integer division. Integer division is without floating point numbers therefore 7/2 is equal to 3. With use of modulus 7%2 is equal to 1, as this is the remainder when you divide 7/2. (p. 95)
The implicit (automatic) conversion of a value from one data type to another.
myFloat = 12;
Coercion changes the integer value of 12 in the above statement to the float value of 12.0.
myInt = 4.852;
Coercion changes the float 4.852 into an integer 4, as the the floating point was removed.
The explicit conversion of a value from one data type to another; also know as type conversion.
myInt = myFloat + 8.2;
myInt = int(myFloat + 8.2);
In both examples, the type int is cast into type float.
Mixed type expression
An expression that contains operands of different data types; also called mixed mode expression.
myInt * myFloat
4.8 + myInt – 3
Sample Program to Clarify Function Calls:
- int main()
- count << “The square of 10 is ” << Square(10) << endl:
- return 0;
- int Square ( int n )
- return n * n;
The statement in line 3, the (main) causes the servant (Square) to compute the square of 10 and return the result back to the main.
In line 3 Square(10) is a function call or function invocation. The computer temporarily puts the main function on hold and starts the Square function running. When Square has finished, the computer goes back to main and picks up where it left off.
Function call (function invocation)
The mechanism that translates control to a function.
A mechanism by which functions communicate with each other.
In line 3, the number 10 is known as an argument (or actual parameter). Arguments make it possible for the same function to work on many different values.
cout << Square(4);
cout << Square(125);
Syntax Template for a Function Call:
Some functions have two, three, four, or more arguments in the list, all separated by commas.
Programming languages include a large collegction of prewritten functions, data types, and other items that any programmer may use. In C++, the functions in the library are placed into separate files known as header files.
Header file: <cmath>
Argument type: float
Result type: float
Result (Value Returned): Square root of x.
To use a library function, add the include statment at the top of your program.
The above example adds the library <cmath> that includes the sqrt function (Square root).
Void function (procedure)
A function that does not return a function to its caller and is invoked as a separate statement.
A function that returns a single value to its caller and is invoked from within an expression.
Manipulators are used only in input and ouput statements.
For example, the the line:
cout << myInt << endl << myFloat;
uses the “End Line” manipulator (endl).
cout << ExpressionOrManipulator << ExpressionOrManiuplator … ;
Some other examples of manipulators:
Set Width: setw
Fix large floating point numbers: fixed
(Large values floating point numbers may be written as scientific numbers; the fixed manipulator causes a decimal to be displayed).
Set desired number of decimal points: setprecision
* Set Width applies only to the very next item printed. Set Precision applies to all subsequent values.
Dale, N. and Weems, C. (2010). Programming and Problem Solving with C++, 5th Edition, Jones and Bartlett Publishers, LLC, Sudbury, MA.