Origin of C++

A simple C++ Program:

#include <iostream.h>
int main()
{
cout << "Hello World!\n";
return 0;
}

Another simple C++ program (pg. 21):

#include <iostream.h>
int main( )
{
int number_of_pods, peas_per_pod, total_peas;
cout << "Press return after entering a number.\n";
cout << "Enter the number of pods.\n";
cin >> number_of_pods;
cout << "Enter the number of peas in a pod.\n";
cin >> peas_per_pod;
total_peas = number_of_pods * peas_per_pod;
cout << "If you have ";
cout << number_of_pods;
cout << " pea pods\n";
cout << "and ";
cout << peas_per_pod;
cout << " peas in each pod, then\n";
cout << "you have ";
cout << total_peas;
cout << " peas in all the pods.\n";
return 0;
}

 

Developing a C++ Program

An iterative process involving the following three steps:

  1. Use a text editor to write your program

  2. Use a program called a compiler to transform what you've written into machine language (referred to as object code).

  3. Execute the object code on suitable input to test correctness.

 

Basic structure (pg. 25):

#include <iostream.h>

int main()
{
Variable_Declarations
Statement_1;
Statement_2;
...
Statement_n;
return 0;
}

When developing C++ code remember to:

 

Variables:

e.g: weight, total_weight

Note: Always use meaningful names for variables. It is good programming practice, it makes your code easier to read and understand.

There are a few restrictions for identifiers:

Example: int, double

Example: cin, cout

Note : C++ is case sensitive. Hence, count, COUNT and Count are all different identifiers.

Variable Declarations

Every variable must be declared before it is used. This tells the compiler what type of values you want the variable to hold. This is important so that the compiler knows how much space to allocate your variable since each type requires a different amount of memory. The general form for a variable declaration is:

type_name <variable_1>, <variable_2>, ... ;

Example:

int count;
int account_number;
double prime_rate, interest_rate;

Note: "int" - integers, "double" - floating point. Initially, the floating-point type was "float". "double" uses twice as much the memory as "float" and was introduced for precision. Given cheap memory, "double" is now the standard. "float" is still available, as well as an even more precise "long double", which will not be used in this class.

Assignment Statements

Assignment statements are used to change the value of a variable. The general form of an assignment statement is:

<variable> = <expression>;

Example:

total_weight = weight * number_of_items;
total_weight = weight;
number_of_items = 16;


Evaluating Assignment Statements:

First evaluate the expression on the right-hand side of the "="then stores the computed value in the variable named on the left. The expression on the right-hand side can be constant, another variable, or an expression involving variables and operators.

Variable Initialization

A variable has no meaning until a program gives it one. Consider the following program fragment:

int main ()
{
double prime_rate, interest_rate;
interest_rate = prime_rate + 9.9;
...
return 0;

}

Although syntactically valid, execution will be unpredictable, since prime_rate has no initial value (in fact the value of prime_rate is likely to be some value left at its memory location by a previous program that used that memory location). Hence initializing variables before use is good programming practice.

The following correction would be acceptable:

e.g: double prime_rate = 8.25, interest_rate;

I/O:

There are several ways that a program can perform input and output. We will discuss streams.

Output using cout

The values of variables as well as strings of text may be output to the screen using cout (you do not need a separate copy of cout for each value to be output).

e.g: the following two sets of statements are equivalent

cout << number_of_items;
cout << " items\n";
cout << number_of_items << " items\n";

 

You can also include arithmetic expressions in output statements (some compilers may require parentheses around the expression).

Example::

cout << "Todays interest rate is: "<< (prime_rate + 9.9);

Note : The operator "<<" is called the insertion operator. Each cout statement must end with a semicolon.

If you need to put a space between two things that are not strings you can do so with a space surrounded by quotes.

e.g: cout << int_rate << " " << prime_rate;

If you need a new line between two non-string items you can use "\n" by itself.

e.g: cout << int_rate << "\n" << prime_rate;

Another way to do this is to use endl (short for end line)

e.g: cout << int_rate << endl << prime_rate;

Note also that endl can be used instead of "\n" with strings.

e.g: cout << "Your rate is " << (int_rate + 3.9) << "% for cash adavances." << endl;

Input using cin

cin is used like cout except the arrows point in the opposite direction.

Note: When a program reaches a cin-statement it waits for input to be entered from the keyboard. It then sets the first variable equal to the first value, the second variable equal to the second value, etc. The computer waits for the return key to be pressed to read the entire input, thus allowing corrections before return is pressed. Numbers must be separated by whitespace (spaces, tabs, newlines) or the program will assume that a single number has been entered. The amount of whitespace is irrelevant. It can be a single space, a tab, lots of spaces, etc…

Another simple program

// convfah.cpp
// CSC 215
// This program converts Celsius to Fahrenheit
//
#include <iostream.h>
int main()
{
double degrees_celsius, degrees_fahrenheit;
const double conv_factor = 1.8;
cout << "Please enter temperature in Celsius: ";
cin >> degrees_celsius;
degrees_fahrenheit = degrees_celsius * conv_factor + 32;
cout << degrees_celsius << " celsius is equivalent to ";
cout << degrees_fahrenheit << " fahrenheit.\n";
return 0;
}