View
29
Download
3
Category
Tags:
Preview:
DESCRIPTION
01/28/11. Basic Elements of a C ++ Program. Quiz 1. Monday Chapter 1, Linux and vim On your own p. 24 #1-8, 13, 14, 16 odd answers in text Define Memory address Memory contents Machine language. Objectives. State the basic parts of a C++ program. Create legal C++ names. - PowerPoint PPT Presentation
Citation preview
Quiz 1
• Monday • Chapter 1, Linux and vim• On your own – p. 24 #1-8, 13, 14, 16– odd answers in text
• Define–Memory address–Memory contents–Machine language
Objectives
• State the basic parts of a C++ program.• Create legal C++ names.• Write statements to output strings.
A Sample C++ Program
#include <iostream>– Preprocessor directive–Must have to do input/output– cout, cin, <<, >>
using namespace std;– Required – Used to prevent name conflicts in large
projects
travel.cpp
int main()– Beginning of first function executed–Only one main –Must be in every program
{ }– Brackets delimit executable statements
travel.cpp
double dist, time, speed;– Declare variables– Set aside locations in memory–Gives the locations names– double tells the data's type
travel.cpp
cout<< "Miles traveled?";
cin >>dist; //input – cout puts message on monitor– cin inputs data and stores it in variable,
dist.– // begins a comment
travel.cpp
speed = dist/time; //find speed• Assignment Statement• speed gets dist/time• Does computation, stores answer in
"speed"
travel.cpp
cout << speed << " mph\n"; –Outputs result to monitor– \n is newline character
return 0;– Ends program
Names • Examples: – rate, time, distance– R2D2, k9, K9, _acceration_gravity
• Contain letters, digits, underscore• Start with letter or underscore
Keywords• Names C++ reserves• Cannot as use variable names.• E.G. double, return, namespace• See appendix F
Output
• Example -- out.cpp• cout – Console output–Output stream• destination for stream of characters
–Goes to standard output, usually monitor screen
Output
• << – insertion operator– inserts characters in output stream
• String– "September 7th is"– inserted into stream
Output
cout << “Fire\n”; //Cursor on new line
cout << “fighter”;
Output :
Fire
fighter
• \n is an escape sequence
Recommended