PART 1

Language Overview

Introduction and justification.

Microsoft Visual C++ development environment.

Tutorial examples of C++ programs.

Basic language philosophy.

Examples of variables, constants, character processing, arithmetic, functions, and control structures.

Input/Output basics in C++

INTRODUCTION TO C++

Designed and developed by Bjarne Stroustrup at AT&T Bell Labs in the early 1980's

Initially released in late 1985 - by mid-1986 already had 24 systems with commercial ports of C++, version 1.0

Intended to be an add-on pre-processor for a conventional C compiler - Improve and extend C - 1988 saw the first native C++ compilers

The original C++ language reference was: The C++ Programming Language by Bjarne Stroustrup – 1986. It applied to AT&T versions 1.0, 1.1, and 1.2.

As the language evolved, C++ language references were: The Annotated C++ Reference Manual by M. A. Ellis and Bjarne Stroustrup - 1990. for versions 2.0 and 2.1, and The C++ Programming Language by Bjarne Stroustrup - 1991 for version 3.0

The language has evolved through many versions since 1985.
- AT&T versions 1.0, 1.1, 1.2, 2.0, 2.1, 3.0, and 4.0
- Various ANSI draft standards, and ANSI standard

The ANSI standards committee for C++ (X3J16) started on the standardization process in December, 1989. The process was extended when the C++ standards committee became a joint ANSI/ISO committee in January, 1992. A standard was adopted in January, 1998.

Not all versions of the language are compatible in all respects. Be careful when selecting and using a compiler to understand what standard the compiler is using and how it differs from the current ANSI standard.

WHY IS C++ POPULAR ?

Provides a fairly complete set of facilities for dealing with a wide variety of applications.

All the useful data types, including pointers and strings.

A rich set of operators and control structures.

A powerful and flexible I/O class library

Is a small language with built-in data types and operators that are closely related to most assemblers. There need only be a small "semantic gap" between C++ and the computer hardware.

A growing number of C++ programs and C++ programmers.

With care and thought, C++ programs can be made portable.

Compilers are available for a wide variety of machines, from micros to mainframes.

Useful as an assembler replacement, increasing software quality and programmer productivity, thus retaining the low-level strengths of C

Provides support for creating and using data abstractions

Provides support for object-oriented design and programming

Provides improvements for certain C constructs, and for input/output

Takes almost nothing away from C, while adding enormous capability

Retains and improves organization capabilities of C

Once you learn C++, Java is relatively easy to learn

DISADVANTAGES OF C++

Can write cryptic and "unreadable" programs.

Operands and functions can have "side effects."

Not a "beginner's" language.

Discipline is left to programmers.

If it was easy to "shoot yourself in the foot" with C, then C++ straps a loaded uzi to your leg

"Like walking blindfolded through an Indiana Jones adventure" - Ladd

C++ assumes you know exactly what you are doing - almost anything is legal in some sense - compiler/preprocessor just follows directions

Need substantial programming experience before venturing into C++

Can migrate a C program forward to C++, but virtually impossible to migrate a C++ program back to C

Many of the arcane language "idioms" and "exceptions" of C are brought forward into C++

Learning about pointers, pointer arithmetic, and operator overloading can be challenging

Programmer is totally responsible for memory management – need to know what memory is allocated and how it is allocated – need to recover all allocated memory when a variable goes out of scope or there will be a memory "leak"

CHARACTERISTICS OF C++

Has built-in data types, but not strongly typed like Pascal or Java, more strongly typed than C

Has high level for support of objects, but ultimately requires specification at a C programming level, which is just slightly lower than FORTRAN, Basic, COBOL, Java, or Pascal

A medium sized language, has a fixed set of operator tokens, does not have exponentiation, does not have input/output statements, does not have complex numbers built in

General purpose, but optimized for system programming

Portable, but most compatible with UNIX

Remarkably standard - because of mentors

Object oriented - can define objects, and the operators and functions to process them

Supports structured programming - can write most user-specific code at a high level, while still maintaining low-level control in the called functions

Supports data abstraction - allows designers to think about the types of operations they would like to provide for a data item, rather than the statements that the programming language supports

hello.cpp

/* HELLO.CPP: First C++ program - after KR, page 6 */

#include <iostream>

main()

{

std::cout < "hello, world\n";

return 0;

}

hello.cpp EXPLANATION

All C++ programs consist of one or more files containing one or more "functions" each.

One form of comments are begun with /* and ended with */. Comments may appear anywhere white space is allowed and should be used freely. Comments of this form may not be nested, and should be reserved for opening comments at the beginning of each function and for commenting out code.

An include statement must be used whenever any library function, any class in the class libraries, or any object defined in the class libraries (in this case cout from the std library) is used.

All C++ programs must contain exactly one function named main, which is where the program begins execution.

In general, mainwill call other functions. Some are system-supplied library functions for mathematics, input/output, and system interaction. Others are written by the programmer.

Braces { } enclose the statement(s) that make up the function. They are also used to group a set of statements into a single statement where a single statement is required. Statements inside braces are called a block. (In other words, C++ is a block-structured language.)

A function is invoked by using its name. There is no CALL statement as there is in some other programming languages.

The C library of input/output functions is included for compatibility in C++, but C++ provides its own set of input/output functions. These can only be intermixed with care. (See the input/output section.)

The string "hello, world\n" is a string constant containing 13 characters - the \n represents a single character, a “newline” character which is a line feed or a carriage return or both in either order, depending upon what the system prefers.

Statements are terminated by a ;

CREATING A C++ PROGRAM

A "typical" C++ source program consists of one or more user created text files. These files usually have a .cpp file extension (or .cxx on some systems). Each of the files contains one or more user-defined C++ functions. These files are created using a separate or integrated text editor.

In addition to the .cpp files, longer and more complex programs also contain one or more user created header files. These files usually have a .h file extension (or .hpp or .hxx on some systems).

Some integrated development environments organize all of the files necessary to generate a running program into a project.

In one of the (or the only) source files there is exactly one function called main. The function main is the entry point into the C++ program. The function main calls the other user defined functions in order to accomplish the intended task of the C++ program.

There are no input/output statements in the C++ language. Instead, use is made of the built-in I/O class library. For example, function calls can be made to the I/O functions in the C++ class library. In our first program, the < operator was “overloaded” to provide “stream output”. (C++ provides for much more input/output control than C.)

COMPILING AND RUNNING A C++ PROGRAM

The following steps are required to compile, link, and run a C++ program:

1. Compile the program. The compiler will:

a. Preprocess each xxx.cpp file

- define constants

- expand macros

- include .h files (especially function and class declarations)

- conditionally retain source code

b. Translate C++ source code into relocatable binary machine code (object files)

- named xxx.o (UNIX) or xxx.obj

- each file can be compiled separately

c. Generate code for a function _main

- _main sets up all static variables and arrays

- _main calls main

- _main passes to main an array of strings, each string

containing one command line parameter, which main can

choose to ignore

- _main will process an integer return value as a message to the

operating system

2. Link the program. The linker will:

a. Obtain all specified object files from various compiles

b. Obtain all necessary precompiled library function object files

c. Produce an executable file named a.out (UNIX default) or xxx.exe

d. (UNIX only) Perform cleanup by deleting xxx.o

3. Run the program.

Place the program into execution

EXAMPLE ENVIRNMENTS

UNIX

vi hello.cpp / creates source code file
cpp hello.cpp / compiles and links program, generating binary file a.out
a.out / executes program a.out

VAX/VMS

edit hello.cxx / creates source code file
define lnk$library -
sys$library:vaxcrtl / allows VAX C Run-Time Library to be found during linking
cxx hello / compiles hello.cxx, generating hello.obj
link hello / links hello.obj and vaxcrtl, generating hello.exe
run hello / runs program hello.exe

Borland C++ (DOS Command Line)

edit hello.cpp / creates source code file
tcc hello / compiles hello.cpp, generating hello.obj, and links hello.obj, generating hello.exe
hello / runs program hello.exe

Borland C++ (Integrated Development Envirnment)

double-click Borland C++ icon / launch Borland C++
Alt-F/O/hello.cpp / create source code file
held right click on source code window /target expert/easy win / configure the development environment to generate a terminal-style text interaction using a DOS window
Alt-F9 / compile hello.cpp, generating hello.obj
F9 / compile (if necessary) and link hello.obj, generating hello.exe
Control-F9 / compile and/or link as necessary and execute hello.exe

USE OF MICROSOFT VISUAL C++ 6.0 - SETUP

1. Turn on the computer (if necessary)

2. Log into Windows NT

a. if you are a UST student, use your UST domain username and password and select the UST domain

b. if you don not have an account, enter

Username: JAVA

Password: JAVA2

Domain: GPS

c. if the network is down, see your instructor for a local machine username, password, and domain

3. Start Microsoft Visual C++

Start/Programs/Microsoft Visual C++ 6.0/Microsoft Visual C++ 6.0

Close Tip of the Day

4. Create a Workspace (if you haven't created one already)

File/New/Workspaces

in Workspace name, type in your first initial, middle initial, last name, and a digit representing the chapter in the notes you are working on. For example, I would enter tpsturm1

Click OK

5. Create a project in the workspace (you will need a separate project for each program that you write)

File/New/Projects

in the Project_name, enter a name for the project. A good name is the name of the .cpp file containing main(), without the .cpp file extension

select Add to current workspace

select Win32 Console Application

click OK

select An empty project

click finish, click OK

USE OF MICROSOFT VISUAL C++ 6.0

EDIT / COMPILE / RUN

6. Place a C++ source code file in the project

a. If the source code file already exists

- project / add to project / files ...

- select file, perhaps after navigating the system or network (instructor files are located at \\GPS-LAB2\Sturm\Cp3progs)

- in the insert into box, select the project for the file

- click OK

b. If you wish to create a new source code file

- project / add to project / new ... / files

- select C++ Source File (or C++ Header File)

- place check mark in Add to project:

- below Add to project: select the project for the file

- in the File name: box, enter the source code file name

- click OK

- enter your C++ code in the blank editing window

7. Compile, link, and test the program

- Build / Execute (filename).cpp

or

- Control-F5

At any "file does not exist" box, click yes

Any warnings or errors will show up in a scrolling message window at the bottom

An MSDos window will appear with the program interaction and/or output.

After the last line of output, the message "Press any key to continue" will appear

MICROSOFT VISUAL C++ 6.0 MAIN SCREEN


MICROSOFT VISUAL C++ 6.0 MAIN MENU

When the main menu is highlighted, pressing Alt-(first key of the menu item name) selects that menu item as follows:

F / file / file and workspace selection, open, print, save ...
E / edit / cut, paste, copy, delete, undo, redo, find ...
V / view / resources, workspaces, output, properties ...
I / insert / resources ...
P / project / set active project, add to project, settings ...
B / build / compile, run, debug program, build project ...
T / tool / customizable development tools, macros ...
W / window / window open, arrange, list, split, movement ...
H / help / view on-line help ...

MICROSOFT VISUAL C++ 6.0

Default System Keystroke Mapping

To /

Press

Activate the selected item and changes to edit mode. In an editor, displays the Object Browser. / F2
Close a menu or dialog box, cancel an operation in progress, or place focus in the current document window. / ESC
Close a tool window. / SHIFT+ESC
Close the current MDI child window. / CTRL+F4
Create a new file. / CTRL+SHIFT+N
Create a new project. / CTRL+N
Cycle through the MDI child windows one window at a time / CTRL+F6 or CTRL+TAB
Display a system menu for an MDI child window. / ALT+HYPHEN (-)
Display a system menu for the application window. / ALT+SPACEBAR
Display the Add Item dialog box where you can add new or existing items to your project. / CTRL+D
Display the Auto window and move the cursor into it. / CTRL+ALT+A
Display the Call Stack window and move the cursor into it. / CTRL+ALT+C
Display the Document Outline window and move the cursor into it. / CTRL+ALT+T
Display the Find window. / CTRL+H
Display the Find window. If there is no current Find criteria, put the word under your cursor in the Find box. / CTRL+F
Display the Immediate window and move the cursor into it. Not available if you are in the Text Editor window. / CTRL+ALT+I
Display the Locals window and move the cursor into it. / CTRL+ALT+L
Display the Object Browser and move the cursor into from anywhere but an editor. / CTRL+ALT+B
Display the Output window and move the cursor into it. / CTRL+ALT+O
Display the Project Explorer and move the cursor into it. / CTRL+ALT+J
Display the Properties window and move the cursor into it. / CTRL+ALT+P
Display the Properties window. / F4
Display the Property Pages window. / SHIFT+F4 or ALT+ENTER
Display the Running Documents window and move the cursor into it. / CTRL+ALT+D
Display the shortcut menu. / SHIFT+F10
Display the Task List window and move the cursor into it. / CTRL+ALT+K
Display the Thread window and move the cursor into it. / CTRL+ALT+H
Display the Toolbox and move the cursor into it. / CTRL+ALT+X
Display the Watch window and move the cursor into it. / CTRL+ALT+W
Find next occurrence of specified text. / F3
Go to the next tool window. / ALT+F6
Go to the previous tool window. / ALT+SHIFT+F6
Move to the next MDI child window. / CTRL+SHIFT+F6
Move to the previous MDI child window. / CTRL+SHIFT+TAB
Move to the next page of items in the current tab if there are more items than can be shown at one time. / PAGE UP
Move to the previous page of items in the current tab if there are more items than can be shown at one time. / PAGE DOWN
New File / CTRL+SHIFT+N
Open a file. / CTRL+SHIFT+O
Open a project. / CTRL+O
Print all or part of the document. / CTRL+P
Run an application using the debugger. / F5
Save all of the files, project, or documents. / CTRL+SHIFT+S
Save the current document or selected item or items. / CTRL+S
Select All. / CTRL+A
Stop Break into a running an application. / CTRL+BREAK

Use the following key combinations in all Windows-based applications:

To / Press
Close the active application; if all windows are closed, close the development environment. / ALT+F4
Copy the selection to the Clipboard. / CTRL+C or CTRL+INS
Cut the selection and move it to the Clipboard. / CTRL+X or SHIFT+DELETE
Display documentation. / F1
Display the shortcut menu. / SHIFT+F10
Insert the Clipboard contents at the insertion point. / CTRL+V or SHIFT+INSERT
Move between the last two active windows. / ALT+F6
Paste the Clipboard contents at the insertion point. / CTRL+V or SHIFT+INSERT
Restore the previously undone action (Redo). / CTRL+SHIFT+Z or ALT+SHIFT+BACKSPACE
Undo the last editing action in the current line. / CTRL+Z or ALT+BACKSPACE

Learning the Development Environment