would destroy all of civilization.
— Anon.
Teacher’s Notes
In this chapter students are introduced to the concept of a program. Programs consist of three parts:
comments, data, and code.
We’ve already covered comments. They are the most important part of a program and are used to
document the things that the program does. They are written for human beings like maintenance
programmers and C++ instructors.
To simplify things as much as possible we present only one type of data, simple variables declared
globally. Later we’ll see how we can declare complex variables such as structures and classes. We’ll
also look at the difference between local and global variables. But for now we just want to get the
concept of a variable across.
Similarly, the code part has also been simplified. Our programs consist of only one function, main.
We also limit ourselves to simple expressions and assignment statements, which, along with the
std::cout statement are all we need to write programs. True, they are very limited programs, but
they still give your students a chance to make a lot of mistakes.
Because the students know next to nothing about programming at this point, we use the “trust me”
method of teaching several key elements.
For example, the executable part of the program begins with:
int main()
{
Rather than confuse the student with what this actually does, we say “trust me, it works”. Actually, we
are declaring the beginning of the function main. This function is just like any other C++ function
except that the special name causes C++ to call it first. All other functions are called directly or
indirectly from main.
At the end of the program we have another “trust me.” We end programs with:
Page 9
return(0);
}
Actually, this returns a value of zero to the caller (the operating system). The 0 indicates a successful
execution. If an error occurs we would return a positive number. The bigger the number, the more severe
the error.
Live Demonstration
Slide 14 tterm/tterm.cpp
Classroom Presentation Suggestions
Keep it simple. The students are about to write their first programs, and they will tend to have a lot of
very basic problems.
Review Questions
1. Name and define the three elements of a program.
Functions, data declarations, and comments.
2. Why are comments required in a program?
Without them programs are next to impossible to debug, maintain, or enhance.
Practical C++ Programming by manish baranwal
Start from the beginning
