Origin of C++
A simple C++ Program: #include <iostream.h> Another simple C++ program (pg. 21):
#include <iostream.h> Developing a C++ Program An iterative process involving the following three steps: Basic structure (pg. 25): #include <iostream.h> When developing C++ code remember to: Variables: e.g: 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: Example:
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; Note Assignment Statements Assignment statements are used to change the value of a variable. The general form of an assignment statement is: <variable> = <expression>; Example:
int main()
{
cout << "Hello World!\n";
return 0;
}
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;
}
int main()
{
Variable_Declarations
Statement_1;
Statement_2;
...
Statement_n;
return 0;
}
{ and } on a new line.
int account_number;
double prime_rate, interest_rate;
total_weight = weight * number_of_items; 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 () } Although syntactically valid, execution will be unpredictable, since The following correction would be acceptable: e.g:
total_weight = weight;
number_of_items = 16;
{
double prime_rate, interest_rate;
interest_rate = prime_rate + 9.9;
...
return 0;
I/O: There are several ways that a program can perform input and output. We will discuss streams. Output using The values of variables as well as strings of text may be output to the screen using e.g: the following two sets of statements are equivalent cout << number_of_items; 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: If you need a new line between two non-string items you can use " e.g: Another way to do this is to use e.g: Note also that e.g: Input using
cin
cin
Note: When a program reaches a Another simple program // convfah.cpp
cin" is the stream associated with the keyboard.
cout << " items\n";
cout << number_of_items << " items\n";
// 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;
}