Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ Basics (Part 1)

Structure of a C++ Program

Building and Running a C++ Program

Starts with source code, like the first sample program
  1. Pre-processing
    • The #include directive is an example of a pre-processor directive (anything starting with #).
    • #include <iostream> tells the preprocessor to copy the standard I/O stream library header file into the program
  2. Compiling
    • Syntax checking, translation of source code into object code (i.e. machine language). Not yet an executable program.
  3. Linking
    • Puts together any object code files that make up a program, as well as attaching pre-compiled library implementation code (like the standard I/O library implementation, in this example)
    • End result is a final target -- like an executable program
  4. Run it!

Variables

Variables are used to store data. Every C++ variable has a: When a program is compiled, a symbol table is created, mapping each variable name to its type, size, and address

Identifiers

Identifiers are the names for things (variables, functions, etc) in the language. Some identifiers are built-in, and others can be created by the programmer.

Style-conventions (for indentifiers)


Declaring Variables


Atomic Data Types

Atomic data types are the built-in types defined by the C++ language

Sizes

This program will print the number of bytes used by each type - sizes may vary from system to system
 

Initializing Variables

More Declaration Examples, using various types
 

Constant Variables

(Woohoo! An oxymoron!)

Symbolic Constants (an alternative)

Questions to consider


Other Basic Code Elements

Literals

Literals are also constants. They are literal values written in code.

Escape Sequences


 

Comments

Comments are for documenting programs. They are ignored by the compiler.

Input and Output with streams in C++:

In C++ we use do I/O with "stream objects", which are tied to various input/output devices.

Some pre-defined stream objects

These stream objects are defined in the iostream library

To use these streams, we need to include the iostream library into our programs.

 #include <iostream>
 using namespace std;
The using statement tells the compiler that all uses of these names (cout, cin, etc) will come from the "standard" namespace.

Using Input and Output Streams

Some special formatting for decimal numbers

Some simple program examples