|
||
|
|
| Learn to program in C++ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Posted July 2, 2006. Written by John Zervos, author of the text Byte Into C++ (2002). Tertiary Press: Melbourne. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This
article provides a comprehensive yet simple to understand treatment of the
C++ language for beginners. The article is divided into a series of
separate lessons as follows:
Lesson 1 - Introduction |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Data type |
Description |
|
Boolean |
A single bit of data that can take on the values 0 (false) or 1 (true) only. |
|
Character |
The code for a single character (such as a symbol, letter, or digit). |
|
Integer |
A whole number. |
|
Floating point |
A number that can have a fractional component. |
|
String |
Two or more characters together. |
C++ data types
Modern C++ compilers recognize the
simple data types shown in the table below. The data types shown in the
first column of this table are C++ keywords.
|
Data type |
Description |
Range | Size (bytes) |
| bool | boolean | 0 to 1 | 1 bit |
| unsigned char | unsigned character | 0 to 255 | 1 |
| char | character | -128 to +127 | 1 |
| unsigned int | unsigned integer | 0 to 4,294,967,295 | 4 |
| int | integer | -2,147,483,648 to +2,147,483,647 | 4 |
| unsigned short | unsigned short integer | 0 to 65,535 | 2 |
| short | short integer | -32,768 to +32,767 | 2 |
| unsigned long | unsigned long integer | 0 to 4,294,967,295 | 4 |
| long | long integer | -2,147,483,648 to +2,147,483,647 | 4 |
| float | floating point | ±3.4 x 10±38 | 4 |
| double | double precision float | ±1.7 x 10±308 | 8 |
| long double | long double precision float | ±3.4 x 10-4932 to ±1.1 x 10+4932 | 10 |
Memory and variables
Data in computer program is normally stored in the computer's memory (RAM). This allows the data to be easily retrieved, manipulated, and calculated on. The memory locations that store this data are referred to as variables as the data can be changed at any time. In third generation languages (3GL's) like C++, variables are given a unique identifier (symbolic name) also known as a variable name. Program instructions then access the data by referring to its variable name. Memory locations whose data cannot be changed still tent to be treated in the same way as variables but are referred to as constants.
Variable names
Variable names are chosen by the programmer and should clearly describe the purpose of the data stored in that variable. For example, if a particular variable is to store the balance of a bank account, the variable should be names something like bankAccount. Avoid using single letter or cryptic variable names such as X or BA.
The following list outlines the rules for naming C++ variables:
You can use any uppercase or lowercase letter: a to z and A to Z.
You can use the numerals 0 to 9.
The only symbol allowed is the underscore character "_".
Variable names must not begin with a numeral.
You cannot use any of the C++ reserved keywords.
Only the first 32 characters of a variable name may be read by the compiler.
Declaring variables
Before using a variable in a C++ programme, it must first be declared to the compiler. A variable declaration statement consists of the the type of data it is going to store, followed by the programmer-chosen variable name. An example is shown below:
int age;
This statement tells the compiler that the variable age is going to be used in following statements, and that it will store integer type data. Some other valid variable declaration examples are shown below:
float height;
double weight;
char symbol;
int quantity, count, items;
The last example demonstrates that multiple variables of the same type can be declared in the one statement by separating each with a comma.
Variables can be declared anywhere in a program provided they are declared before their first use. However, it's customary to declare most variables at the beginning of a program (just inside the opening brace of the main function).
When variables are declared they can also be initialised (given a starting value). This is done using the assignment operator (the equals sign) to assign a value to the variable. Some examples are shown below:
int age = 21;
float weight = 57.85;
char symbol = '$';
int quantity = 0, sum = 0;
Notice in the third example that character values must be surrounded by single quotation marks. Do not confuse this with the double quotation marks required around string values.
The forth example demonstrates how multiple initialisations of the one data type can be combined in the one statement using commas to separate the initialisations.
Variables whose data do not need to be changed throughout the program should be made constants. A constant is created by placing the keyword const at the beginning of an initialisation statement. An example is shown below:
const double pi = 3.141592654;
Constant declarations are normally placed at the top of the program outside of the main functions so they are available to other functions in the program (covered in a later topic).
Displaying the contents of a variable
The cout object is used to display the contents (value) of a variable. An example is shown below:
cout << age;
Notice that the variable age is not surrounded by double quotation marks. If this were the case, the word "age" would be printed on the screen which is not what's required. A more user friendly version is shown below:
cout << "Your age is " << age << endl;
If the value of age was say 21, then this statement would display Your age is 21 on the screen.
Inputting data from the keyboard
Just as cout is used to output data to the screen, cin is used to input data from the keyboard. The cin object is used with the "get from" or "extraction" operator, >>, followed by the variable in which the received data is to be stored. An example is shown below:
int age;
cout << "Please enter your age: ";
cin >> age;
When using cin you'll need to remember to include it in the program as follows:
#include <iostream>
using std::cin;
The program below prompts the user for the radius of a circle and displays its area.
|
1: #include <iostream> 2: using std::cout; 3: using std::cin; 4: using std::endl; 5: 6: const double pi = 3.1416; 7: 8: int main() 9: { 10: double radius, area; 11: cout << "Please enter the radius: "; 12: cin >> radius; 13: area = pi * radius * radius; 14: cout << "The area is " << area << endl; 15: return 0; 16: } |
This program works as follows:
Line 1: Includes the file iostream in the program.
Lines 2 to 4: Tells the compiler to only use cout, cin, and endl from iostream.
Line 6: Declares pi as a constant double precision floating point type having the value 3.1416.
Line 8: This is the start of the main function.
Line 9: This is the opening brace for the main function.
Line 10: Declares variables radius and area and double precision types.
Line 11: Prompts the user to input a radius value.
Line 12: Gets the user-entered value from the keyboard and stores it in the variable radius.
Line 13: Calculates the value of area. The symbol for multiplication is the asterisk.
Line 14: Displays the result in a user-friendly way.
Line 15: Ends execution of the function and returns the integer 0 back to the calling program.
Line 16: This is the closing brace for the main function.
There are two ways of dealing with strings in C++: the older style C strings (character arrays), or as C++ objects. Both types of strings are dealt with in a later lesson. However, a brief treatment of C++ strings is given below.
To use C++ strings in a program, you must include the string header file and use its string object as shown below:
#include <string>
using std::string;
This allows you to use string as a data type in your programs. The following example shows how you can initialise and display a string of this type.
string title = "Learn to do stuff";
cout << title << endl;
In the example above, string is a class (data type) and title is the name of the string object (variable). The string object title is initialised with the string value "Learn to do stuff". The contents of this string can be displayed in the same way as you would display the contents of any other variable using cout. You'll learn more about C++ strings, classes and objects in later lessons.
| © 2006 learn2dostuff.com |