[Phy 405/905 Home Page] [ Lecturer ]

0. Writing, compiling, and running C++ programs

In the introductory lecture, I gave a rapid (maybe terrifyingly so) overview of C and C++, and some examples of the object-oriented features of the latter. Here we begin a much slower, thorough introduction to the subject. Starting with the basics of compiling programs under VMS (msupa.pa.msu.edu) and UNIX (pads1.pa.msu.edu), a simple program to print out the command-line arguments introduces identifiers, literals, functions (and function overloading).

Also bear in mind that I'll sprinkle definitions of terms which will sometimes seem rather elementary to you, but not to others - due to the very large spread in computer-programming experience in our class.


C++ compilation basics

Files and filenames

C++ is usually a compiled language (in contrast to BASIC, which is traditionally an interpreted language). This means that after you have written your program and typed it into one or more files, you will need to compile each of the files (together or separately) and then link all the files together. Here's some definitions in case you're fuzzy on the concepts:
compile
To reduce a file containing C++ code into a form the computer can directly execute, i.e., machine language. The original file is a source code file while the end result is an object file.
link
To combine several object files together into a single executable file - a "binary". This is less trivial than it sounds, as we shall see when we discuss the scope of objects.
The details of this process are different in UNIX (e.g., pads1.pa.msu.edu) and VMS (e.g., msupa.pa.msu.edu), but the smallest unit of compilation for both is a single file.

Unix:

VMS:

We are discussing here having the shell (csh or Bourne under UNIX, DCL under VMS) run your program for you, but of course it could be called directly by the operating system on a lower-level, or by an application program such as Mathematica. Just bear the possibility in mind.

Editing


Programming Basics

As will be the pattern this semester, I'll present a couple concepts in detail, then take a look at a simple illustrative example. In this lecture, we'll next define identifier and literal, then learn a little on printing to the screen.

Identifier

identifier
Basically a "legal" name in C++, be it for words reserved for use by the language (keyword) or as the name of a variable, function, etc.

Besides the wide variety of punctuation marks used by C++ (&, *, $, ..., etc.), identifiers are the "words" making up the "sentences" in your source code.

Identifiers:

Literal

Next up is the literal, which I'm bringing up because
  1. we use them in the simple program that follows
  2. they provide a convenient warm-up to discussion of the basic types in C++.

Literals are what are often called "constants" in other languages, but since C++ has a precise idea of what a constant is, let's stick with literal.

literal
An explicit value (no associated identifier) of one of the types described below.

There are different kinds of literals, roughly corresponding to the basic types (numeric and character) as well as an array type (character arrays). There are:

The first three types are broken down into more specific categories according to the number of bits used to represent the literal, and whether they are signed or not.

Just a reminder -

Integer literals

Floating-point literals

Character literals

The last type of literal doesn't correspond to a basic type but instead is an array of a basic type:

Strings (Character array literals)

First Program

So, armed with the concept of a literal, here's a simple program (our first!) that outputs the various types of literals (as well as the odd variable:

// first_prog.cc 
//
#include <iostream.h> // to get definitions for cout, operator<<. etc

// int main() // (1) alternative version of main()

int main(int argc, char *argv[]) // (2) version of main() needed here
// argc-1 = # of commmand-line args
{


cout << 34.0 << '\n'; /* output float, then char literal */
cout << "Hello world\n";
cout << "# of command-line args = " << argc <<'\n'; // output variable!
cout << 'H' << "i there, my "<< 0xFF << "friends\n"; //mix and match
return 0; // return statement

}

Things to note: [ Phy 405/905 Home Page] [Lecturer ]