Program Construction
- Every C++ program has some common elements.
- No matter what you need to put these things in
- So just start out with the, don't worry about other parts.
- And I just lied, but ignore that fact.
- I will construct a program and talk about these elements.
- Pay attention to EVERYTHING
- spacing (between elements and between lines)
- Capatalization
- Punctuation
- Everything else too.
- Unless I say otherwise, you need to know what each part does and why it is formatted that way.
- If you don't understand, ask
- #include <iostream>
- This is called a preprocessor directive
- It "adds" elements to the C++ language.
- In this case, it adds input and output routines.
- Ok, so this one is tough right now.
- We will really learn about this in 230
- We will have other includes, and I will talk about when to use them.
- using namespace std
- If two people have the same name, a namespace helps us determin which we are talking about.
- This is a convienence thing, you should use it.
- We will really lean about this in 330
- int Square( int);
- int is a reserved word.
- It has a predefined meaning in the langague
- There are a list of reserverd words in appendis A.
- We will study reserverd words through 330 and beyond.
- In this case, it is a data type
- A data type is a specific set of values along with a set of operations on those values.
- This is the data type for integers.
- We will explore this more in chapter 3.
- Square is an identifier
- This is a name associated with a user definied object in a program.
- Everything we "make" will have an associated identifier.
- Syntax rules for declaring an identifier
- Identifiers must begin with a letter or the underscore
- They may only contain letters, underscore, or digits.
- They may NOT be reserverd words.
- There are conventions for declaring identifiers too.
- Some are specific to the type of object being named.
- But a global rule is that the identifier should have meaning.
- Square is also a function name.
- this is a small sub-program which performs a task.
- In this case, it will square a number.
- When declaring function names, we use the conventions:
- Each word should start with an upper case letter.
- All other letters should be lower case.
- We will study functions further in chapters 8 and 9
- (int ) is a parameter list
- This says the the function Square takes a single parameter,
an integer.
- ; marks the end of a statement.
- int Square(int); is also a function prototype
- Don't worry about this until later
- Except that it is defining a function
- In C/C++ everything must be Declared or defined before it is used.
- This tells the compiler how the function Square is used.
- Again, this is chapter 8/9 material.
- int main()
- Begins the definition of the main routine.
- main is a standard identifier
- Every C/C++ program expects a function called main.
- This is where execution begins.
- main is not a reserverd word, but is in some sense already taken,
- But you must declare it for every program.
- {
- This is the beginning of a code block.
- it ends with a matching }
- A code block turns one or more statements into a single statement.
- Placement:
- Two choices, like in the book, or like I do it.
- Convention:
- Everything inside of a code block is indented by a standard amount.
- More on this later.
- int theNumber;
- This is a variable declaration.
- it is in the form: datatype identifier;
- There are other forms which we will discuss, but this will do for now.
- A variable is a location in memory referenced by an identifier
- Variables may change value throughout the program.
- But there is ALWAYS a value in the variable.
- This is an uninitialized varable, since we have not given it a value
- It currently contains garbage.
- Variables must be defined before they can be used.
- Convention:
- Variable names start with a lower case letter.
- Each new word starts with an upper case letter.
- That way we can easly tell a variable from a function.
- theNumber = 7;
- This is the first executable statement.
- That means that the computer actually performs some action based upon this statement.
- It is also an assignment statement.
- In this case, the expression on the right hand side is evaluated.
- The value is then stored in the variable on the left hand side.
- = is an operator
- These also have special meaning.
- The list of operators is in appendix b.
- The presidence is in this table as well.
- 7 is a literal constant
- That is a constant written in the program.
- The result of this operation is that a 7 is stored at theNumber
- cout << "The number is " << theNumber << endl;
- This is an executable statement as well.
- cout is a standard identifier.
- It is an input stream (part of iostream)
- It is usually associated with the monitor, but it can be other things.
- << is a stream insertion operator.
- It takes the item on the right and inserts it into the stream on the left.
- "The number is "
- Is a string literal constant.
- A string is another data type.
- It is a collection of characters.
- It is enclosed in quotation marks.
- theNumber is an expression
- The value of the variable (theNumber) is retreived and printed out.
- endl is a standard identifier
- It is part of the iostream library
- It prints en end of line marker.
- And it also flushes the buffer.
- cout << "The square is " << Square(theNumber) << endl;