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
  2. Compiling
  3. Linking
  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

NOTE: You'll most likely need to #include the library iomanip to use these techniques.

Some simple program examples