#include<iostream.h>
int main()
{
int age;
int my_age = 23;
cout << "\nHi!
What is your age? Type your age then press return."
cin >> age;
cout << "\nYou
are " << age << "years
young!";
cout << "\nI
am" << my_age << "years
young!";
return(0);
}
FIGURE 3.1
Was the output of the programme:
| Hi! what is your name? Type your age
then press return. |
| You are (your age) years young! |
| I am 23 years young! |
VARIABLE PROPERTIES
Name
Each variable consists
of a name, much like each person on earth has a name. A name
identifies the block of memory the variable occupies. In this
case, we reserved space for a variable we arbitrarily named
'age'. We also arbitrarily named my age 'my_age'.
Address
An address specifies the
exact location of the variable in the memory of your computer.
Like the streets in your neighbourhood or the flats in a building,
no two addresses are the same.
Lifetime
A variable has a specific
lifetime in your programme, you may choose for it to never be
destroyed or you may choose for it to be destroyed at the end
of a function, etc. Reasons for a variable to be destroyed include
freeing up memory space which would otherwise be used up by
the unnecessary variable and destroying a variable because it
is simply no longer needed.
Scope
Scope identifies which
parts of your programme are allowed access to a certain variable.
Think of a variable as a secret FBI or Scotland Yard file. Some
people working for FBI/ Scotland Yard are not high enough on
the ranking ladder to gain access to the secret files. They
may be allowed access to some files but not the ultra secret
files. Those on the top of the secrecy ladder are allowed unlimited
access to the top secret file. The same applies to a variable.
Some parts of a programme are not allowed access to a variable
and some are allowed access.
Value
Here, the value of the
data type 'age' was whatever the user typed in before they pressed
the return key. Notice 'my_age' had a value of 23 assigned to
it. The equal sign placed whatever is in the right side of the
equal sign to be assigned to the left side of the equal sign.
Also notice that the values for both 'my_age' and 'age' were
stored in the two variables and accessed later on using the
statements:
cout << "\nYou
are " << age << "years
young!";
cout << "\nI
am" << my_age << "years
young!";
Data Type
In this case, we chose
the data types for 'age' and 'my_age' to be integers. They could
have been floats (decimals) after all, some people are 7 1/2,
etc.
An integer is any number
from 0 to 32767. If you type in 32768, that would constitute
a float. As an example to test this, cut and paste this piece
of code into your C++ Compiler:
Declaring a variable
To declare a variable you
type the data type followed by what you choose to name your
variable, followed by a semicolon. For example, if you wanted
to declare an integer named number1 or number_2, they would
be as follows:
|
| int number1;
float number_2;
Assigning a value to
a variable
The default value for any
number will always be zero. Above when I typed int number1;,
the value for number1 is automatically zero. You could change
that value to whatever you want (provided it is within range
of integers since the date type we are using here is int).
|
| int number1 = 5;
float number_2 = 29.76;
FIGURE 3.2
#include<iostream.h>
int main()
{
int num1 = 50;
int num2 = 32767;
int num3 = 327657;
float num4 = 327657;
cout << "\nNum1
is: " << num1;
cout << "\nNum2
is: " << num2;
cout << "\nNum3
is: " << num3;
cout << "\nNum4
is: " << num4;
return(0);
}
To summarize, each variable
consists of the following:
TABLE 3.1
| NAME |
This is any name the user
desires. |
| DATA TYPE |
Is it a float or int or char or string? |
| ADDRESS |
This is the location in the memory. |
| SCOPE |
What parts of the programme can see
the variable. |
| VALUE |
What you assign to the datatype ie
int num1 = 10; (it is the specific value of the variable) |
| LIFE TIME |
Length of time of the variable before
it changes it's value or is deleted. |
Explanation of the code
in FIGURE 3.2
#include<iostream.h>:
We have already explained the reason and meaning for #include<iostream.h>
in Section 1 titled "Statements". To refresh your memory, the
compiler needs to access certain files in order to run properly.
If it does not have access to the files, your programme will
not compile at all and your computer may crash. When you include
the #include<iostream.h> statement, you tell your
compiler "I want to create a programme. I know that in order
for you to compile my programme properly, you need a certain
file named "iostream.h" therefore I will type it in my programme
for you to access it." Remember: if you do not type #include<iostream.h>,
your compiler WILL NOT be able to access the file. You must
provide the access by including the file name in the directive.
At this point in time, you do not need to know what #include<iostream.h>
means as it will be explained later when you have more knowledge
of C++.
int main { }: This
tells the compiler that main part of the programme resides within
the two curved brackets { }. It is similar to saying "The main
part of my programme is within this compartment".You are compartmentalising
your code within a specific region to make it easy for the compiler
to understand and access your code. If you did not include the
int main {} statement, your compiler would not be able to find
the code to compile and produce an error message.
int num1 = 50: This
is an example of an assignment statement. By convention, anything
on the right side of the equals sign "=" is assigned to what
is on the left side of the equals sign. That means in this assignment
statement you are saying "I want a variable named num1 to have
the value of 50." The term "int" appears at the beginning of
the statement telling the compiler that you want the variable
num1 with the value of 50 to be an integer. The same applies
for the 3 other assignment statements except that in float num4
= 327657, you are telling the compiler that you want the variable
to be a float.
cout <<
"\nNum1 is: " << num1:
Was the output of the programme:
| Num1 is 50 |
| Num2 is 32767 |
| Num3 is -23 |
| Num4 is 327657 |
Each datatype can only
occupy a specific amount of space in the memory, this largely
depends on the operating system of the user. In most operating
systems, an integer is assigned 4 bytes of space. If a user
assigns a value to an integer that occupies greater than 4 bytes,
the output of your programme will be "garbage" or "junk". Data
types called long or double are used to handle
values of greater than 4 bytes.
Other data types include
"char" which is used for characters (numbers, letters, or symbols).
A "char" cannot be greater than one character. REMEMBER!! C++
IS CASE SENSITIVE!! This means that if you name an int num1,
it is not the same as int NUM1 or int NuM1. They are three separate
variables.
FIGURE 3.3
#include<iostream.h>
int main()
{
char num1 = 504;
char num2 = 3;
char name1 = 'hi';
char name2 = 'k';
cout << "\nNum1
is: " << num1;
cout << "\nNum2
is: " << num2;
cout << "\nName1
is: " << name1;
cout << "\nName2
is: " << name2;
return(0);
}
Was the output of the programme:
| Num1 is Ø |
| Num2 is • |
| Name1 is h |
| Name2 is k |
STRINGS VS CHAR
Why was this you may ask?
In our previous example, we noticed that if we had an integer
named num1 which was greater than 32767, the programme would
print out garbage. The same applies for the data type "char".
It will only print out one char. Instead of printing "hi" the
program only printed the "h". The reason for this is the same
reason for the integer not printing digits greater that 32767
-which is the use of the datatype 'char' is too small (occupies
too small a memory location) to hold greater than one character.
If the user wanted to name a block of memory "hello" they would
have to use a data type called "string" or a character array
(discussed later).
For strings, we must include
the cstring.h file. Beware that some old compilers may not handle
strings. In that case, use pointers or character arrays to overcome
that obstacle (both discussed later). Also, strings must be
enclosed in DOUBLE quotes not single quotes.
FIGURE 3.4
#include<iostream.h>
#include<cstring.h>
int main()
{
string my_string1 = "This is my
programme";
string my_string2 = "book";
cout << "\nString1
is: " << my_string1;
cout << "\nString2
is: " << my_string2;
return(0);
}
Was the output of the programme:
| String1 is: This is my programme |
| String2 is: book |
RESERVED WORDS
There are certain words
in the C++ language that CANNOT be used as variable names. These
name include reserved words such as "int", "float", "char",
"string", "include",
NEWLINE CHARACTER (CARRIAGE
RETURN)
The character '\n' is a
called a newline character. It prints your output onto a new
line as opposed to having all your output on the same line.
An alternative to the newline character is 'endl'. Both can
be placed at the end or beginning of a statement as shown in
Figure 3.5:
FIGURE 3.5
#include<iostream.h>
#include<cstring.h>
int main()
{
string my_string1 = "This is my
programme";
string my_string2 = "book";
cout << "\nString1
is: " << my_string1;
cout << "\nString2
is: " << my_string2;
cout << "String1
is: \n" << my_string1;
cout << "String2
is: \n" << my_string2;
cout << endl
<< "String1 is: " << my_string1;
cout << endl
<< "String2 is: " << my_string2;
cout << "String1
is: " << my_string1 << endl;
cout << "String2
is: " << my_string2 << endl;
return(0);
}
| String1 is: This is my programme |
| String2 is: bookString1 is: |
| This is my programmeString2 is: |
| book |
| String1 is: This is my programme |
| String2 is: bookString1 is: This is
my programme |
| String2 is: book |
Notice that when you place
'\n' or << endl at the end of a statement,
the sentence continues on the same line, therefore it is best
to place them before the statement you want printed to the screen.