Chapter 2 Lecture Notes
HELLO WORLD PROGRAM
Let's now consider an actual C++ program that can be compiled and executed.
We have seen one previously in Program 0. We call this type of program a
Hello World program because the program simply outputs a sentence (string
of characters) to the screen. Consider the follwoing is a hello world
program with the line numbers added.
1 #include "iostream.h"
2 //This is a hello world program
3 int main()
4 {
5 cout << "Hello World." << endl;
6 return 0;
7 }
1) This line includes the input/output stream library. This library provides
functions which allow you to accept input from the keyboard as well as write
to the screen.
2) A comment can be found on this line. A comment is (DEF) defined as
characters which are ignored by the compiler. The (//) signals the compiler
to ignore everything until the end of the line. Another comment is (/*...*/)
which "comments out" everything between the /* and */. Comments are actual
English text that are used to indicate to the reader what your program is
doing.
3) Every C++ program requires a main routine. This is the entrance
point of execution for a program. The 'int' found before the 'main()'
indicated that we are returning a value. In this case we are returning a
value to the operating system.
4) The left curly brace indicates a 'begin'. Specifically, in this program,
it indicates the beginning of the main routine.
5) This line uses the 'cout' function which allows us to write to the screen.
Note that 'cout' has been defined in the iostream library which was included
in line 1.
6) The main routine, specified in line 3, expects that we return an integer.
Line 6 simply returns that expected integer, in this case a 0.
7) The right curly brace indicates an 'end' which matches up with the begin
of line 4.
OTHER FILES (and extensions)
What we have just considered an example of a C++ source file. As you may
remember from Program 0, source files have the .cpp extension attached to the
file name. For example 'Hello.cpp'.
There are also other file name extensions which indicate different file types.
Extensions Files
.cpp Source program
.obj Object
.exe Executable
.h Header
.dat Data
DATA TYPES
Before we consider how to accept input from the keyboard let us first consider
data types. Data types will allow us to set aside space in memory by declaring
variables, this will allow us to take input.
The following table displays all the possible data types which are available
for your use.
Data Type Description
int Integer (+/-)
short Short integer (smaller range than int)
long Long integer (supposedly has larger range than an int)
unsigned Unsigned integer (no negative values allowed)
char Character
bool boolean (true/false)
float Real number (+/-)
double More precise real number (+/-)
Given these data types we inspect the number of bytes set aside in memory
when we declare a variable of the certain data type.
Data Type Maximum Data Type Value # bytes
short (int) 32767 2
int 2147483647 4
long (int) 2147483647 4
unsigned int 4294967295 4
char 255 1
bool 2147483647 4
float 3.40282e+38 4
double 1.79769e+308 8
TAKING INPUT FROM THE KEYBOARD
Now that we have discussed data types let's use it in a program. Consider
the following simple program which accepts input from the keyboard.
#include "iostream.h"
int main()
{
int DemoInt;
cout << "Hello World." << endl;
cout << "Enter an integer: " << endl;
cin >> DemoInt;
cout << "You entered the following integer " << DemoInt << ".";
return 0;
}
Output:
Hello World.
Enter an integer: 3
You entered the following integer 3.
OPERATIONS
The following table is a list of all the simple mathematical and comparisons
with the associated operations.
Mathematical Operators
+ Addition
- Subtraction
* Multiplication
/ Division
+= A += B equivalent to A = A + B;
-= A -= B equivalent to A = A - B;
/= A /= B equivalent to A = A / B;
*= A *= B equivalent to A = A * B;
% Modulus operator
Comparison Operators
= Assignment operator
== Equality operator
> Greater than
< Less than
<= Less than or equal
>= Greater than or equal
! The 'not' operator
If you are unsure of the modulus operation: the modulus operator (%)
results in the remainder of two numbers:
33 % 4 = 1
since 33 / 4 = (4 * 8) + 1
^ ^
| |
Quotient Remainder (Modulus)
5 % 3 = 2
since 5 / 3 = (3 * 1) + 2
45 % 5 = 0
since 45 / 5 = (9 * 5) + 0
Let's now write a program which performs some of the above operations.
#include <iostream.h>
int main()
{
int A, B; //Two integers A and B
A = 5; //Assign values to A and B
B = 10;
cout << "A + B = " << A + B << endl;
cout << "A - B = " << A - B << endl;
cout << "A * B = " << A * B << endl;
cout << "B / A = " << B / A << endl;
cout << "B % A = " << B % A << endl;
B /= A;
cout << "B = " << B << endl;
B += A;
cout << "B = " << B << endl;
B *= A;
cout << "B = " << B << endl;
B -= A;
cout << "B = " << B << endl;
return 0;
}
Output
A + B = 15
A - B = -5
A * B = 50
B / A = 2
B % A = 0
B = 2
B = 7
B = 35
B = 30
CASTING
The previous example has only performed operations on two integers. Is it
possible to perform operations on different data types: for instance,
float = float + int, int = char + int, char = int / int?
We will discover that we can make these conversions through an explicit
type conversion operation called casting. Casting requires that you specify
a type to which the result will be converted. For example,
float B;
int A;
B = (float) A;
This says that B is assigned a float value which is converted from an integer.
It is important to note that A is not changed to a float data type, it is
left unaltered as an integer.
Let's explore more possible type conversions through a program.
#include <iostream.h>
int main()
{
int ASCIIval;
char chr = 'A';
ASCIIval = (int)chr; //User-casting
cout << ASCIIval << endl;
ASCIIval = chr; //Auto-Casting
cout << ASCIIval << endl;
ASCIIval += 5; //Adding and casting int to char
chr = (char)ASCIIval;
cout << chr << endl;
ASCIIval += 5; //Is there an auto-cast for char to int?
chr = ASCIIval;
cout << chr << endl;
return 0;
}
Output:
65
65
F
K
The above program uses two types of casts: user-casts and auto-casts.
User-casting is the operation specified in the first example where the
user must specify the type. An auto-cast refers to casting which is
automatically converted at run-time by the compiler. Many of the legal
casts can be done automatically, however there are situations where
auto-casts do not produce the desired results. Do not overcode by
casting when it is not necessary.
Chris Alvin