1 / 24

Basic Elements of a C ++ Program

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.

Download Presentation

Basic Elements of a C ++ Program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 01/28/11 Basic Elements of a C ++ Program

  2. 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

  3. Objectives • State the basic parts of a C++ program. • Create legal C++ names. • Write statements to output strings.

  4. A Sample C++ Program • travel.cpp

  5. 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

  6. travel.cpp int main()‏ • Beginning of first function executed • Only one main • Must be in every program { } • Brackets delimit executable statements

  7. travel.cpp double dist, time, speed; • Declare variables • Set aside locations in memory • Gives the locations names • double tells the data's type

  8. 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

  9. travel.cpp speed = dist/time; //find speed • Assignment Statement • speed gets dist/time • Does computation, stores answer in "speed"

  10. travel.cpp cout << speed << " mph\n"; • Outputs result to monitor • \n is newline character return 0; • Ends program

  11. Names • Examples: • rate, time, distance • R2D2, k9, K9, _acceration_gravity • Contain letters, digits, underscore • Start with letter or underscore

  12. Names • Are these legal? Yr2007, 10_year_average, Y2K, form-99, M_PI

  13. Keywords • Names C++ reserves • Cannot as use variable names. • E.G. double, return, namespace • See appendix F

  14. Output

  15. Output • Example -- out.cpp • cout • Console output • Output stream • destination for stream of characters • Goes to standard output, usually monitor screen

  16. Output • << • insertion operator • inserts characters in output stream • String • "September 7th is" • inserted into stream

  17. Output cout << “Fire”; //Leaves cursor after “e” cout << “fighter”; Output : Firefighter

  18. Output cout << “Fire\n”; //Cursor on new line cout << “fighter”; Output : Fire fighter • \n is an escape sequence

  19. Output • Program out1.cpp • several \n's

  20. Example

  21. Output

  22. Output • endl • stream manipulator function. • Another way to output a newline character. • out2.cpp

  23. Next • Algorithms • Data Types 2.4 • Input 2.3

More Related