ADVANCED PLACEMENT Computer Science - C++

 LESSON 1:  A SIMPLE C++ PROGRAM

The key topics for this lesson are:

A.    A Simple C++ Program

B.    Programming Paradigms

C.    Compiling and Running a Program

D.    Printing Source Codes and Run Outputs

 

VOCABULARY:                    

FUNCTION  STREAMS
COUT DOCUMENTATION
OBJECT 

IMPERATIVE LANGUAGE

  OBJECT-ORIENTED PROGRAMMING

 DISCUSSION:                        A.    A Simple C++ Program

 1.    The following is a complete C++ program:

 Program 1-1

//  A first C++ program

#include <iostream.h>

main ()

{

         cout << "Hello world!";

         return 0;

}

 2.    The first line of the program is a documentation statement.  Programming languages allow the inclusion of comments that are not part of the actual code.  Documentation is an important aspect of writing programs as it helps the reader to understand what is going on.  C++ allows for two forms of documentation:

  /*  This style will allow you to write longer documentation notes as the text can wrap around to succeeding lines.  */

       //     This style automatically ends at the end of the line.

3.    A function in C++ is defined as a block of code which can have input and output, and executes a sequence of statements inside the function.

4.    Every C++ program consists of a main function.  A function has the following general syntax:

Return type   FunctionName (parameter list)

       {

             statements

       }

a.     The return type refers to the type of data a function returns.  The data type can be one of the predefined types (integer, real, char) or a user-defined type. 

b.    The function name is the name of the function.  In the case of program 1-1, the name of the function is main.

c.     The parameter list will allow us to send values to a function.  This will be covered in future lessons.

d.    The statements accomplish the work of the function.

5.    The text constant "Hello world!" is sent to cout which represents the output screen.

6.    The statement at the end of function main, return 0, satisfies the requirement that the function return an integer value.  If a function does not specify a return type it is assumed to be an integer type.  Most C++ programmers choose to return a zero (0) value.

7.    To send multiple lines to the screen, add either the newline character escape sequence ("\n") or use endl.

cout << "This will appear as the first line\n";

cout << "While this will appear as a second line.\n";

A second option:

cout << "This will also work to send two lines" << endl;

cout << "The endl functions as a line terminator." << endl;

B.    Programming Paradigms

1.    Programming languages fall into two broad categories:  imperative languages and object-oriented languages. 

2.    Imperative languages involve working with program control.  The focus of imperative programming is logic and action. 

       The main tools of imperative programmers are control structures (if-else, loops, etc.) and the concept of a module (procedures in Pascal, functions in C).

3.    Object-oriented programming will focus more on the nouns of the problem, the data if you like.  Programmers who use object-oriented programming (OOP) attempt to create user-defined data types to model real-world objects.

4.    Objects are an important feature of the C++ language.  An object is defined as a software model for a real-world object.  An object consists of two parts:  attributes and behaviors.  For example, a window in a computer operating system must maintain certain data attributes:  size, name, colors, etc.  A window also has certain behaviors or abilities such as moving, re-sizing, opening up, closing, etc.  Object-oriented programming (OOP) models real-world objects using software representations.

5.    This curriculum will introduce the concept, vocabulary, and strategies of object-oriented programming.  We will not cover inheritance and the more advanced features of OOP, but it is valuable to learn about objects and the C++ tools for abstraction.  As we learn C++, it will be necessary to describe features of the language using OOP terms._

6.    The compiler directive #include <iostream.h> instructs the compiler to include the code from the iostream.h library.  This library will declare the cout object and implement the output operator (<<).

7.    The iostream.h library contains objects and operations useful to solve stream processing.  Streams can be thought of as streams of characters flowing from one location to another.

8.    Programmers try to avoid "reinventing the wheel" by using predefined libraries of code.  Object-oriented programming encourages recycling of code.

C.   Compiling and Running a Program

1.    The source code refers to the C++ program that you will write.  This source code will need to be saved to disk for future reference.

2.    Compiling is the process of converting a program written in a high-level language into the binary language the computer recognizes.  The object code is the result of compiling the source code into a machine language version. 

3.    The C++ compiler will compile your source code into binary language, then link the objects and operations requested in the #include compiler directives.  Your C++ environment will probably save the resulting object code on disk. 

D.   Printing Source Codes and Run Outputs

1.    Printing the source code is accomplished by invoking a system dependent command which varies according to the computer system and C++ compiler being used. 

2.     The run output is the correct text and numbers that result from running the program.  After confirming the results on the screen you will need to copy and paste this output to your source code. 

See HA1-2-PP1.htm

SUMMARY/REVIEW:          In these first few lessons we are learning some of the fundamentals of programming languages.  The iostream.h library contains many abilities which will be covered in Lesson 3.

ASSIGNMENT:                    
Lab Exercise, L.A.1.1, Schedule

Lab Exercise, L.A.1.2, Address

Lab Exercise, L.A.1.3, Address and Schedule