C++ Programming Basics

Programming on Unix

 

Getting started with UNIX

•      Multiuser system

–   Each user gets a slice of the CPU’s time

–   You must log on to tell the OS you are a legal user

–   The CS department’s computer’s name is esus.

•      Security

–   Since each user has their own disk space, it is necessary to protect files from open access

–   Permissions allow you to protect your files from other esus users.

 

Linux

•      Linux is a free version of UNIX distributed by GNU (The Free Software Foundation).

•      The source code was released, encouraging hundreds of programmers to add to it and make changes.

•      The computers in 254 EPS dual boot; they can run either the Windows OS or Linux

•      Be sure your machine is booted with Linux.

 

Before you start

•      You must have an esus account 

–   Login name and password

–   These are set up automatically for all CS students

•      Changing your password

–   passwd changes the password

•      erase key (either backspace or delete)

•      interrupt key (control-C)

•      Remember, UXIX is case sensitive

 

Writing your programs on Linux

•      First must decide on a text editor

–   I will show you pico; other choices are emacs and vi

•      After the program is written, it must be compiled

–   We will use the g++ compiler in lab

–   On your own computer, you may use any C++ compiler you want, but the source code must port to lab and the g++ compiler

–   If you keep your code standard, there should not be too much problem with porting the code.

 

 

A Simple C++ Program

#include <iostream>
using namespace std;
int main( )
{
     cout <<“First program for CS 210”<<endl;
     return 0;
}

 

Note this about the program

•      Functions

–   The main( ) function is not inside a class like a Java program

•   Functions are one of the fundamental building blocks of C++ (not always classes)

–   Like Java,

•   parentheses follow the function name

•   the function has a return type

•   the body of the function is surrounded by braces

•   execution of the program will begin with main( )

•      Whitespace is not important to the compiler

•      Terminal output is done using the cout object and the << (the insertion operator)

•      Similarly, terminal input uses cin and the >> (extraction operator)

•      The preprocessor

–    Both #include <iostream> and using namespace std; are directives to the compiler

–    An #include directive substitutes an entire file for the #include line

 

 

Primitive types in C++

•      Integer types

–  short, int, long

•   How many bits for each is implementation dependant

–   Each can be modified by signed or unsigned

•   They are signed by default

•      Floating point types

–  float, double

•      Character type char

•      Boolean type  called bool in C++

 

Character and string constants

•      Character constants are enclosed in single quotes

–   Example char middleInitial = ‘M’;

•      String constants are enclosed in double quotes

–   Example string st = “this is a string”;

–   To use strings, include the <string> header

 

 

Branching and loops

•      If/else statement is identical to Java except:

–   In C++ an integer is also a boolean; zero is false an any other integer is true

–   The switch statement is also the same

•      Loops are the same as in java

–   Possibilities:  for loop, while loop, and do loop

–   I advise against using break and continue

 

Assigning values to variables

•      The Java compiler will not let you fail to give variables an initial value

•      The C++ compiler will not give an error message if a variable is not initialized

–   This can lead to errors, since there is always something in every byte of RAM

–   Many C++ compilers will give a warning

 

Simple Input/Output in C++

•      Use cout << to send data to the screen

–   A linefeed can be sent in two ways

•   \n within a string:  “type this\n second line”

•   endl outside a string:  cout << “string” <<endl;

•      Use cin >> to read from the keyboard

–   Use:  cin >> variable_name;

•      Both >> and << can be cascaded

–   You can use more than one insertion or extraction operator in a statement

 

Manipulators

•      Manipulators are used with the insertion operator << to modify the way output is displayed.

–   endl is a manipulator

–    The setw(n) manipulator

•    This makes the field n characters wide

•      You must include the iomanip header file to use setw(n)

–   #include <iomanip>

•      There is a table of the manipulators on page 572 of the Lafore book

 

Escape Sequences

•      Escape sequences can also be used to format output.

    An example is the \n

•      Escape sequences always start with a backslash

•      They can be used either as separate characters or embedded in a string constant

•      There is a table of common escape sequences on page  44 of Lafore.