The Fundamentals of C++: Basic Programming Elements and Concepts
The Fundamentals of C++: Basic Programming Elements and Concepts
The Fundamentals of C++: Basic Programming Elements and Concepts
Declaration
Action
Executable unit
Named set of program statements
names
Subroutine: Fortran and Basic
Procedure: Pascal
Function : C++
Program Organization
C++ program
Collection of definitions, declarations and functions
Advantages
Structured into small understandable units
Complexity is reduced
Values or properties
Data members
Ability to react to requests (messages)!!
Member functions
When an object receives a message, one of two actions are
performed
Object is directed to perform an action
cout << "Area = " << Area << " = Length "
<< Length << " * Width " << Width << endl;
return 0;
}
Visual C++ IDE with Area.cpp
Area.cpp Output
Comments
Allow prose or commentary to be included in program
Importance
Programs are read far more often than they are written
maintained
C++ has two conventions for comments
// single line comment (preferred)
Typical uses
Identify program and who wrote it
Floating-point objects
Character objects
Z
5 1.28345
1 3.14
P
Integer Object Types
The basic integer object type is int
The size of an int depends on the machine and the
compiler
On PCs it is normally 16 or 32 bits
Other integers object types
short: typically uses less bits
Octal (base 8)
Digits 0, 1, 2, 3, 4, 5, 6, 7
Hexadecimal (base 16)
Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a , b, c, d, e, f, A, B, C,
D, E, F
Consider
31 oct and 25 dec
Decimal Constants
Examples
97
L or l indicates long integer
40000L
50000
23a (illegal)
'\t' is the explicit tab character, '\n' is the explicit new line
character, and so on
Literal String Constants
A literal string constant is a sequence of zero or more
characters enclosed in double quotes
"We are even loonier than you think"
"Rust never sleeps\n"
"Nilla is a Labrador Retriever"
Fractional part
double
long double
Floating-Point Constants
Standard decimal notation
134.123 F or f indicates single precision
0.15F floating point value
Digits
Identifiers
Keywords
Keywords are words reserved as part of the language
int, return, float, double
Examples
Min
Temperature
CameraAngle
CurrentNbrPoints
Definitions
All objects that are used in a program must be defined
Name
of object
Integer Division
Integer division produces an integer result
Truncates the result
Examples
3 / 2 evaluates to 1
4 / 6 evaluates to 0
10 / 3 evaluates to 3
Mod
Produces the remainder of the division
Examples
5 % 2 evaluates to 1
12 % 4 evaluates to 0
4 % 5 evaluates to 4
Operators and Precedence
Consider mx + b
Consider m*x + b which of the following is it equivalent to
(m * x) + b
m * (x + b)
(4 / 5)
((4 / 5) * 2)
((4 / 5) * 2) (3 * 5)
((4 / 5) * 2) ((3 * 5) % 4)
(20 -((4 / 5) * 2)) ((3 * 5) % 4)
(20 -((4 / 5) * 2)) + ((3 * 5) % 4)
Defining and Initializing
When an object is defined using the basic form, the memory
allotted to it contains random information
the object