02 C++Fundamentals
02 C++Fundamentals
02 C++Fundamentals
C++ Fundamentals
3rd Edition
Computing Fundamentals with C++
Rick Mercer
Franklin, Beedle & Associates
Goals
// Comment
#include-directive(s)
using namespace std;
int main() {
object-initializations
statement(s)
return 0;
}
Example C++ program
// This C++ program gets a number from the
// user and displays that value squared
return 0;
}
The compiler
• The compiler
• reads source code in a character by character fashion
• reports errors whenever possible
• reports warnings to help avoid errors
• conceptually replaces #includes with the source code
of the #included file
#include directives
• The form with " " first searches the working folder
before searching the system folder(s)
• the " " indicates a new file from your working folder.
Pieces of a C++ Program
int main() {
cout << "Hello World!";
return 0;
}
Special Symbols
• floating-point literals
1.234 -12.5 0.0 0. .0 1e10 0.1e-5
• string literals
"character between double quotes"
• integer literals
-1 0 1 -32768 +32767
• character literals
'A' 'b' '\n' '1'
Comments
/*
between slash star and star slash
*/
Common Operations on Objects
int main() {
double aDouble = 1.1;
string name = "Carpenter";
return 0; Output?
}
Assignment
bill = 10.00;
bill = bill + (0.06 * bill);
name = "Bee Bop";
name = "Hip Hop";
// bill is ___________?
• General forms :
cin >> object-1 ;
-or-
cin >> object-1 >> object-2 >> object-n ;
• Example: cin >> test1;
• When a cin statement is encountered
• the program pauses for user input
• the characters typed by the user are processed
• the object's state is changed to the value of the input
Input is Separated by Whitespace
blanks, tabs, newlines
int main() {
string name;
cout << "Enter your name: ";
cin >> name;
cout << "Hello " << name;
return 0;
}
Dialogue when the user enters Dakota Butler
Note: WindowMaker is still waiting for a non-existent future cin
Enter your name: Dakota Butler
Hello Dakota
Arithmetic Expressions
• Example expression:
(40 * payRate) + 1.5 * payRate * (hours - 40)
• The previous expression has how many
operators ?___ operands ?___
Arithmetic Expressions
• A recursive definition
a numeric object x
or a numeric constant 100 or 99.5
or expression + expression 1.0 + x
or expression - expression 2.5 - x
or expression * expression 2*x
or expression / expression x / 2.0
or ( expression ) (1 + 2.0)
Precedence of Arithmetic Operators
return 0;
}
int Arithmetic
int quarter;
quarter = 79 % 50 / 25; // What is quarter? ___
quarter = 57 % 50 / 25; // What is quarter? ___
Integer division, watch out
fahrenheit = 212;
celcius = 5 / 9 * (fahrenheit - 32);