Lesson3
Data
types in C++
Integer
- positive or negative whole number including zero. (keyword
int)
Largest and smallest integer available dependent on the
compiler/computer.
2
bytes - 32,767 to -32768
4
bytes - 2,147,483,647 to +2,147,483,648
ex.
5 -32 24
0
Floating - Point Numbers - signed or unsigned real numbers having a
decimal point.
ex. 5.0 4. -2.33333
float - 4 bytes
double 8 bytes
Sometimes displayed in exponential notation.
Number Exponential Notation Scientific Notation
23.5
2.35e1 2.35 X 101
0.235
2.35e-1 2.35
X 10-1
Moving
the decimal point right gives a positive exponent.
Moving
the decimal point left gives a negative exponent.
Character
- 1 byte
letters, digits, all special characters on the keyboard such as . ,
! ? @ # $ % ^ & * ( ),
etc.
To display a single character, we enclose it
in single quotes.
cout << 'a' ; //displays
the letter a 1 byte long
cout << "a"; //
not a character but a character string (2 bytes)
cout << “The rain in Spain”; character string of 17 characters.
Variable
- a name used to denote a section of computer memory. Each cell in memory has an address. The variable name is used instead of the
actual numeric address.
We
use declaration statements to tell the computer to put aside space in memory
and associate that space with a variable.
The amount of space set aside depends on the data type of the variable.
General
form of a declaration statement:
datatype variableName;
Ex.
int num1; //integer variable
int num2;
float average; //float variable
char letter; //character
variable
multiple declarations can be written as follows:
int num1, num2;
Variables
get values placed into their memory locations using assignment statements.
num1
= 5;
letter = 'a';
The
equal sign does not denote equality in the arithmetic sense. What appears on the right hand side of the
equal sign is evaluated and then stored in the variable location on the left.
Variables
of type string need the string library to be included in the program. In the first versions of C++ string variables
and the functions used with them were not a part of the language.
#include <iostream>
#include
<string>
using namespace std;
int
main()
{
int
num1, num2;
float amount;
char letter;
string name;
num1 = 4;
num2 = 9;
letter = 'z';
amount = 22.50;
name = “Imberman”;
cout
<< num1 << ' ' << num2 << ' ' << letter
<< ' '
<< amount << endl;
num2 = 15;
cout
<< num1 << ' ' << num2 << ' ' << letter
<< ' '
<< amount << endl;
cout
<< name << “ is an interesting Profesor.”
<< endl;
return 0;
}
We can
initialize a variables value in the declaration statement.
#include <iostream>
#include
<string>
using namespace std;
int
main()
{
int
num1, num2;
float amount;
char letter;
string name;
num1 = 4;
num2 = 9;
letter = 'z';
amount = 22.50;
name =
"Imberman";
cout
<< num1 << ' ' << num2 << ' ' << letter
<< ' '
<< amount << endl;
num2 = 15;
cout
<< num1 << ' ' << num2 << ' ' << letter
<< ' '
<< amount << endl;
cout
<< name << " is an interesting Profesor."
<< endl;
return 0;
}
Notice:
Proper format - place declarations after the opening curly bracket. Skip a line after the declarations.
The
variable name is a tag for the first byte of the memory location that will
store the given information.
In
reality, this corresponds to a memory address.
We can print out the address of a variable by placing an ampersand
(&) in front of the variable's name in a cout
statement.
Ex. cout
<< num1 << ' ' << & num1;
The above prints out the value of num1 and its address.
#include <iostream>
using namespace std;
int
main()
{
int
num;
num = 22;
cout
<< "The value stored in num is " << num << endl;
cout
<< "The address of num = " << &num << endl;
return 0;
}
OUTPUT
The value stored in
num is 22
The address of num =
006AFDF4
Press any key to
continue
Simple
arithmetic expressions:
+ addition
- subtraction
* multiplication
/ division
sum
= 4 + 5;
num2
= 6;
num3
= 8;
sum
= num2 + num3;
Any
expression can appear on the right hand side of an assignment statement. An expression is any combination of
constants, variables, and function calls that yield a result.
Only
a single variable is allowed on the left hand side of an assignment statement .