Lesson 1
Introduction to Computing in C++

This lesson is an introduction to computing and to the language C++. We will focus attention on telling computers how to do things. We will get an overall view of languages and how programs are put together. We'll also examine a simple C++ program to get a sense of what the language C++ looks like.

Reading assignment
Chapter 1: Object-oriented program development
Chapter 2, Section 2.1: Line-by-line program analysis


What is a computer language?

A computer program is a set of instructions for a computer to perform a certain task.

Each computer can follow instructions written in its native machine language. People write programs in high-level languages.

Machine languages
  • Differ among computers
  • Hard to write and read

    High-Level Languages
  • Designed for people to write and read
  • Can be created via a text editor
  • Used on different machines
  • Examples: C, C++, Pascal, Java, Fortran90, Lisp

    The term code refers to instructions written in a computer language. Machine language code is stored in binary files as opposed to text files, which can be viewed and modified in a text editor.


    Compiling a program

    A compiler is a program that translates high-level language code into machine language code.

    Programs consist of:
  • code that you write
  • libraries of code that others (or you) have written
  • A library is a collection of code to perform a set of related tasks.

    Compilers work in steps (phases):
    1. translate source code to object code
    2. link object code with appropriate libraries

    Compiler output is an executable file, which is a set of computer instructions in machine language.

    Self Test Question


    What is C++?

    C++ is a high-level language
  • C++ is a superset of C
  • C designed by Dennis Ritchie of Bell labs in 1972 -- essential form of modern C by 1980
  • C++ designed by Bjourne Stroustrup in mid 1980's

    Goals of C++
  • Make programming more enjoyable for serious programmers
  • Provide a general-purpose language better than C
  • Support data abstraction (separation of information from the details of how it is represented)
  • Support object-oriented program development
  •  


    A small C++ program

    // Our First C++ Program               Line 1
    #include <iostream.h>               // Line 2
                                        // Line 3
    void main()                         // Line 4
    {                                   // Line 5
       cout << "Hello World" << endl;   // Line 6
    }                                   // Line 7
    
    1. Line 1: comment begins with //
    2. Line 2: use the C++ I/O stream library
    3. Line 3: blank lines are ignored
    4. Line 4: the function named main
    5. Line 5: beginning brace of main function body
    6. Line 6: statement indicating what is to be done when the program executes
    7. Line 7: ending brace of main function body

    There is one statement in main()

    cout is the standard output stream
    << is the output operator
    "Hello World" is a string literal
    endl outputs a newline
    ; is the statement terminator
    You can create the source code for this program with any text editor. Save the source code under the file name hello.C or hello.cpp. Then compile it. From a command-line environment such as UNIX, the compile command will look something like this (where % is the operating system prompt.)
      %  CC -o hello hello.C
    Executing the code generated by the compiler in that same command-line environment requires this command.
      % hello
    When the code executes, it will print the message Hello World on the terminal.

    Self Test Question