[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 03
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] <03> [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Wed Jan 21 13:29:21 PST 2009

Contents


    03 First C++ Program

      Previous -- Introduction to Subject

      [ 02.html ]

      Prepare

        2.1 Introduction

        2.2 First Program -- printing a line of text

        Notice the Good Programming Practices and follow them in your work.

        Figure 2.1 A Program that prints text

        The comments are targeted at beginners. Normally we do not explain everything in detail. You put in enough extra comments to help your colleagues (and yourself) understand what the code is about.

        I'll be happy if you put three or four lines that say who you are, what the program should do, and how far it has progressed. You can also comment on things that are not obvious.

        You are allowed to have a single sheet of 8.5><11 piece of paper to aid your memory in quizzes and final. You might like to write down the outline of a small working program:

         #include <iostream>
         int main()
         {
         	std::cout << "Hello, World!\n";
         	return 0;
         }
        [ 03.cpp ] You need to get completely used to the C++ syntax.... including where semicolons (";") are needed and where they must be left out.A

        Why do we need iostream in every program

        Because a program without input and output (IO) is useless. We put it first so that the compiler knows the meaning of many special words and symbols that it defines before it meets them. And every where in the program. For example <iostream> defines the stream extraction operator ">>" is defined to extract data from a stream. An example being
         		cin >> number;

        Putting these definitions isdie a program is a syntax error!

        return 0;

        I read this as
         		terminate with zero errors.
        In UNIX the number you return from the main function is the the number of the error things go wrong.

        Later we will find another use for the return statement in C++.

        Figure 2.2 -- Escape Sequences

        This is a good place for a sticky note. You might like to add Newline and Horizontal tab to your Cheat Sheet.

        2.3 Modifying our first program

        All programs are modified, many times. Hence we make our code clear and amendable -- even if this takes extra effort!

        We change code because

        1. It does something it shouldn't -- -- --(debugging)
        2. It can't do do what it should do -- -- --(enhancement)
        3. It is not clear, clean, and simple -- -- --(refactoring)
        4. It is too slow, big, ... -- -- --(improvement)
        5. And other reasons I haven't thought of...

        2.4 Another program -- adding integers

        Take note of the new ideas:
        1. declarations
        2. identifiers
        3. variables
        4. types: int, double, char, ...
        5. prompts sent to the user
        6. inputs from the user go into variables

        Destruction of data in variables

        Many commands replace the value of a variable by a new value. The old value is "destroyed". You can not get it again. Suppose we have two int variables a and b with values 1 and 2 respectively. We may want to exchange the two values, and we might write:
         		a=b;
         		b=a;
        (which is good math!). But what happens? [ bad.cpp ] Here is a trace
        Table
        Commandab
        Initially12
        a=b;22
        b=a;22

        (Close Table)

        We destroyed the value of a with the command "a=b;".

        To exchange or swap two values, the standard technique is to add a variable to save the old value of one variable, like this:

         		int old_a=a;
         		a=b;
         		b=old_a;
        Here is how it works
        Table
        Commandab
        Initially12old_a
        int old_a=a;121
        a=b;221
        b=old_a;211

        (Close Table)

        Good Programming Practice 2.12

        I've used both styles: declare all variables first vs declare just before use. I will except either in your code.... but not BOTH.

        Declare one variable per line

        because you will make fewer mistakes!

        Finding errors without compiling

        1. Look at the code with a jaundiced eye.
        2. Get others to look at the code and ask them to inspect it for errors.
        3. Pretend to be a computer and walkthrough or trace the code, step by step keeping track of the value of every value.

        2.5 Memory Concepts

        Memory is like a gigantic piece of paper with lots of small boxes (locations) where you can put numbers and other data. These boxes are numbered and the computer actually uses these numbers (addresses) to determine which data to use and which to change. In C++, and most languages, we don't have to use numbers to address data but declare variables instead. A variable is the name of a box(location). A ">>" puts data into a box, "+" gets data from two boxes, into the CPU and a "<<" copies it out to the user.

        Self Review Exercises -- If you have time

        Pages 75-77, Exercises 2.1-2.3.

      . . . . . . . . . ( end of section Prepare) <<Contents | End>>

      Deliver -- a question on one of the above sections

      Demo -- walkthrough or trace of figure 2.5 Page 50

      Exercises -- probably from 2.7 to 2.13

      TBA

      Next -- C++ Elements etc

      [ 04.html ] [ lab02/ ]

    . . . . . . . . . ( end of section 03 First C++ Program) <<Contents | End>>

    Abbreviations

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  3. KDE::="Kommon Desktop Environment".
  4. OOP::="Object-Oriented Programming", Current paradigm for programming.
  5. Semantics::=Rules determining the meaning of correct statements in a language.
  6. SP::="Structured Programming", a previous paradigm for programming.
  7. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  8. Q::software="A program I wrote to make software easier to develop",
  9. TBA::="To Be Announced", something I should do.
  10. TBD::="To Be Done", something you have to do.
  11. UML::="Unified Modeling Language".

End