C++ Programming: Nyambati N. Richard
C++ Programming: Nyambati N. Richard
C++ Programming: Nyambati N. Richard
Nyambati N. Richard
Basic Structure of C++ Program
Probably the best way to start learning a programming
language is by writing a program. Therefore, here is our
first program:
1. // my first program in C++
2. #include <iostream>
3. using namespace std;
4. int main ()
5. {
6. cout << "Hello World!";
7. return 0;
8. }
Basic Structure of C++ Program
// my first program in C++ is a comment line.
#include <iostream>: Lines beginning with a hash sign
(#) are directives for the preprocessor.
using namespace std; :All the elements of the standard
C++ library are declared within what is called a
namespace, the namespace with the name std.
int main () : This line corresponds to the beginning of
the definition of the main function. The main function is
the point by where all C++ programs start their
execution
Basic Structure of C++ Program
cout << "Hello World!"; This line is a C++
statement that prints “Hello World” on the screen
return 0; The return statement causes the main
function to finish.
Variables
A variable is a symbolic name for a memory
location in which data can be stored and
subsequently recalled.
Variables are used for holding data values so that
they can be utilized in various computations in a
program.
Variables
All variables have two important attributes:
A type which is established when the variable is
defined (e.g., integer, real, character). Once defined,
the type of a C++ variable cannot be changed.
A value which can be changed by assigning a new
value to the variable. The kind of values a variable
can assume depends on its type. For example, an
integer variable can only take integer values (e.g., 2,
100, -12).
Identifiers (Variable Names)
float f = 0.52L;
signed char = -1786;
char c = '$' + 2;
sign char h = '\111';
char *name = "Peter Pan";
unsigned char *num = "276811";
Exercises
Assignment (=)
The assignment operator assigns a value to a variable.
a = 5;
// assignment operator
#include <iostream>
using namespace std;
int main ()
Operators
{
int a, b; // a:?, b:?
a = 10; // a:10, b:?
b = 4; // a:10, b:4
a = b; // a:4, b:4
b = 7; // a:4, b:7
cout << "a:";
cout << a;
cout << " b:";
cout << b;
return 0;
}
Operators
Arithmetic operators ( +, -, *, /, % )
The five arithmetical operations supported by the
C++ language are:
+ addition
- subtraction
* multiplication
/ division
% modulo
a = 11 % 3;
Operators
expression is equivalent to
value += increase; value = value + increase;
a -= 5; a = a -5;
a /= b; a = a / b;
price *= units + 1; price = price * (units + 1);
Operators
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Operators
// number echoer
#include <iostream>
using namespace std;
int main ()
Control Structures
{
unsigned long n;
do {
cout << "Enter number (0 to end): ";
cin >> n;
cout << "You entered: " << n << "\n";
} while (n != 0);
return 0;
}
Control Structures
The for loop
Its format is:
for (initialization; condition; increase) statement;
and its main function is to repeat statement while
condition remains true, like the while loop.
0 1 2 3 4
Bill
y
Arrays
Where each blank panel represents an element of the
array, that in this case are integer values of type int.
These elements are numbered from 0 to 4 since in arrays
the first index is always 0, independently of its length.
Like a regular variable, an array must be declared before
it is used. A typical declaration for an array in C++ is:
type name [elements];
where type is a valid type (like int, float...), name is a
valid identifier and the elements field (which is always
enclosed in square brackets []), specifies how many of
these elements the array has to contain.
Arrays
Therefore, in order to declare an array called billy
as the one shown in the above diagram it is as
simple as:
int billy [5];
In both cases, local and global, when we declare an
array, we have the possibility to assign initial
values to each one of its elements by enclosing the
values in braces { }. For example:
int billy [5] = { 16, 2, 77, 40, 12071 };
Arrays
In any point of a program in which an array is
visible, we can access the value of any of its
elements individually as if it was a normal variable,
thus being able to both read and modify its value.
The format is as simple as:
name[index]
Arrays
For example, to store the value 75 in the third
element of billy, we could write the following
statement:
billy[2] = 75;
and, for example, to pass the value of the third
element of billy to a variable called a, we could
write:
a = billy[2];
Therefore, the expression billy[2] is for all purposes
like a variable of type int.
Arrays
// arrays example
#include <iostream>
using namespace std;
int billy [] = {16, 2, 77, 40, 12071};
int n, result=0;
int main ()
Arrays
{
for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}
cout << result;
return 0;
}
Multidimensional arrays
0 1 2 3 4
0
Jimm 1
y 2
Multidimensional arrays
jimmy represents a bidimensional array of 3 per 5
elements of type int. The way to declare this array
in C++ would be:
int jimmy [3][5];
and, for example, the way to reference the second
element vertically and fourth horizontally in an
expression would be:
jimmy[1][3]
Multidimensional arrays
#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT][WIDTH];
int n,m;
int main ()
Multidimensional arrays
{
for (n=0;n<HEIGHT;n++)
for (m=0;m<WIDTH;m++)
{
jimmy[n][m]=(n+1)*(m+1);
}
return 0;
}
Functions
Using functions we can structure our programs in a
more modular way, accessing all the potential that
structured programming can offer to us in C++.
A function is a group of statements that is executed
when it is called from some point of the program.
The following is its format: