1 / 24

Chapter 2

Chapter 2. Creating a C++ Program. Elements of a C++ Program. Four basic ways of structuring a program Sequencing Selection Looping Sub-program. Basics. All programs must have at least one function We will learn to write many functions

clodia
Download Presentation

Chapter 2

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. Chapter 2 Creating a C++ Program

  2. Elements of a C++ Program • Four basic ways of structuring a program • Sequencing • Selection • Looping • Sub-program

  3. Basics • All programs must have at least one function • We will learn to write many functions • We will build more complex programs from simple functions

  4. Parts of the Programs • Identifiers • A name associated with a function or data object • It is used to reference that function or data object

  5. How to Name Identifiers • Must start with a letter or underscore • Followed by a letter, number or underscores • Cannot be a reserved word

  6. Quiz • Write true if the identifier’s name good and false otherwise: _Fred 3DArray R2D2 Silly_Name Good Name x BadName Id3nt1f13r Bonus: int const_cast

  7. Examples of Reserved Words • int • double • const • return • bool • if • while • for

  8. Data and Data Types • Data type • Determines how the data is represented in the computer and how the computer can operate on it • Data • What all computers use to compute

  9. Data Types • Two Kinds • Built-in • User Defined • Example of Built-in • int • double • bool • char

  10. char Data Type • Any single alphanumeric character • Example • ‘A’ • ‘a’ • ‘B’ • ‘1’ • ‘^’

  11. string Data Type • Not a built-in type • Any sequence of characters • Example • “Dave” “C++” “ 9 “ • Strings cannot span more than one line • Null String • “”

  12. Naming Elements • Use a Declaration • int empNum; • Variables • A location in memory, referenced by an identifier, that contains a data value that can be changed • Constants • A location in memory, referenced by an identifier, that contains a data value that cannot be changed

  13. Examples • int Counter; • double taxRate, Percent; • const char MIDDLEINITIAL = ‘P’; • const bool NOTTRUE = “false”;

  14. Executable Statements • Assignment Statements • lastName = “Roszak”; • Output Statements • cout << “Hello World”;

  15. Non-executable Statements • Comments • //This is how you indicate a comment • /*Or you can do it this way*/

  16. Blocks • Groups of lines that are to be grouped together between curly braces • Example • int main() { cout << “Hello World; }

  17. Pre-processor • Before the program is compiled, a program called a pre-processor parsers the code looking for directives; things to do • Example • #include <iostream> • #ifndef • #endif

  18. Introduction to Namespaces int main() { cout << “Happy Birthday” << endl; return 0; }

  19. More Namespaces #include <iostream> int main() { cout << “Happy Birthday” << endl; return 0; }

  20. Still More #include <iostream> int main() { std::cout << “Happy Birthday” << std::endl; return 0; }

  21. Yet More #include <iostream> using std::cout; using std::endl; int main() { cout << “Happy Birthday” << endl; return 0; }

  22. One More #include <iostream> using namespace std; int main() { cout << “Happy Birthday” << endl; return 0; }

  23. More Output • What will the following lines produce? • cout << “Hi there. “ << endl; • cout << endl; • cout <<“What are you doing?” << endl;

  24. More Output • What will these produce? • cout << “Hi there. “ << endl << endl <<“What are you doing?” << endl; • How about these? • cout <<“Hi there. \n\nWhat are you doing?\n”

More Related