Lecture 2: C++ Basics

Last time we have seen our first basic C++ program:

#include <iostream.h>

int main(void)
{
   // Defining a constant Pi
   const double Pi = 3.14;
   // Getting radius from user
   double r;
   cin >> r;
   // Computing volume of Sphere
   double V = 4.0 / 3.0 * Pi * r*r*r;
   // Displaying the output
   cout << V;
   return 0;
}

When you write C++ programs, there are several things to keep in mind:

your program does exactly and only what you define, no more and no less
a C++ program executes one line at a time, from top to bottom, unless specified otherwise
C++ programs can use cin to get input from a user, and cout to display output
C++ programs usually store some information
C++ programs usually do some computations

Let's look at this program now in detail. 

C++ Basics

There are a few basic rules for every C++ program:

Basic C++ Rules:

every C++ program uses certain keywords that have special meaning (e.g. int, main, void, return)
every C++ program uses certain operators that perform specific actions (e.g. +, *, cin, cout)
every C++ program is case-sensitive, i.e. the capitalization of keywords and operators counts (e.g. int is different from Int or INT)
every C++ program uses curly brackets to group statements together
every C++ program has a semicolor at the end of every instruction
almost every C++ program that handles user input and output will contain the line #include <iostream.h>
almost every c++ program will contain the lines

   int main(void)
   {
      // other stuff
      return 0;
   }

Let's focus on how information is stored, what type of information can be stored with a C++ program, and how to do it.

Declaring Variables

C++ provides several different basic data types. The most important ones are:
double: decimal numbers between a smallest, negative one and a largest, positive one
int: a whole number between a smallest, negative one and a sargest, positive one
char: a single letter or special symbol, anything that is on your keyboard

If you need to store information anywhere in your program, you need to tell the compile what type of info you plan to store and what name you want to use to refer to that information.In C++ lingo, you need to declare a variable:

Syntax for declaring a variable:

type varName [1, varName2, ..., varNameN];

where type is one of the basic (or other known) data types, and varName is the name of a variable. The variable can be declared anywhere in your program. You usually declare it just before it is needed, unless the variable represents something very important to your program in which case you declare it at the beginning of the program.

Example:

double x; 
int X, Y, Z; 
char cc;

This means that you can now use these variables to store and manipulate information.Note that x and X are two different variables, i.e. C++ is case-sensitive.

Assigning values to variables

Once a variable is declared, you can assign values to it, and use it in computations. To assign a value to a variable, the assignment operator = is used. Note that this operation looks like the math symbol for equal, but it works differently.

Syntax for assignment:

varName = expression

where varName is the name of a variable that has been declared previously, and expression is a valid C++ expression.

Example:

double x;
x = 10.0;

C++ Expression and Standard Operations

A valid C++ expression can consists of constants, declared variables, or combinations of constants, declared variables, and operations. The standard operations are +, -, *, /, and %, where % stands for the remainder after integer division. The usual order of operations applies, and can be modified using standard parenthesis.

Note: When you assign an expression to a variable, the following happens:

first, the value of the right side is computed
second, that computed value is assigned to the variable on the left

It is that order of events that make the assignment operator different from the mathematical equal sign.

Example:

double x, y;
int i, k;
x = 2.8;
y = -1.4*x;
i = 9;
k = (i % 2) * (7 + 5*i);

Combined Declaration and Assignment:

In C++, you can declare a new variable, and at the same time assign a value to it (or initialize the variable).

Example;

double x = 1.0;
int i = 10, j = 20;
int k = i + j;

Sometimes you also need a number that represents a particular constant (for example, Pi = 3.1415, or e = 2.71). C++ allows you to define constants like this:

Defining Constants

To define a constant in C++ you preface the type of the variable by the keyword const. The value of a variable declared as a constant can not change inside your program. Constants are usually declared at the beginning of your program, and they must be assigned a value at the time you declare them.

Example:

const double Pi = 3.1415;

Now we can produce some more interesting programs.


Task 1: Create a program that asks the user for the radius of a disk, then computes the area and circumference.

Stage 0: As usual, our stage-0 program is:

#include <iostream.h>

int main(void)
{
   // stuff
  return 0;
}

Stage 1: As usual, we use comments to break up our task into smaller subtasks:

#include <iostream.h>

int main(void)
{
   // get the radius from the user
   // compute the area
   // compute the circumference
   // display the answers
}

Stage 2: Now we get into the details of which variables and formulas to use:

#include <iostream.h>

int main(void)
{
   // need a variable r for the radius
   double r;
   // getting the input from the user
   cin >> r;
   // computing the area A = Pi * r^2
   double A = Pi * r^2;
   // computing the circumference C = 2 * Pi * r;
   double C = 2 * Pi * r;
   // displaying both answers
   cout << A;
   cout << C;
   return 0;
}

At this point, we let the compiler tell us if the C++ grammar is correct or not. The compiler will tell us the r^2 is "unknown", so we change that line to:

double A = Pi * r^2;

Also, the compiler will tell us that it does not know what Pi stands for. Therefore, we declare it as a constant at the beginning of our program:

const double Pi = 3.1415;

Then we compile it again. It will now compile, so we can link it to produce the executable file. Finally, we execute the program to test it, and we find that everything works, but it does not look good. So, we'll modify the program one more time:

Stage 3: We add some more input/output statements to make our program more "appealing" to the user:

#include <iostream.h>

int main(void)
{
   // defining the constants needed in this program
   const double Pi = 3.1415;
   // need a variable r for the radius
   double r;
   // getting the input from the user
   cout << "Please enter the radius: ";
   cin >> r;
   // computing the area A = Pi * r^2
   double A = Pi * r^2;
   // computing the circumference C = 2 * Pi * r;
   double C = 2 * Pi * r;
   // displaying both answers
   cout << "The area is: ";
   cout << A;
   cout << "The circumference is: ";
   cout << C;
   return 0;
}

Acutally, the last four statements can be linked together. Instead of saying:

   cout << "The area is: ";
   cout << A;
   cout << "The circumference is: ";
   cout << C;

we can also say:

  cout << "The area is: " << A << " and the circumference is " << C;

After changing that, our program will work correctly, and produce reasonably nice looking results on the screen.


Software Development

When creating a program, you usually proceed in four distinct stages:

Stage 1: Problem Analysis

In this stage you analyze what exactly it is that your program needs to accomplish. In particular, you describe:
all input values, i.e. values that must be supplied from outside the program
all constant values, i.e. values that are given with the problem
all output values, i.e. values that must be produced as part of the solution to the problem
You should also think about the types of all these values.

Stage 2: Design

In this stage, you break up the problem into subtasks and arrange them in the order necessary to produce an answer. You often pretend to write the program, using comments to describe the subtasks instead of actually coding it. All necessary formluas are part of this stage.

Stage 3: Coding

In this stage you actually enter the code for your program, following the rules that C++ requires. You should leave the comments from stage 2 intact, and put your code after the respective comments. The end product of this stage should be a program that compiles and links without errors.

Stage 4: Verification and Validation

You now need to check if your program works correctly, and if it looks and "feels" adequate. In other words, do all the algorithms and formulas work correctly ? Is the sequence of events correct ? Does your program print out enough information to guide the user as to what to do and what the output means ?

The most important stages are the Analysis and Design stages. If those are done carefully, the rest is often easy. In the coding stage, the compiler itself can help you getting the C++ syntax right, and you often have to go back and modify your program until you get it right. Also, the last stage will also require you to return to the code, modify it, and test it again. Let's look at some examples:


Task: You are working as a consultant for a cable company. For each installation that is performed by the company, there's a $25.00 service charge and an additional $2.00 charge per foot of cable used. They need a program to compute the total revenue generated by their installers per month. In other words, if they use 263 yards of cable at 27 different locations, they make $2253.00 in revenue.

(bgw)