Lecture 2: C++ Basics
Last time we have seen our first basic C++ program:
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.
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|
// other stuff
Let's focus on how information is stored, what type of information can be stored with a C++ program, and how to do it.
|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.
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.
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.
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).|
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:
|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.|
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:
Stage 1: As usual, we use comments to break up our task into smaller subtasks:
Stage 2: Now we get into the details of which variables and formulas to use:
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:
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:
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:
Acutally, the last four statements can be linked together. Instead of saying:
we can also say:
After changing that, our program will work correctly, and produce reasonably nice
looking results on the screen.
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|
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.