Chapter 2 Creating a C Program Elements of
























- Slides: 24

Chapter 2 Creating a C++ Program

Elements of a C++ Program n Four basic ways of structuring a program 1. 2. 3. 4. – Sequencing Selection Looping Sub-program In C++ Sub-programs are known as functions

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

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

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

Quiz n Write true if the identifier’s name good and false otherwise: 1. _Fred 2. 3 DArray 3. R 2 D 2 4. Silly_Name 5. Good Name 6. x 7. Bad. Name 8. Id 3 nt 1 f 13 r 9. _ 10. 3 Bonus: 1. int 2. const_cast

Examples of Reserved Words n int n double n const n return n bool n if n while n for

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

Data Types n Two Kinds – Built-in – User Defined n Example – int – double – bool – char of Built-in

char Data Type n Any single alphanumeric character – Example – ‘A’ – ‘a’ – ‘B’ – ‘ 1’ – ‘^’

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

Naming Elements Use a Declaration n int emp. Num; n Variables n – A location in memory, referenced by an identifier, that contains a data value that can be changed n Constants – A location in memory, referenced by an identifier, that contains a data value that cannot be changed

Examples n int Counter; n double tax. Rate, Percent; n const char MIDDLEINITIAL = ‘P’; n const bool NOTTRUE = false;

Executable Statements n Assignment Statements – last. Name = “Roszak”; n Output Statements – cout << “Hello World”;

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

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

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

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

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

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

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

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

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

More Output n What will these produce? – cout << “Hi there. “ << endl <<“What are you doing? ” << endl; n How about these? – cout <<“Hi there. nn. What are you doing? n”